Solved Rename a chest?

Discussion in 'Plugin Development' started by thepaperboy99, Dec 14, 2013.

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

    thepaperboy99

    Hello bukkit, I just wanted to know how to rename a chest. I remover a while back that there was a method for this, but I lost the code. Does a method like that still exist? Or do you have to use NMS or something? Thanks.
     
  2. Offline

    DoctorDark

    thepaperboy99

    If you create a custom chest you are able to do that:

    Code:java
    1. Inventory inv = Bukkit.createInventory(p, 27, ChatColor.RED + "Your Chest Name Goes Here");


    27 would be replaced with the amount of slots you want the custom chest to have, 9 slots per line (3 lines = 27).
     
  3. Offline

    Jogy34

    You have to (and have always had to) use NMS and reflection for this:
    Code:java
    1.  
    2. public static void setChestName(Location loc, String name)
    3. {
    4. try
    5. {
    6. loc.getBlock().setType(Material.CHEST);
    7.  
    8. Field inventoryField = CraftChest.class.getDeclaredField("chest");
    9. inventoryField.setAccessible(true);
    10. TileEntityChest teChest = ((TileEntityChest) inventoryField.get((CraftChest) loc.getBlock().getState()));
    11. teChest.a(name);
    12. }
    13. catch (Exception e)
    14. {
    15. e.printStackTrace();
    16. }
    17. }
     
  4. Offline

    thepaperboy99

    You always had to do it that way? I could of swore there was a method built in. But anyways thanks!
     
  5. Offline

    Jogy34


    I figured this method out shortly after it was possible to get chests with different names by using an anvil. From my searching around then, there wasn't any API implementation of it. It is possible though that since then a method was added to the API but this works fine for me and I use a lot more NMS stuff where I need this so I haven't looked.
     
  6. Jogy34
    How would you get the name of the chest. For example, I'm using "death chests" in a plugin and I want everyone to be able to access the chest except for the person who died. So, I need to check the name of the chest and cancel the interact event if the name matches that of the person who's opening the chest.
     
  7. Offline

    AoH_Ruthless

    The Gaming Grunts
    I can't think of an exact code right now, but check the player who died, get their name, and set the chest name to the dead player's name.

    In a playerinteract event, listen for if players open a chest with the name of the dead player. If their names are equal, cancel the event.
     
  8. AoH_Ruthless
    I know what to do I just don't know how to do it :p I already have the code for the PlayerDeathEvent working perfectly, just not for the interact event
     
  9. Offline

    Jogy34


    For that you should be able to go through the bukkit API. Try getting the chest, getting the inventory from the chest, and then checking the title of the inventory. If that doesn't work then I can write up a quick method to retrieve the name using reflection as I can't find any method to retrieve the name.
     
  10. Offline

    thepaperboy99

    I was looking through the craftbukkit code and just by using Jogy34 's code, you can use the getInventoryName() method to get the chests name, but you may want to test that cause I'm not completely sure that would work.

    Code:java
    1.  
    2. public static String getChestName(Location loc)
    3. {
    4. try
    5. {
    6.  
    7.  
    8. Field inventoryField = CraftChest.class.getDeclaredField("chest");
    9. inventoryField.setAccessible(true);
    10. TileEntityChest teChest = ((TileEntityChest) inventoryField.get((CraftChest) loc.getBlock().getState()));
    11. return teChest.getInventoryName();
    12. }
    13. catch (Exception e)
    14. {
    15. e.printStackTrace();
    16. }
     
  11. Offline

    Jogy34

    Wow, I completely overlooked that.
     
Thread Status:
Not open for further replies.

Share This Page