Solved How can I give a player a shulker box containing items?

Discussion in 'Spigot Plugin Development' started by Rektb, Jul 16, 2018.

  1. I want to give a player a shulkerbox containing items, the problem is I can't find a way to do it by NBT.
    I am trying to make a plugin so players can open shulker boxes without placing them
     
    #1 Rektb, Jul 16, 2018
    Last edited: Jul 16, 2018
  2. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    • Agree Agree x 1
  3. Why nbt and crazy stuff? Check for the BlockPlaceEvent check if if the placed block is a shulker, check if it's your shulker (custom name or something else), get the ShulkerBox:
    Code (Text):
    ShulkerBox box = (ShulkerBox) block.getState();
    , get its inventory with .getInventory(); and add items to it using .addItem();
     
  4. The problem is I want to edit the shulker inventory without placing it, I mean, while a player is holding one
     
  5. Why?
     
  6. I am trying to make a plugin so players can open shulker boxes without placing them
     
  7. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Well I thought it would be obvious enough that I wouldn't have to explain it to the dot.

    If you can get a shulker box's inventory, you can set it.
    You can cast the item's meta to BlockStateMeta: https://hub.spigotmc.org/javadocs/spigot/org/bukkit/inventory/meta/BlockStateMeta.html
    Then get the block state, which you can cast to ShulkerBox, which you can use to getInventory(). You can modify that inventory, and it will apply to the item.

    EDIT: I believe if you open the inventory ingame modifications made to it will apply to the item as well.
     
  8. Yes but would this work without placing the shulkerbox?
     
  9. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Did you read my reply? I said ItemMeta at least once, which implies an item.
    So yes, this will only work for a Shulker Box ItemStack.
     
  10. Code (Text):

    ItemStack itemStack = new ItemStack(Material.BLACK_SHULKER_BOX);
    BlockStateMeta bsm = (BlockStateMeta) itemStack.getItemMeta();
    ShulkerBox box = (ShulkerBox) bsm.getBlockState();
    box.getInventory().setContents(e.getInventory().getContents());
    p.getInventory().remove(p.getInventory().getItemInMainHand());
    p.getInventory().setItem(p.getInventory().getHeldItemSlot(), itemStack);
     
    I did it but it still gives me an empty shulkerbox
     
  11. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Are you getting any errors?

    EDIT: Also, try using box.update() after editing the inventory.
     
  12. It doesn't work but I don't get any errors
     
  13. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Did you try using box.update()?
     
  14. Yes, I tried
    Code (Text):

    ItemStack itemStack = new ItemStack(Material.BLACK_SHULKER_BOX);
    BlockStateMeta bsm = (BlockStateMeta) itemStack.getItemMeta();
    ShulkerBox box = (ShulkerBox) bsm.getBlockState();
    box.getInventory().setContents(e.getInventory().getContents());
    box.update();
    p.getInventory().remove(p.getInventory().getItemInMainHand());
    p.getInventory().setItem(p.getInventory().getHeldItemSlot(), itemStack);
     
     
  15. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

  16. Still no luck :/
    Code (Text):

    ItemStack shulker = new ItemStack(Material.BLACK_SHULKER_BOX);
    BlockStateMeta meta = (BlockStateMeta) shulker.getItemMeta();
    ShulkerBox box = (ShulkerBox) meta.getBlockState();
    meta.setBlockState(box);
    box.getInventory().setContents(e.getInventory().getContents());
    box.update();
    p.getInventory().remove(p.getInventory().getItemInMainHand());
    p.getInventory().setItem(p.getInventory().getHeldItemSlot(), shulker);
     
     
  17. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    After setting the inventory contents.
    EDIT: Also set the meta back to the item again.
     
  18. I've done it like this
    Code (Text):

    ItemStack itemStack = new ItemStack(Material.BLACK_SHULKER_BOX);
    BlockStateMeta bsm = (BlockStateMeta) itemStack.getItemMeta();
    ShulkerBox box = (ShulkerBox) bsm.getBlockState();
    box.getInventory().setContents(e.getInventory().getContents());
    bsm.setBlockState(box);
    box.update();
    p.getInventory().remove(p.getInventory().getItemInMainHand());
    p.getInventory().setItem(p.getInventory().getHeldItemSlot(), itemStack);
     
    but still it gives me an empty shulkerbox
     
  19. ScarabCoder

    ScarabCoder Retired Resource Staff
    Retired

    Try placing it in the inventory before editing maybe? If that doesn't work, it may be a bug.