Saving/Loading the contents of a chest. Using basic Configuration.

Discussion in 'Plugin Development' started by Orcem12, Apr 27, 2012.

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

    Orcem12

    I've tried many different things, used google and the dreaded Bukkit search so far I came up with this nasty looking this:
    Code:java
    1.  
    2. public boolean onCommand(CommandSender s, Command c, String sl, String[] args) {
    3. Player p = (Player)s;
    4. PermissionUser user = PermissionsEx.getUser(p);
    5. if(c.getName().equalsIgnoreCase("rhrestore") && user.has("Rehab.HGC.rhrestore")){
    6. int x = plugin.getConfig().getInt("Chest.Storage.Chest["+t+"].X");
    7. int y = plugin.getConfig().getInt("Chest.Storage.Chest["+t+"].Y");
    8. int z = plugin.getConfig().getInt("Chest.Storage.Chest["+t+"].Z");
    9. Block blockatlocation = Bukkit.getWorld(p.getWorld().getName()).getBlockAt(x,y,z);
    10. if (blockatlocation.getState() instanceof Chest) {
    11. Chest chest = (Chest) blockatlocation.getState();
    12. chest.getInventory().clear();
    13. ItemStack[] itemsinchest;
    14. Object o = plugin.getConfig().get("Chest.Storage.Chest["+t+"].Contents");
    15. if (o instanceof ItemStack[])
    16. itemsinchest = (ItemStack[]) o;
    17. else if (o instanceof List)
    18. itemsinchest = (ItemStack[]) ((List) o).toArray(new ItemStack[0]);
    19. else
    20. plugin.log.info("Exception!");
    21. chest.getInventory().setContents((ItemStack[]) o);
    22. }
    23. s.sendMessage(ChatColor.GOLD+"[RHG] "+ChatColor.DARK_GRAY+"chests restored!");
    24. }
    25. return false;
    26. }
    27. }
    28.  
    29.  


    The above code doesn't work!

    There has to be a simpler way of going about saving and loading chest contents from a regular old configuration file right? Thanks for the insight.
     
  2. Offline

    Tzeentchful

    I'm actually working on something similar. i know you can sterilize an item stack with
    PHP:
     ItemStack.serialize();
    I then put the sterilized output into a hashmap with the slot as an key and a sterilized item stack as the value.

    PHP:
    public Map<IntegerMap<StringObject>> sterilizeInv(Inventory inv){
     
                
    Map<IntegerMap<StringObject>> invOut = new HashMap<IntegerMap<StringObject>>();//hasmap with slot number as int and the sterlized itemstack as the value
                
    for(int i 0inv.getSize(); i++){//loop through all the slots in the inv
                    
    ItemStack is inv.getItem(i);//get the items stack at current slot
                    
    Map<StringObjectsis is.serialize();//sterlize
                    
    invOut.put(isis);//put into the hasmap
                
    }
                return 
    invOut;
            }
    i haven't tested this code yet btw.
    i then go and save the hashmap as a binary file
    PHP:
    public void saveMap(String filenameMap<IntegerMap<StringObject>> saves) {
                try {
                    
    FileOutputStream fos = new FileOutputStream(filename);
                    
    GZIPOutputStream gzos = new GZIPOutputStream(fos);
                    
    ObjectOutputStream out = new ObjectOutputStream(gzos);
                    
    out.writeObject(saves);
                    
    out.flush();
                    
    out.close();
                }
                catch (
    IOException e) {
                    
    System.out.println(e);
                }
              }
     
  3. Offline

    Orcem12

    @Tzeentchful
    Interesting, I did not know the itemstack could be serialized now. I think that will make things so much more easier. Thank you.

    Sadly the code given doesn't work. Does anyone have a better idea?
    @Tzeentchful
    I'll use that for saving a player inventory.

    I'm still very stumped... Does anyone have any idea on storing the chest contents then reloading them from a Yml File?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 25, 2016
  4. Offline

    Darq

Thread Status:
Not open for further replies.

Share This Page