EnderChests

Discussion in 'Plugin Development' started by PHILLIPS_71, Aug 16, 2013.

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

    PHILLIPS_71

    I'm wanting to change the Ender chest inventory slots to something smaller and change the name "Ender Chest" to something different was using a inventory open event but i could see nothing there that would change the slots and the name for it anyone have a idea how i could achieve this?
     
  2. Offline

    Niv200

    emm heres some code that may help you..
    Code:java
    1. @EventHandler
    2. public void onInteract(PlayerInteractEvent e){
    3. if(e.getClickedBlock().getType() == Material.ENDER_CHEST && e.getAction() == Action.RIGHT_CLICK_BLOCK ){
    4. e.setCancelled(true);
    5. invv = Bukkit.getServer().createInventory(null, 9,"Bank");
    6. e.getPlayer().openInventory(invv);
    7. // save and load the items every time player open his inventory..
    8. }
    9. }
     
    PHILLIPS_71 likes this.
  3. Offline

    PHILLIPS_71

    Niv200
    Ah Thanks for that i forgot to get the block type, but how would i save and load the items when they open/close the inventory?
     
  4. Offline

    xCyanide

    PHILLIPS_71
    You would save and load the inventory with a yml file I will show you how tomorrow morning unless someone else shows you how
     
  5. Offline

    PHILLIPS_71

    xCyanide
    Ah thanks for that much appreciated i'm not to good with config files.
     
  6. Offline

    xCyanide

    PHILLIPS_71
    This is the class that is allows you to serialize the inventory to string.
    Code:java
    1. import java.io.ByteArrayInputStream;
    2. import java.io.ByteArrayOutputStream;
    3. import java.io.DataInputStream;
    4. import java.io.DataOutputStream;
    5. import java.math.BigInteger;
    6.  
    7. import net.minecraft.server.v1_6_R2.NBTBase;
    8. import net.minecraft.server.v1_6_R2.NBTTagCompound;
    9. import net.minecraft.server.v1_6_R2.NBTTagList;
    10.  
    11. import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftInventoryCustom;
    12. import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack;
    13. import org.bukkit.inventory.Inventory;
    14. import org.bukkit.inventory.ItemStack;
    15. import org.bukkit.inventory.PlayerInventory;
    16.  
    17. public class InventoryStringDeSerializer {
    18.  
    19. public static Inventory getArmorInventory(PlayerInventory inventory) {
    20. ItemStack[] armor = inventory.getArmorContents();
    21. CraftInventoryCustom storage = new CraftInventoryCustom(null, armor.length);
    22.  
    23. for (int i = 0; i < armor.length; i++)
    24. storage.setItem(i, armor[i]);
    25.  
    26. return storage;
    27. }
    28.  
    29. public static Inventory getContentInventory(PlayerInventory inventory) {
    30. ItemStack[] content = inventory.getContents();
    31. CraftInventoryCustom storage = new CraftInventoryCustom(null, content.length);
    32.  
    33. for (int i = 0; i < content.length; i++)
    34. storage.setItem(i, content[i]);
    35.  
    36. return storage;
    37. }
    38.  
    39. public static String toBase64(Inventory inventory) {
    40. DataOutputStream dataOutput = new DataOutputStream(outputStream);
    41. NBTTagList itemList = new NBTTagList();
    42. for (int i = 0; i < inventory.getSize(); i++) {
    43. NBTTagCompound outputObject = new NBTTagCompound();
    44. net.minecraft.server.v1_6_R2.ItemStack craft = getCraftVersion(inventory.getItem(i));
    45. if (craft != null)
    46. craft.save(outputObject);
    47. itemList.add(outputObject);
    48. }
    49. NBTBase.a(itemList, dataOutput);
    50. return new BigInteger(1, outputStream.toByteArray()).toString(32);
    51. }
    52.  
    53. public static Inventory fromBase64(String data) {
    54. ByteArrayInputStream inputStream = new ByteArrayInputStream(new BigInteger(data, 32).toByteArray());
    55. NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream), 0);
    56. Inventory inventory = new CraftInventoryCustom(null, itemList.size());
    57.  
    58. for (int i = 0; i < itemList.size(); i++) {
    59. NBTTagCompound inputObject = (NBTTagCompound) itemList.get(i);
    60. if (!inputObject.isEmpty()) {
    61. inventory.setItem(i, CraftItemStack.asBukkitCopy(net.minecraft.server.v1_6_R2.ItemStack.createStack(inputObject)));
    62. }
    63. }
    64. return inventory;
    65. }
    66.  
    67. private static net.minecraft.server.v1_6_R2.ItemStack getCraftVersion(ItemStack stack) {
    68. if (stack != null)
    69. return CraftItemStack.asNMSCopy(stack);
    70.  
    71. return null;
    72. }
    73. }[/i][/i]


    Example use:
    Code:java
    1. @EventHandler
    2. public void onClick(PlayerInteractEvent event) {
    3. Player p = event.getPlayer();
    4. if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    5. if (event.getClickedBlock().getType().equals(Material.ENDER_CHEST)) {
    6. event.setCancelled(true);
    7. if (p.hasPermission("first.chest") && !p.isOp()) {
    8. if(conf.getString(p.getName() + ".content") != null) {
    9. Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")");
    10. Inventory i = InventoryStringDeSerializer.fromBase64(conf.getString(p.getName() + ".content"));
    11. yourchest.setContents(i.getContents());
    12. p.openInventory(yourchest);
    13. } else if(conf.getString(p.getName() + ".content") == null) {
    14. Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")");
    15. conf.set(p.getName() + ".content", InventoryStringDeSerializer.toBase64(First));
    16. p.openInventory(yourchest);
    17.  
    18. }
    19. }
    20. }
    21. }
    22. }
     
    PHILLIPS_71 likes this.
  7. Offline

    Quantix


    You're assuming the event has a block, which it very well might not. I'd first check if it has one:
    Code:java
    1. public void onInteract(PlayerInteractEvent e){
    2. if (event.hasBlock()) {
    3. //Do stuff
    4. }
    5.  
    6. OR
    7.  
    8. if (event.getClickedBlock() != null) {
    9. //Do stuff
    10. }
    11. }
     
  8. Offline

    Niv200

    ok, :) good to know :p will edit my plugin :)
     
  9. Offline

    PHILLIPS_71

    xCyanide
    Thanks for the help i really appreciate it! but how do i set the cconfig up in y onEnable and when i create the config.yml file in eclipse do i need to add any text to it?
     
  10. Offline

    xTrollxDudex

    PHILLIPS_71
    saveDefaultConfig(). You don't need to add anything to it unless you want text to appear when getConfig().options().copyDefaults(true) is called
     
  11. Offline

    xCyanide

    PHILLIPS_71
    You can do this
    Code:java
    1. File configFile = new File(this.getDataFolder(), "config.yml");
    2.  
    3. if(!configFile.exists()) {
    4. saveDefaultConfig();
    5. }
     
  12. Offline

    PHILLIPS_71

    xCyanide
    When i right click on the enderchest it does nothing,

    here is the code of the enderchest its the same but i had to change

    Code:
    getConfig().set(p.getName() + ".content", InventoryStringDeSerializer.toBase64(First));
    to

    Code:
    getConfig().set(p.getName() + ".content", InventoryStringDeSerializer.toBase64(yourchest));
    here is the whole class:

    Code:
    public class BankChests extends JavaPlugin implements Listener {
     
        @EventHandler
        public void onClick(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (event.getClickedBlock().getType().equals(Material.ENDER_CHEST)) {
                    event.setCancelled(true);
                    if (p.hasPermission("first.chest") && !p.isOp()) {
                        if(getConfig().getString(p.getName() + ".content") != null) {
                            Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")");
                            Inventory i = InventoryStringDeSerializer.fromBase64(getConfig().getString(p.getName() + ".content"));
                            yourchest.setContents(i.getContents());
                            p.openInventory(yourchest);
                        } else if(getConfig().getString(p.getName() + ".content") == null) {
                            Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")");
                            getConfig().set(p.getName() + ".content", InventoryStringDeSerializer.toBase64(yourchest));
                            p.openInventory(yourchest);
     
                        }
                    }
                }
            }
        }
    }
     
  13. Offline

    xCyanide

    PHILLIPS_71
    Yeah, I forgot to change that :p, did you register the event?
     
  14. Offline

    PHILLIPS_71

    xCyanide
    Yes i have for the onClick, but how would i do that for the InventoryStringDeSerializer
     
  15. Offline

    xCyanide

    PHILLIPS_71
    Are there any errors and you don't need to register InventoryStringDeSerializer. Also, add some debug messages
     
  16. Offline

    PHILLIPS_71

    xCyanide
    i did system.out.Print(Test") in the InventoryStringDeSerializer and i got no messages in the colsole the onClick is working it is generating a config file but the ender chest wont even open.
     
  17. Offline

    xCyanide

    PHILLIPS_71
    Oh I am an idiot, just remove && !p.isOp(); I made the same plugin you wanted to make a while ago :p
     
  18. Offline

    PHILLIPS_71

    xCyanide
    Ah that seemd to fix it now im getting a error on

    Code:
    if(getConfig().getString(p.getName() + ".content") != null) {
     
     
    if(getConfig().getString(p.getName() + ".content") != null) {
    Its says file cannot be null.
     
  19. Offline

    xCyanide

  20. Offline

    PHILLIPS_71

    xCyanide

    My main class is about 400 lines...

    but here is the onClick event and here is the InventoryStringDeSerializer class

    onClick:

    Code:
        @EventHandler
        public void onClick(PlayerInteractEvent event) {
            Player p = event.getPlayer();
            if (event.getAction() == Action.RIGHT_CLICK_BLOCK) {
                if (event.getClickedBlock().getType().equals(Material.ENDER_CHEST)) {
                    event.setCancelled(true);
                    if(getConfig().getString(p.getName() + ".content") != null) {
                        Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")");
                        Inventory i = InventoryStringDeSerializer.fromBase64(getConfig().getString(p.getName() + ".content"));
                        yourchest.setContents(i.getContents());
                        p.openInventory(yourchest);
                    } else if(getConfig().getString(p.getName() + ".content") == null) {
                        Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")");
                        getConfig().set(p.getName() + ".content", InventoryStringDeSerializer.toBase64(yourchest));
                        p.openInventory(yourchest);
     
                    }
                }
            }
        }
    }

    InventoryStringDeSerializer:

    Code:
    public class InventoryStringDeSerializer {
     
        public static Inventory getArmorInventory(PlayerInventory inventory) {
            ItemStack[] armor = inventory.getArmorContents();
            CraftInventoryCustom storage = new CraftInventoryCustom(null, armor.length);
     
            for (int i = 0; i < armor.length; i++)
                storage.setItem(i, armor[i]);
            return storage;
        }
     
        public static Inventory getContentInventory(PlayerInventory inventory) {
            ItemStack[] content = inventory.getContents();
            CraftInventoryCustom storage = new CraftInventoryCustom(null, content.length);
     
            for (int i = 0; i < content.length; i++)
                storage.setItem(i, content[i]);
     
            return storage;
        }
     
        public static String toBase64(Inventory inventory) {
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
            DataOutputStream dataOutput = new DataOutputStream(outputStream);
            NBTTagList itemList = new NBTTagList();
            for (int i = 0; i < inventory.getSize(); i++) {
                NBTTagCompound outputObject = new NBTTagCompound();
                net.minecraft.server.v1_6_R2.ItemStack craft = getCraftVersion(inventory.getItem(i));
                if (craft != null)
                    craft.save(outputObject);
                itemList.add(outputObject);
            }
            NBTBase.a(itemList, dataOutput);
            return new BigInteger(1, outputStream.toByteArray()).toString(32);
        }
     
        public static Inventory fromBase64(String data) {
            ByteArrayInputStream inputStream = new ByteArrayInputStream(new BigInteger(data, 32).toByteArray());
            NBTTagList itemList = (NBTTagList) NBTBase.b(new DataInputStream(inputStream), 0);
            Inventory inventory = new CraftInventoryCustom(null, itemList.size());
     
            for (int i = 0; i < itemList.size(); i++) {
                NBTTagCompound inputObject = (NBTTagCompound) itemList.get(i);
                if (!inputObject.isEmpty()) {
                    inventory.setItem(i, CraftItemStack.asBukkitCopy(net.minecraft.server.v1_6_R2.ItemStack.createStack(inputObject)));
                }
            }
            return inventory;
        }
     
        private static net.minecraft.server.v1_6_R2.ItemStack getCraftVersion(ItemStack stack) {
            if (stack != null)
                return CraftItemStack.asNMSCopy(stack);
     
            return null;
        }
    }
    
     
  21. Offline

    xCyanide

    PHILLIPS_71
    Is your config.yml generating? and can you show me the stack trace/error?
     
  22. Offline

    PHILLIPS_71

    xCyanide
    It is making the config but there is nothing in it, here is the error

    Code:
    >11:47:44 [SEVERE] Could not pass event PlayerInteractEvent to SCPvE v1.0
    org.bukkit.event.EventException
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:427)
            at org.bukkit.plugin.RegisteredListener.callEvent(RegisteredListener.jav
    a:62)
            at org.bukkit.plugin.SimplePluginManager.fireEvent(SimplePluginManager.j
    ava:477)
            at org.bukkit.plugin.SimplePluginManager.callEvent(SimplePluginManager.j
    ava:462)
            at org.bukkit.craftbukkit.v1_6_R2.event.CraftEventFactory.callPlayerInte
    ractEvent(CraftEventFactory.java:192)
            at net.minecraft.server.v1_6_R2.PlayerInteractManager.interact(PlayerInt
    eractManager.java:373)
            at net.minecraft.server.v1_6_R2.PlayerConnection.a(PlayerConnection.java
    :628)
            at net.minecraft.server.v1_6_R2.Packet15Place.handle(SourceFile:58)
            at net.minecraft.server.v1_6_R2.NetworkManager.b(NetworkManager.java:296
    )
            at net.minecraft.server.v1_6_R2.PlayerConnection.e(PlayerConnection.java
    :116)
            at net.minecraft.server.v1_6_R2.ServerConnection.b(SourceFile:37)
            at net.minecraft.server.v1_6_R2.DedicatedServerConnection.b(SourceFile:3
    0)
            at net.minecraft.server.v1_6_R2.MinecraftServer.t(MinecraftServer.java:5
    90)
            at net.minecraft.server.v1_6_R2.DedicatedServer.t(DedicatedServer.java:2
    26)
            at net.minecraft.server.v1_6_R2.MinecraftServer.s(MinecraftServer.java:4
    86)
            at net.minecraft.server.v1_6_R2.MinecraftServer.run(MinecraftServer.java
    :419)
            at net.minecraft.server.v1_6_R2.ThreadServerApplication.run(SourceFile:5
    82)
    Caused by: java.lang.IllegalArgumentException: File cannot be null
            at org.apache.commons.lang.Validate.notNull(Validate.java:203)
            at org.bukkit.configuration.file.YamlConfiguration.loadConfiguration(Yam
    lConfiguration.java:170)
            at org.bukkit.plugin.java.JavaPlugin.reloadConfig(JavaPlugin.java:117)
            at org.bukkit.plugin.java.JavaPlugin.getConfig(JavaPlugin.java:111)
            at Shops.BankChests.onClick(BankChests.java:22)
            at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
            at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
            at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
            at java.lang.reflect.Method.invoke(Unknown Source)
            at org.bukkit.plugin.java.JavaPluginLoader$1.execute(JavaPluginLoader.ja
    va:425)
            ... 16 more
    xCyanide
    same error saying file cannot be null.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 3, 2016
Thread Status:
Not open for further replies.

Share This Page