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?
emm heres some code that may help you.. Code:java @EventHandler public void onInteract(PlayerInteractEvent e){ if(e.getClickedBlock().getType() == Material.ENDER_CHEST && e.getAction() == Action.RIGHT_CLICK_BLOCK ){ e.setCancelled(true); invv = Bukkit.getServer().createInventory(null, 9,"Bank"); e.getPlayer().openInventory(invv);// save and load the items every time player open his inventory.. }}
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?
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
PHILLIPS_71 This is the class that is allows you to serialize the inventory to string. Code:java import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import java.io.DataInputStream;import java.io.DataOutputStream;import java.math.BigInteger; import net.minecraft.server.v1_6_R2.NBTBase;import net.minecraft.server.v1_6_R2.NBTTagCompound;import net.minecraft.server.v1_6_R2.NBTTagList; import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftInventoryCustom;import org.bukkit.craftbukkit.v1_6_R2.inventory.CraftItemStack;import org.bukkit.inventory.Inventory;import org.bukkit.inventory.ItemStack;import org.bukkit.inventory.PlayerInventory; 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; }}[/i][/i] Example use: Code:java @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(conf.getString(p.getName() + ".content") != null) { Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")"); Inventory i = InventoryStringDeSerializer.fromBase64(conf.getString(p.getName() + ".content")); yourchest.setContents(i.getContents()); p.openInventory(yourchest); } else if(conf.getString(p.getName() + ".content") == null) { Inventory yourchest = Bukkit.createInventory(p, 36, ChatColor.BLACK + "Ender Chest " + "(" + p.getName() + ")"); conf.set(p.getName() + ".content", InventoryStringDeSerializer.toBase64(First)); p.openInventory(yourchest); } } } } }
You're assuming the event has a block, which it very well might not. I'd first check if it has one: Code:java public void onInteract(PlayerInteractEvent e){ if (event.hasBlock()) { //Do stuff } OR if (event.getClickedBlock() != null) { //Do stuff }}
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?
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
PHILLIPS_71 You can do this Code:java File configFile = new File(this.getDataFolder(), "config.yml"); if(!configFile.exists()) { saveDefaultConfig(); }
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); } } } } } }
PHILLIPS_71 Are there any errors and you don't need to register InventoryStringDeSerializer. Also, add some debug messages
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.
PHILLIPS_71 Oh I am an idiot, just remove && !p.isOp(); I made the same plugin you wanted to make a while ago
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.
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; } }
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.