Solved Setting the name of an EnderChest

Discussion in 'Plugin Development' started by mazentheamazin, Feb 4, 2014.

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

    mazentheamazin

    Hey,

    I am currently wondering something so I decided to ask here, my question is stated at the top and this is what I mean.
    Code:java
    1. /* I am currently getting the players enderchest as an inventory and opening the inventory for them. */
    2. player.closeInventory();
    3. Inventory ec = player.getEnderChest();
    4. //Now I want to set the name of the enderchest
    5. player.openInventory(ec);
    6.  


    Any help is appreciated..
    Thanks,
    Mazen
     
  2. Offline

    Jogy34

    The ender chest inventories are stored in the players themselves. They are InventorySubcontainers. They contain a variable for their name but I don't know if that actually has something to do with what shows up as the actual inventory title. What you could do is write up some reflection to change that to see what happens (let me know if you have no idea how to do that) or you could create your own custom inventory for each player and either replace every player's ender chest inventory with that or sync it up to the player's ender chest inventory. The second option would be a little harder but it would allow you to avoid having to store the inventories yourself and removing or adding your plugin wouldn't have any affect on player's ender chest inventory.
     
  3. Offline

    mazentheamazin

    Jogy34 Yes, I have no idea how to do that. Please explain and thanks for responding ;D
     
  4. Offline

    Jogy34

    Which one, the custom inventory or using reflection?
     
  5. Offline

    xTrollxDudex

    Jogy34
    Correct!
    PHP:
            this.playerConnection.sendPacket(new PacketPlayOutOpenWindow(this.containerCounter0iinventory.getInventoryName(), iinventory.getSize(), iinventory.k_()));
    get inventory name is the a String in InventorySubcontainer, set by the first parameter in the constructor.
     
  6. Offline

    mazentheamazin

    Jogy34 using reflection
    xTrollxDudex Can you explain more about what that code does? (and the playerConnection method)
     
  7. Offline

    xTrollxDudex

    mazentheamazin
    Actuall, that's just a bit off the minecraft server source. I was confiming that the InventoryEnderChest's name actually does affect the displayed inventory.

    Basically, you want to get field off EntityHuman:
    PHP:
    Field a EntityHuman.class.getDeclaredField("enderChest");
    Set accessible:
    PHP:
    a.setAccessible(true);
    Then get it:
    PHP:
    InventoryEnderChest chest = (InventoryEnderChesta.get((EntityHuman) ((CraftPlayerplayer).getHandle());
    Now, we edit the name:
    PHP:
    Field b chest.getSuperclass().getDeclaredField("a");
    Whoops, its private, what do we do?
    PHP:
    b.setAccessible(true);
    Now we set it:
    PHP:
    b.set("NEW NAME");
    Done.
     
  8. Offline

    Jogy34


    Or... you know, just player.getEnderChest();
     
  9. Offline

    xTrollxDudex

    Jogy34
    I'm too compicated for that :p
     
  10. Offline

    mazentheamazin

    Jogy34 xTrollxDudex
    Uh, this is my updated Code:
    Code:java
    1. Field a = EntityHuman.class.getDeclaredField("enderChest");
    2. InventoryEnderChest chest = (InventoryEnderChest) player.getInventory();
    3. Field b = chest.getClass().getDeclaredField("a");
    4. b.setAccessible(true);
    5. b.set("you mad bro?");

    b.set needs 2 objects, and what variable do I call when I do player.openInventory()?
     
  11. Offline

    xTrollxDudex

    mazentheamazin
    You use (chest, "you mad bro?"). Cast chest to InventorySubcontainer if you'd like. But the fact is, you're doung it really wrong, I suggest fixing all the errors with my code and put t in.
     
  12. Offline

    Jogy34

    Code:java
    1.  
    2. try
    3. {
    4. InventoryEnderChest enderChest = player.getEnderChest(); //Gets the ender chest inventory for the player
    5. Field chestNameField = InventorySubcontainer.class.getDeclaredField("a"); //Gets the 'a' field in the InventorySubcontainer class which corresponds in this instance to the name
    6. chestNameField.setAccessable(true); //It's private so you have to set it so it can be accessed as if it were public
    7. chestNameField.set(enderChest, "Name"); //Sets the variable to whatever in the enderChest object
    8. }
    9. catch(Exception e) {e.printStackTrace();) //In case you attempt to retrieve a field that doesn't actually exist
    10.  


    Also, xTrollxDudex, why do you use php instead of java syntax highlighting when writing java code?
     
  13. Offline

    xTrollxDudex

    Jogy34
    I don't feel like bothering these two rigt now, but desht and Henzz were being a bad influence on me, plus, I like the colors and the contrasts makes it easier to see.
     
  14. Offline

    mazentheamazin

    Jogy34
    Now what variable do I call when I open the players inventory?
     
  15. Offline

    Jogy34

    If you're talking about the actual player inventory with the player's inventory in the bottom with the player character in the top with the armor and the crafting 2x2, I don't think that is actually possible because as far as I know that is handled client side.
     
  16. Offline

    xTrollxDudex

    mazentheamazin
    The regular .getEnderChest(). You have fundamentally set the player's ender chest on the level of NMS, let the Bukkit API handle the rest for you.

    Hopefully you are opening an ender chest right?
     
  17. Offline

    mazentheamazin

Thread Status:
Not open for further replies.

Share This Page