如何获取Bukkit API中的区块数据?
How to get block data in Bukkit API?
我有一个数据块:
{display:{Name:"{"text":"Монитор"}"},SkullOwner:{Id:[I;-1626538924,-1410775545,-1984946359,940139578],Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvODJmMTkxYTIyNjM5MzI1MjI2MmMyMmExNTQxY2FlNzkzNDlkMDExMmQzOTlkNmVjZjczOTU5MWQzZmM5NDdmIn19fQ=="}]}}}
如何从插件中获取这些数据?
您应该使用 NMS (net.minecraft.server
) 来获取 NBT 标签。
这是一个 1.8.8 的例子(v1_8_R3,用它测试过),但只要你导入它就可以工作:
Block block = event.getClickedBlock();
Location w = block.getLocation();
CraftWorld cw = (CraftWorld) w.getWorld(); // CraftWorld is NMS one
// For 1.8 to 1.12 :
TileEntity te = cw.getTileEntityAt(w.getBlockX(), w.getBlockY(), w.getBlockZ());
// for 1.13+ (we have use WorldServer)
TileEntity te = cw.getHandle().getTileEntity(new BlockPosition(w.getBlockX(), w.getBlockY(), w.getBlockZ()));
try {
PacketPlayOutTileEntityData packet = ((PacketPlayOutTileEntityData) te.getUpdatePacket()); // get update packet from NMS object
// here we should use reflection because "c" field isn't accessible
Field f = packet.getClass().getDeclaredField("c"); // get field
f.setAccessible(true); // make it available
NBTTagCompound nbtTag = (NBTTagCompound) f.get(packet);
plugin.getLogger().info(nbtTag.toString()); // this will show what you want
} catch (Exception exc) {
exc.printStackTrace();
}
字段名称不会跨版本更改(始终 c
)。
文档:
我有一个数据块:
{display:{Name:"{"text":"Монитор"}"},SkullOwner:{Id:[I;-1626538924,-1410775545,-1984946359,940139578],Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvODJmMTkxYTIyNjM5MzI1MjI2MmMyMmExNTQxY2FlNzkzNDlkMDExMmQzOTlkNmVjZjczOTU5MWQzZmM5NDdmIn19fQ=="}]}}}
如何从插件中获取这些数据?
您应该使用 NMS (net.minecraft.server
) 来获取 NBT 标签。
这是一个 1.8.8 的例子(v1_8_R3,用它测试过),但只要你导入它就可以工作:
Block block = event.getClickedBlock();
Location w = block.getLocation();
CraftWorld cw = (CraftWorld) w.getWorld(); // CraftWorld is NMS one
// For 1.8 to 1.12 :
TileEntity te = cw.getTileEntityAt(w.getBlockX(), w.getBlockY(), w.getBlockZ());
// for 1.13+ (we have use WorldServer)
TileEntity te = cw.getHandle().getTileEntity(new BlockPosition(w.getBlockX(), w.getBlockY(), w.getBlockZ()));
try {
PacketPlayOutTileEntityData packet = ((PacketPlayOutTileEntityData) te.getUpdatePacket()); // get update packet from NMS object
// here we should use reflection because "c" field isn't accessible
Field f = packet.getClass().getDeclaredField("c"); // get field
f.setAccessible(true); // make it available
NBTTagCompound nbtTag = (NBTTagCompound) f.get(packet);
plugin.getLogger().info(nbtTag.toString()); // this will show what you want
} catch (Exception exc) {
exc.printStackTrace();
}
字段名称不会跨版本更改(始终 c
)。
文档: