Private Chest

Discussion in 'Plugin Development' started by kyle7273, Apr 18, 2014.

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

    kyle7273

    How could I make it so that players can create a private chest using a command such as /storage to make it so a 9 item chest pops up?

    Thanks
     
  2. Offline

    Plo124

    kyle7273
    I wont give you a full code example because that will take a while to do, but basically you have to listen to the command /storage, then I would find a way to serialise an inventory into a string, like so (note this currently doesnt store things like enchantments, content in written books, renamed items, e.t.c), then open the inventory by reading it from the string, then once they close the inventory (theres an event for that), put the new inventory into the config.
     
  3. Offline

    NonameSL

    Code:java
    1. import java.util.Map;
    2. import java.util.Map.Entry;
    3.  
    4. import org.bukkit.Bukkit;
    5. import org.bukkit.Material;
    6. import org.bukkit.enchantments.Enchantment;
    7. import org.bukkit.inventory.Inventory;
    8. import org.bukkit.inventory.ItemStack;
    9.  
    10. public class InventoryStringDeSerializer {
    11. public static String InventoryToString (Inventory invInventory)
    12. {
    13. String serialization = invInventory.getSize() + ";";
    14. for (int i = 0; i < invInventory.getSize(); i++)
    15. {
    16. ItemStack is = invInventory.getItem(i);
    17. if (is != null)
    18. {
    19. String serializedItemStack = new String();
    20.  
    21. String isType = String.valueOf(is.getType().getId());
    22. serializedItemStack += "t@" + isType;
    23.  
    24. if (is.getDurability() != 0)
    25. {
    26. String isDurability = String.valueOf(is.getDurability());
    27. serializedItemStack += ":d@" + isDurability;
    28. }
    29.  
    30. if (is.getAmount() != 1)
    31. {
    32. String isAmount = String.valueOf(is.getAmount());
    33. serializedItemStack += ":a@" + isAmount;
    34. }
    35.  
    36. Map<Enchantment,Integer> isEnch = is.getEnchantments();
    37. if (isEnch.size() > 0)
    38. {
    39. for (Entry<Enchantment,Integer> ench : isEnch.entrySet())
    40. {
    41. serializedItemStack += ":e@" + ench.getKey().getId() + "@" + ench.getValue();
    42. }
    43. }
    44.  
    45. serialization += i + "#" + serializedItemStack + ";";
    46. }
    47. }
    48. return serialization;
    49. }
    50.  
    51. public static Inventory StringToInventory (String invString)
    52. {
    53. String[] serializedBlocks = invString.split(";");
    54. String invInfo = serializedBlocks[0];
    55. Inventory deserializedInventory = Bukkit.getServer().createInventory(null, Integer.valueOf(invInfo));
    56.  
    57. for (int i = 1; i < serializedBlocks.length; i++)
    58. {
    59. String[] serializedBlock = serializedBlocks[i].split("#");
    60. int stackPosition = Integer.valueOf(serializedBlock[0]);
    61.  
    62. if (stackPosition >= deserializedInventory.getSize())
    63. {
    64. continue;
    65. }
    66.  
    67. ItemStack is = null;
    68. Boolean createdItemStack = false;
    69.  
    70. String[] serializedItemStack = serializedBlock[1].split(":");
    71. for (String itemInfo : serializedItemStack)
    72. {
    73. String[] itemAttribute = itemInfo.split("@");
    74. if (itemAttribute[0].equals("t"))
    75. {
    76. is = new ItemStack(Material.getMaterial(Integer.valueOf(itemAttribute[1])));
    77. createdItemStack = true;
    78. }
    79. else if (itemAttribute[0].equals("d") && createdItemStack)
    80. {
    81. is.setDurability(Short.valueOf(itemAttribute[1]));
    82. }
    83. else if (itemAttribute[0].equals("a") && createdItemStack)
    84. {
    85. is.setAmount(Integer.valueOf(itemAttribute[1]));
    86. }
    87. else if (itemAttribute[0].equals("e") && createdItemStack)
    88. {
    89. is.addEnchantment(Enchantment.getById(Integer.valueOf(itemAttribute[1])), Integer.valueOf(itemAttribute[2]));
    90. }
    91. }
    92. deserializedInventory.setItem(stackPosition, is);
    93. }
    94.  
    95. return deserializedInventory;
    96. }
    97. }[/i]

    Code:java
    1.  
    2. HashMap<String, Inventory> playerChests = new HashMap<String, Inventory>();
    3. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    4. if(sender instanceof Player){
    5. Player p = (Player) sender;
    6. if(commandLabel.equalsIgnoreCase("storage"){
    7. if(args.length==0){
    8. if(playerChests.containsKey(p.getName())){
    9. p.openInventory(playerChests.get(p.getName()));
    10. }else{
    11. playerChests.put(p.getName(), createChest(p));
    12. p.openInventory(playerChests.get(p.getName()));
    13. }
    14. }
    15. }
    16. }
    17. }

    Code:java
    1.  
    2. public Inventory createChest(Player p){
    3. //Code for creating inventory with 9 spots here
    4. Inventory inv = Bukkit.getServer().createInventory(null, 9, p.getName());
    5. return inv;
    6. }

    Code:java
    1. public void onDisable(){
    2. List<String> list = new ArrayList<String>();
    3. for(Player p : playerChests.keySet()){
    4. list.add(p.getName()+":"+InventoryStringDeSerializer.InventoryToString(playerChests.get(p.getName())));
    5. }
    6. getConfig().set("chests",list);
    7. saveConfig();
    8. }
    9. public void onEnable(){
    10. saveDefaultConfig();
    11. List<String> list = getConfig().getStringList("chests");
    12. for(String str : list){
    13. playerChests.put(str.split(":")[0], InventoryStringDeSerializer.StringToInventory(str.split(":")[1]));
    14. }
    15. }
    16.  


    Well, it took me a while, but... Ta da.
     
  4. Offline

    kyle7273


    Thanks! I will try that!
     
  5. Offline

    NonameSL

    kyle7273 No prob, if theres an error tell me, it could be stupid because I didnt write this in Eclipse
     
Thread Status:
Not open for further replies.

Share This Page