How to get a custom block on PlayerInteractEvent

Discussion in 'Spigot Plugin Development' started by Dcgames, May 16, 2017.

  1. Hello, I'm trying to open a menu when player click on a specific block, This block has a custom NBT, But I can't get the ItemMeta/ItemStack on PlayerInteractEvent So how can I check if the clicked block is that specific block?

    Thank you.
     
  2. electronicboy

    IRC Staff

    Block#getState()#getData() or something like that
    PIE will expose the block they interacted with in the event, if they did
     
  3. So my custom NBT will not reset when I place the block right? I'm using a NBT-API
     
  4. electronicboy

    IRC Staff

    You'd have to double-check, fairly sure that some info is lost but I can't recall off the top of my head, worse case you have to hook the block place event and transfer the NBT you set over manually
     
  5. I was trying but I could not do it, Have another way to check if this block placed is my specific block? I was trying to save the block location on config when player place the block, But I don't know how I check when player place the block too.
     
  6. I did not explain well, I know how to save. But how do I check if the block that the player put is the same block that I want to open the menu? The block who I want has a custom NBT
     
  7. Someone know how can I do that?
     
  8. NBT data stored on an item will not be transferred to the block when placed. Most blocks can't have any data on them at all, only tile entities like chests.
     
  9. You can just store a reference to the block on place (and store the location in a file/db if it needs to be persistent beyond restarts)
     
  10. Yeah, but when the player will place the block, How I'll check if this block is the custom block?
     
  11. uh, use block.setMetadata(...) and block.getMetadata(...)? although it will reset after a restart..
     
  12. Hey, I'm trying to use: "NBTTagCompound" now. This is the code to get the ItemStack:

    Code (Text):
        private ItemStack add() {
            ItemStack i = new ItemStack(Material.CLAY, 1);
            net.minecraft.server.v1_8_R2.ItemStack nms = CraftItemStack.asNMSCopy(i);
            NBTTagCompound tag = new NBTTagCompound();
            if (nms.getTag() != null) {
                tag = nms.getTag();
            } else {
                nms.setTag(new NBTTagCompound());
                nms.setTag(tag);
            }
            tag.setString("Machine", "Machine");
            ItemStack item = CraftItemStack.asCraftMirror(nms);
            return item;
        }
    But how do I check on "BlockPlaceEvent"(when player place the block) if this Item has this custom NBT tag?
     
  13. Look at the item in the player's main hand (not sure how to check for offhand block place...), call getTag like you're doing above.
     
  14. You might find it much simpler to generate an additional .yml file, store the block locations. This is the simplest way I use to check when I'm dealing with plugins that require custom blocks. After that its just a matter of getting and saving info on disable and enable from the file