Inventory changes, how to get what items have changed?

Discussion in 'Plugin Development' started by blackwolf12333, Dec 7, 2012.

Thread Status:
Not open for further replies.
  1. I wrote a listener to get what items have changed in an inventory after it closes, so i use the InventoryOpen and CloseEvent to get the two inventories i want to compare.
    Than i try to compare the two inventories, but the stupid thing is that the two inventories are the same, and i don't know why:(
    I put the code in pastebin: http://pastebin.com/GSuNrTpx it did screw up the layout a bit:(.
    I have no idea why the InventoryView in the hashmap is the same as the InventoryView from the InventoryCloseEvent.
    I hope you guys can help me,
    greetz blackwolf12333
     
  2. Offline

    Chuckleluck

    Java passes objects by reference, so the inventory added in the hashmap is the same as the inventory in the event. Try changing line 31 to this:
    Code:
    inventories.put(event.getPlayer().getName(), event.getView().clone());
     
  3. There is not method clone in InventoryView, so that doesn't work:(
     
  4. Offline

    Chuckleluck

    You could try this workaround:
    In your onInventoryOpen() method, put this:
    Code:
    InventoryView inv = new SpecialInventoryView(event);
    inventories.put(inv);
    And somewhere add this class:
    Code:
    class SpecialInventoryView extends InventoryView {
           
            private InventoryOpenEvent event;
           
            // gets the event for future reference
            public SpecialInventoryView(InventoryOpenEvent event) {
                this.event = event;
            }
           
            public Player getPlayer() {
               
                if(!(event.getPlayer() instanceof Player)) {
                    return null;
                }
                return (Player)event.getPlayer();
            }
           
            public InventoryType getType() {
                return event.getView().getType();
            }
           
            // creates a new top inventory, then fills it with the content of the real top inventory
            public Inventory getTopInventory() {
                Inventory top = Bukkit.createInventory(event.getPlayer(), event.getView().getTopInventory().getType());
                top.addItem(event.getView().getTopInventory().getContents());
                return top;
            }
           
            // creates a new bottom inventory, then fills it with the content of the real bottom inventory
            public Inventory getBottomInventory() {
                Inventory bottom = Bukkit.createInventory(event.getPlayer(), event.getView().getBottomInventory().getType());
                bottom.addItem(event.getView().getBottomInventory().getContents());
                return bottom;
            }
    }
    
     
  5. Hmm, yeah i thought of that too, so i guess i'll just do that.

    That doesn't work...it's weird, i get an NullPointerException on:
    top.addItem(event.getView().getTopInventory().getContents());
    Did you try your code? It doesn't work for me:(

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

    Chuckleluck

    Is event.getView().getTopInventory() empty when you tested it? You might have to modify it to check for NullPointExceptions.
     
Thread Status:
Not open for further replies.

Share This Page