Search code examples
bukkit

How to get block data in Bukkit API?


I had a block with this data:

{display:{Name:"{"text":"Монитор"}"},SkullOwner:{Id:[I;-1626538924,-1410775545,-1984946359,940139578],Properties:{textures:[{Value:"eyJ0ZXh0dXJlcyI6eyJTS0lOIjp7InVybCI6Imh0dHA6Ly90ZXh0dXJlcy5taW5lY3JhZnQubmV0L3RleHR1cmUvODJmMTkxYTIyNjM5MzI1MjI2MmMyMmExNTQxY2FlNzkzNDlkMDExMmQzOTlkNmVjZjczOTU5MWQzZmM5NDdmIn19fQ=="}]}}}

How do I get this data from plugin?


Solution

  • You should use NMS (net.minecraft.server) to get an NBT Tag.

    Here is an example with 1.8.8 (v1_8_R3, tested with it), but just with your import this should works :

    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();
    }
    

    The field name see to don't change accross version (always c).

    Documentation: