Live Inventory Updates

Discussion in 'Plugin Development' started by darkbro100, Jun 18, 2014.

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

    darkbro100

    Hello,

    So I'm trying to figure out how to do live inventory updates. When two players are currently in the same inventory, if one of them removes an item from the inventory it needs to update the inventory for the other player.

    Code:
    @EventHandler
    public void onInvClick(InventoryClickEvent e) {
    if(!(e.getWhoClicked() instanceof Player)) return;
     
    Player p = (Player) e.getWhoClicked();
    for(HumanEntity h : e.getInventory().getViewers()) {
    if(!(h instanceof Player)) return;
     
    Player player = (Player) h;
     
    if(player.getName().equalsIgnoreCase(p.getName())) return;
     
    player.getOpenInventory().setItem(e.getSlot(), e.getCurrentItem());
    }
    }
    
    This is what I have tried and it hasn't worked. If anyone has a better idea on how to do this that would be great, thank you.
     
  2. darkbro100 well, if youre creating a single inventory and opening it for both, it should update (correct me if im wrong), since its same inventory, just 2 player looking at it.. (same as 2 people looking in a chest)
     
  3. Offline

    darkbro100


    I'm creating the inventory when the command is ran, there is no inventory holder. If two people have the inv opened nobody and someone takes an item out of the inv it doesn't update for the other players. I need a way in code to fix this.
     
  4. darkbro100 well, perhaps you can store the inventory in hash map (when creating it via command) with the player name, and for the other player, open the inventory using the hashmap, that should solve the problem I think... (since its really, same inventory) then you can simply cancel click event if you dont want certain player to take certain item
     
  5. Offline

    Rocoty

    darkbro100 The command? Which command? You didn't mention a command previously. Are you 100% sure that the player's are seeing the exact same inventory? Post more of your code that we can help you further.
     
  6. Offline

    darkbro100


    Oh, my bad. I'm creating a new inventory each time with the same item contents. So, I'll refurbish the code and let you know if I'm still receiving this problem.
     
Thread Status:
Not open for further replies.

Share This Page