1.12.2 Compare ItemStack and Block meta from BlockBreakEvent

Discussion in 'Spigot Plugin Development' started by C4LLM3P3T3R, Jan 30, 2020.

  1. Hello,
    the title is my problem because i need to check if the block under the event block is my item that i created that has a special meta. If i place my item (with special meta) and regular item without special meta it triggers for both of them code 1, 2
  2. Correct me if I'm wrong, but I believe that a block cannot contain the same Item-Meta as an Item, especially a Block cannot have a lore...
    Therefore your check does not work. If you want to check whether the Block in a BlockBreakEvent is your Block, you would probably have to add an NBT-tag (persistent data holder) and then create the new Item when the block gets destroyed...
  3. NBT-tag, what is this? And how would i add it
  4. Well, it only works for Items, forgot about that... but it's minecraft's way of injecting data into Items that should not be visible to the player directly (for example that's what's being used to store what is in a chest)
    Considering your problem, the best way that you could do this is to actually store the location of every block in a map and then check for that block...
  5. How do you store the location of an item in hand? My problem is that i have a custom recipe for a end stone that is named "Stone Maker" and when i place it, it creates stone above it, but when i place a normal end stone without any custom name, it does the same thing. I want to only the custom named end stone create a stone above it and not the regular end stone.
  6. Well, for that you can indeed use the PersistantDatatype API. there was a tutorial about this a while ago, so I'll just point out the basics:
    Code (Java):
    ItemStack stack = ...;
    ItemMeta meta = stack.getItemMeta();
    // getting the container
    final PersistantDataContainer dataContainer = meta.getPersistentDataContainer();
    dataContainer.set(NamespacedKey, type, value);
    The NamespacedKey will produce something like minecraft:stone, but instead of minecraft it will use your plugin-name. You create a new one by simply calling its constructor that takes an instance of your plugin and the name.
    Code (Java):
    NamespacedKey customKey = new NamespacedKey(<plugin instance>, <name>);
    There are several types, all can be found using the static fields in PersistantDataType. The value is the value you want to store. It has to match your type (for example, if you insert PersistantDataType.INTEGER, you will have to provide an int)
  7. could be wrong here but i think placing an item as a block makes it lose its item attributes, such as persistent data and meta. when a block gets placed, record the value from the item meta and add it to the block youre placing as metadata. this is not persistent however. persistent data for blocks do not exist, so if you needed this data to stay after restarts, youd need to save it yourself
    • Agree Agree x 1
  8. Ι found that to be true as well. I think that there are exceptions thou for blocks that are also a Tile Entity such as chests (because your can set the NBT-Tag for for example a chest). What I therefore wonder is if one could add such a Tile-Entity to a block that would normally not have one and how much of an effort that would be?
  9. dont think you could. besides, that creates more work (as well as an unncessary amount of entities, even if theyre tile entities) than just saving the data to a local file
  10. Okay, so on the BlockPlaceEvent you need to get the item in their hand, and if the item has a display name, that matches "Stone Maker" then you add the code that places the stone above it.
  11. Thank you! It works like a charm! I can't belive that something that simple made that much of a difference.

    Edit: I checked the whole meta not only if the item is named "Stone Maker"
    #11 C4LLM3P3T3R, Feb 2, 2020
    Last edited: Feb 2, 2020
  12. If you dont need it to be persistent between server restarts, you can try adding meta-data to the block placed.