Getting block via NMS?

Discussion in 'Spigot Plugin Development' started by Strahan, Oct 3, 2018.

  1. Strahan


    Hi. I'm trying to figure out tag manipulation of blocks. I read one must get a TileEntity. I'm doing it in a PlayerInteractEvent so I have:

    Code (Text):
    public void onPlayerInteract(PlayerInteractEvent e) {
        if (!e.getClickedBlock().getType().equals(Material.AIR)) {
            if (e.getAction() == Action.RIGHT_CLICK_BLOCK && e.getHand() == EquipmentSlot.HAND) {
                CraftWorld cw = (CraftWorld)e.getPlayer().getLocation().getWorld();
                Block tmp = cw.getBlockAt(173, 64, 177);
                e.getPlayer().sendMessage("Block is " + tmp.getType().name());
                TileEntity te = cw.getHandle().getTileEntity(new BlockPosition(173, 64, 177));
                if (te == null) { e.getPlayer().sendMessage("Null TileEntity"); return; }
    I normally would not of course use hard coded values but I was just testing something. At 173/64/177 I have a block of sand. If I right click it, I get "Block is SAND" followed by "Null TileEntity". If it sees the sand there when I get the block, why is the TileEntity returning null? What did I goof up? Thanks!
  2. I might be wrong but ain't a tile entity like a blocks with inventorys and extra data eg chest, hopper, furnace ext
  3. Strahan


    Oooohh, OK. Is it possible to access tags on "regular" blocks, like sand?
  4. What kind of "tags"
  5. md_5

    Administrator Developer

  6. Strahan


    I wanted to be able to tag a block with custom fields containing information about it that my plugin could read when it was interacted with. I guess I can just use the config file, but I felt that keeping the block data on the actual block would be cleaner than a config file that is a mile long with block coordinates.
  7. Senmori

    Senmori Retired Resource Staff
    Retired Patron

    Non tile entity blocks do not have NBT tags that you can add to.
    Your best bet is to save the location to a config and do whatever you want from there. Even for Tile entities you should save locations to file, it saves you the trouble of dealing with NMS.