Chest Storing/restoring

Discussion in 'Plugin Development' started by Double0negative, Aug 23, 2012.

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

    Double0negative

    Hi guys,

    So it seems I have a problem with chest storing/restoring. I am able to store and restore just fine except for 1 issue. When it restores, it only restores with 1 item per each stack

    example

    before
    [​IMG]

    after

    [​IMG]

    methods im using for storing and setting

    chest.getBlockInventory().setContents(itemstack[]);

    chest.getBlockInventory().getContents();
     
  2. So you just store the ItemStack[] arrat of getContents() ? You're only referencing it tough, you're not copying it.... so whatever happens to those items happens to your store items.

    Just try this, clone the array instead... I'm unsure if there's a better method but you can just do this:
    Code:
    ItemStack[] contents = inventory.getContents();
    ItemStack[] store = new ItemStack[contents.length];
    
    for(int i = 0; i < contents.length; i++)
    {
        if(contents[i] != null)
             store[i] = contents[i].clone();
    }
    EDIT: Ah yes, .clone() on the array calls clone on all of its elements, wasn't sure and got lazy to open the IDE or to google it xD
     
  3. Offline

    skore87

    No need to loop through it, you can call the clone method on the whole array. But java is annoying sometimes since everything is a reference.
     
  4. Offline

    Double0negative

    Tried cloning, same issue. heres my code, its in a lot of different parts so bare with me

    Code:
    QueueManager.getInstance().addChest(gameid, c, c.getBlockInventory().getContents().clone());
    
    QueueManager
    Code:
        private ArrayList<ChestData>cd = new ArrayList<ChestData>();
    
        public void addChest(int g, Chest c, ItemStack[] i){
            cd.add(new ChestData(g,i,c));
        }
    
    ChestData
    Code:
    public class ChestData {
    
        private int gameID;
        private ItemStack[] i;
        private Chest c;
        
        public ChestData(int id, ItemStack[] i, Chest c){
            gameID = id;
            this.i = i;
            this.c = c;
            
        }
        
        public int getGameID() {
            return gameID;
        }
        public void setGameID(int gameID) {
            this.gameID = gameID;
        }
        public ItemStack[] getI() {
            return i;
        }
    
        public Chest getC() {
            return c;
        }
    
        
        
    }
    
    rollback part
    Code:
                    for(ChestData c: cd.toArray(new ChestData[0])){
                        if(c.getGameID() == game.getID()){
                            c.getC().getBlockInventory().setContents(c.getI());
                            cd.remove(c);
                        }
                    }
    
     
  5. Offline

    skore87

    Just a little nit-picking, but your rollback part shouldn't initialize an array of zero since you're just going to destroy it and recreate it.

    Since cloning the ItemStack[] doesn't seem to work, maybe manually doing so would?
    Code:
        public ItemStack[] justAnIdea(ItemStack[] chestItems){
           
            ArrayList<ItemStack> aList = new ArrayList<ItemStack>();
           
           
            for (ItemStack i : chestItems){
                aList.add(new ItemStack(i));
            }
           
            return aList.toArray(new ItemStack[aList.size()]);
        }
       
        public ItemStack[] anotherSimilarIdea(ItemStack[] chestItems){
            ItemStack[] is = new ItemStack[chestItems.length];
            for (int i = 0; i < chestItems.length; i++){
                is[i] = new ItemStack(chestItems[i]);
            }
            return is;
        }
     
  6. Offline

    Double0negative

    Its a required part of teh toArray method.

    After some testing, ive found the problem is with the rollback part, the data makes it thru the save, but its something in the rollback. Thanks for the code tho :)
     
  7. So it's when you restore it ?
    Loop through the list/array when you're about to restore it into the chest and print each itemstack in console and see what amounts you get.

    If amount is corect it means that setContents() doesn't consider amounts.... you'll need to make a loop and use inventory.setItem(slot, item)
     
  8. Offline

    skore87

    I know what it is, what I was saying is that you're actually creating the array object twice, not once like you think you are since arrays cannot be resized. Basically, when you instantiate a new anonymous array object in that method, you're forcing the method to create another one whereas if you give it a size it doesn't have to and thus saves processing power and memory albeit small amounts.

    Oh... and it isn't actually required, it is an overload. It just saves you from casting the base Object type every time you want to use it.
     
  9. Offline

    Double0negative

    I know this, I missread what you said above.
    I tried this too, with similar results. Im not sure where the data is getting lost, but its getting lost somewhere along the way.
     
  10. You tried ? Debugging is not a matter of trying as it *always* yelds results...
    Just add this:
    Code:
    System.out.print("[debug] items:");
    
    for(ItemStack item : yourStoredItems)
    {
        System.out.print(" - " + item); // it should either print "Nx Material" or null - watch the numbers, those are the amounts...
    }
     
  11. Offline

    Double0negative

    Code:
     
    02:40:51 [INFO]  - ItemStack{GLASS x 0}
    02:40:52 [INFO]  - ItemStack{GLASS x 0}
    02:40:52 [INFO]  - ItemStack{GLASS x 0}
    02:40:52 [INFO]  - ItemStack{GLASS x 0}
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    02:40:52 [INFO]  - null
    
    4 stacks of glass originaly. However, running same loop after its been stored yeilds

    ItemStack{GLASS x 64}
    ItemStack{GLASS x 64}
    ItemStack{GLASS x 64}
    ItemStack{GLASS x 64}
     
  12. Hmm, so you're sure you're cloning the items ? Tried individual items as well ?

    I dunno... you could also try using new ItemStack(id, amount, data) instead of cloning, however that won't copy written book's contents, you can do that with the BookAPI later if the method works, no reason to do it now for testing.

    Still... do you restart server between saving and loading ? You might want to check your saving or loading methods...
     
  13. Offline

    Double0negative

    Managed to get it working by storing each item individually. Thanks for your help :)
     
Thread Status:
Not open for further replies.

Share This Page