Player's Inventory not updating after adding items

Discussion in 'Plugin Development' started by ZephireNZ, Apr 22, 2014.

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

    ZephireNZ

    I'm encountering an annoying bug, where the player's inventory becomes out of sync with that of the server. I run player.getInventory().addItem(item), and I can see it's definitely there through the debug. But the client's inventory never updates. I've tried using the player.updateInventory() method as mentioned elsewhere, but that doesn't seem to work either. I've also tried other methods, such as updating the array in getContents(), using setContents(), and using setItem(i, item), all with updating the inventory as well.
    For reference: here's the code snippet I've got currently:
    Code:java
    1. Inventory inv = ((InventoryHolder) bAttached.getState()).getInventory();
    2. ItemStack[] contents = inv.getContents();
    3. Double val = 0D;
    4. for(int i = 0; i<inv.getSize(); i++) {
    5. ItemStack item = contents[ i ];
    6. if(item == null) continue;
    7. inv.setItem(i, null);
    8. if(eco.isDenomination(item.getType())) {
    9. val += eco.getDenomination(item.getType()).getValue() * item.getAmount();
    10. continue;
    11. }
    12.  
    13. HashMap<Integer, ItemStack> leftovers = player.getInventory().addItem(item);
    14. for(ItemStack left : leftovers.values()) {
    15. player.getWorld().dropItem(player.getLocation(), left);
    16. }
    17. }

    If it helps, I'm also running Spigot build 1391, which is 1.7.8. If it turns out this is related to Spigot, I'll be happy to take this to their forums instead :)
     
  2. Offline

    Drkmaster83

    What's the end result of the leftovers? Does the item drop but still remain in the inventory?
    What did you expect the inventory to do, and what did it actually do?
     
  3. Offline

    ZephireNZ

    Essentially, I try and add the item to the player's inventory. Leftovers will return back any items that couldn't be added to the player's inventory, so I drop it instead.

    If I have space (which is usually the case), the item is added to the inventory. I can see this by doing getInventory afterwards. I would expect the client to see the inventory be updated, but in this case nothing happens. The client doesn't see the item in its player inventory until another inventory (such as a chest) is opened. When they open an inventory, it 'pops' into existence for the player in their own inventory.
    What's more worrying is that the player can overwrite the item if they can't see it, wiping it from the Bukkit and client inventories altogether.

    Alright, so I had the idea to delay the update using a task, and that seems to have worked. I have no idea, but it works so I'll go with it xD
    I used:
    Code:java
    1. new BukkitRunnable() {
    2. @Override
    3. public void run() {
    4. player.updateInventory();
    5. }
    6. }.runTaskLater(plugin, 0);


    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
Thread Status:
Not open for further replies.

Share This Page