Ender Chest打开和关闭动画

时间:2015-10-16 03:50:15

标签: java plugins minecraft bukkit

我希望取消ender胸部打开事件,并在右键单击时显示打开和关闭动画。我目前的代码是:

public void changeChestState(Location loc, boolean open) {
    if (open) {
        for (Player p : Bukkit.getOnlinePlayers()) {
            p.playNote(loc, (byte) 1, (byte) 1);
        }
    } else {
         for (Player p : Bukkit.getOnlinePlayers()) {
             p.playNote(loc, (byte) 1, (byte) 0);
         }
    }
}

当我右键单击胸部时,我听到声音,但没有动画发生。谢谢你的帮助!

1 个答案:

答案 0 :(得分:5)

您可以向播放器发送PacketPlayOutBlockAction数据包,让他们看到胸部打开或关闭。该包用于显示音符粒子,活塞运动和胸部状态变化。

您需要创建一个BlockPosition并获取NMS Block(Minecraft块的类型,在这种情况下是胸部)以及提供两个字节的数据。在胸部动画的情况下不使用第一个字节,第二个字节表示胸部状态的变化,0表示关闭,1表示开胸。下面是您重写的方法,以显示相应的动画。

@SuppressWarnings("deprecation")
public static void changeChestState(Location loc, boolean open) {
    byte dataByte = (open) ? (byte) 1 : 0; // The byte of data used for the note and animation packet (1 if true, 0 if false)
    for (Player player : Bukkit.getOnlinePlayers()) {
        player.playNote(loc, (byte) 1, dataByte); // Play the sound
        BlockPosition position = new BlockPosition(loc.getBlockX(), loc.getBlockY(), loc.getBlockZ()); // Create the block position using loc
        // Instantiate animation packet, get NMS Block using getById() and loc.getBlock() (deprecated), the server version may vary
        PacketPlayOutBlockAction blockActionPacket = new PacketPlayOutBlockAction(position, net.minecraft.server.v1_8_R3.Block.getById(loc.getBlock().getTypeId()), (byte) 1, dataByte);
        ((CraftPlayer) player).getHandle().playerConnection.sendPacket(blockActionPacket); // Send animation packet to CraftPlayer
    }
}