Solved How can I set the title of a chest when placed by a player?

Discussion in 'Plugin Development' started by RyanTheLeach, Nov 17, 2013.

  1. Offline

    RyanTheLeach

    How can I set the title of a chest, dispenser etc when being placed by a player?

    I've looked through the bukkit API and tickets and it seems that this functionality is only available when *creating* an inventory, as there seems to be no real way of assigning an inventory to a InventoryHolder, Is there some other way I can set the name?
     
  2. You probably have to use the CraftBukkit.jar file for this, not the Bukkit.jar. I only found this thread, it might help: https://forums.bukkit.org/threads/creating-a-chest-with-custom-inventory-name.147966/

    I was just guessing that you already knew, just in case you don't, this would be the event you'd have to call.
    Code:java
    1. @EventHandler
    2. public void onBlockPlace(BlockPlaceEvent evt) {
    3.  
    4. if(evt.getBlock() instanceof CraftChest) {
    5. CraftChest block = (CraftChest) evt.getBlock();
    6. //everything else
    7. }
    8. }
     
  3. Offline

    RyanTheLeach

    I got that far but was stumped by the lack of API, I want to avoid reflection or relying on NMS if at all possible.
     
  4. RyanTheLeach if it was set through code, you would have to do it that hard way. if it is placed by a player, you can listen for a BlockPlaceEvent or whatever it is (for player), get the item in the hand of that player, set the name of that item. due to events being fired just a tick BEFORE the final action (such as the block actually being placed in the world) actually happens, setting the name of the ITEM that the player right-clicked with will actually change the name of the placed chest.
     
  5. Offline

    RyanTheLeach

    I've tried that with the help of #bukkitdev maybe you can see what could be wrong?
    http://pastebin.com/1cV2imXt
     
  6. What's wrong with your current code is that with ItemMeta you have to set it back to the item after you edit it. So you have to do e.getItemInHand().setItemMeta(meta); after changing the display name.
     
  7. Offline

    RyanTheLeach

    Cheers that helped, now my problem is that it won't rename it back to the old name, I suspect that it's happening because it only gives me a temporary copy of the item in hand, instead of a reference to the item in hand, so when I schedule the task, I can't be sure that the copy is still relevant.

    My only problem is how can I work around this? If I just get the current item in the players hand on the delayed task, I run the risk of the player swapping their items out and it renames the wrong item.

    http://pastebin.com/ZJzyYDy3

    Cheers the problem ended up being that the reference to the itemstack held by the player was no longer relevant, I just guess and hope that the player hasn't changed the held item when they place the chest, and use player.getHeldItem and it works, thanks for the help.
     

Share This Page