Creating a chest with custom inventory name?

Discussion in 'Plugin Development' started by lzravanger, May 18, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    lzravanger

    I know it's possible now to change the inventory name of a chest. I am able to place down a chest with a custom name, but I want it to disappear and reappear with the same name and it will not. I set the block's id to 0 to remove it and then I can't turn it back into a chest with a custom name. Any advice?
     
    greaperc4 likes this.
  2. Offline

    Technius

    Change the display name of the dropped item to the chest's name.
     
  3. Offline

    lzravanger

    It's a block, not an item.
     
  4. Offline

    Jogy34

    For one of my plugins I wanted to do this and I took 2 or 3 hours to figure it out but unfortunately there is no easy way to do it. You have to use reflection with cratbukkit code in order to get the nms TileEntity then from there it's just finding and calling the right method. In order for this to work you have to be using the Craftbukkit.jar not the Bukkit.jar since the plain Bukkit.jar only has the API in it. That also means that your plugin will break on pretty much every new version of bukkit that comes out since they implemented dynamically changing package names.

    Anyways here's the code:
    Code:
    CraftChest chest = (CraftChest) block.getState(); //block has to be a chest
            
    try
    {
        Field inventoryField = chest.getClass().getDeclaredField("chest"); //This get's the CraftChest variable 'chest' which is the TileEntityChest that is stored within it
        inventoryField.setAccessible(true); //Allows you to access that field since it's declared as private
        TileEntityChest teChest = ((TileEntityChest) inventoryField.get(chest)); //obtains the field and casts it to a TileEntityChest
        teChest.a("Name Goes Here"); //The a(String) method sets the title of the chest
    }
    catch (Exception e) //This has to be here as the getDeclaredField(String) throws an exception if the input doesn't exist in the given class
    {
         e.printStackTrace();
    }
    
    hope that helps.
     
  5. Offline

    lzravanger

    I don't care about the dynamic name as long as it works. This saves a lot of effort with saving the chests in a file for restarts. Now I can just use the names, thanks a lot.
     
  6. Jogy34
    How would this be done from a "PlayerInteractEntityEvent" Eventhandler?
     
  7. Offline

    Jogy34

    This renames chests, chests aren't entities (they're tile entities which are different than normal entities). If you somehow get a chest block from that event then you would just use that in place of the block in the code chunk I posted above.

    Also, you shouldn't revive dead posts. It can get really annoying.
     
  8. Ok thanks. And sorry, but I need to know what I need to know, so...
     
  9. Offline

    Jogy34


    You could have just made a new thread or PM'd me
     
Thread Status:
Not open for further replies.

Share This Page