Block broken -> ItemStack?

Discussion in 'Spigot Plugin Development' started by 24763, May 27, 2016.

  1. Hello,
    So i have a custom item which is given by command containing a name lore and some various NBT data for storing player name. I wondered how on break event i could convert that block back to an itemstack so i can give it to the player, this is because the item i give the player is a chest which is placed down.

    Could anyone help me if and how this is possible?
     
  2. Store location of places block with the player in the stored with the block then check on break if the block is located at specified location and then give the player the item with the nbt.

    (Might be a more efficient way, this was just my first thought)
     
  3. I did not understand fully sorry :/ I do store the coordinates of the placed chests under the players uuid in config, i would just like to know on break how i can get the itemstack of the block as i wish to set that to air and give them the item in their inventory on break
     
  4. Choco

    Moderator

    When they break a block, reference the config to check if it's one of the blocks that were stored.
    It's actually better if you cache information about placed blocks locally in a List or a Set or something along those lines. Perhaps even a HashMap<UUID, Location>. Then you won't have to reference the configuration file every time someone places down a block.

    When the servers shuts down, save the HashMap to the config.yml, and upon enabling the plugin, load config information to the HashMap. That will probably save you a bit of processing power
     
    • Agree Agree x 1
  5. Create a new itemstack of the broken block and add what you need to add to it then set the block they broke to air without dropping it then you just have to give them the itemstack. I also recommend @2008Choco's way of storing data, seems way more efficient and takes less space in the file (if using flat file)
     
    • Like Like x 1
  6. Thanks for the response, i will take this into account after but im trying to get the framewokrk setup before then optimising it :p
     
    • Like Like x 1
  7. Im asking how :/ I have tried various methods and got the following:
    Type mismatch: cannot convert from Block to ItemStack

    Not asking for spoonfeed but more of an explanation, you have pretty much told me what i did say.
     
  8. Choco

    Moderator

    new ItemStack(block.getType());?
     
  9. My placed block contains custom data :p such as name, lore, nbt data etc thats why
     
  10. Choco

    Moderator

    OH okay that makes a bit more sense why you're struggling. So assuming you have something such as a chest, you want to be able to give them a chest with NBT data containing the items in the chest? If that's the case, I know it's supported in vanilla, but I'm not sure it's supported with the API. You may have to use NMS to modify the NBT tags directly. I could be mistaken though
     
  11. Yep lol :p And im using an API here somewhere on Spigot for NBT tags, but requires a itemstack to check off :p All i need is converting the placed block to an itemstack and then im fine for the rest :)

    Example:
    Chest:
    name: StarterChest
    lore: place me
    nbttag: "md_5" (its the players name)
     
  12. Can't you just give them a new version of the chest, like store the type of the placed block as well and then give it to the block breaker with the nbttags added?
     
  13. I do store the chest id within nbt using an API however it requires an itemstack to find the tag :p
     
  14. Err nope it only gives me a chest :( with no name etc