Refresh Inventory

Discussion in 'Plugin Development' started by funkystudios, Jul 20, 2012.

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

    funkystudios

    After depositing items into a chest, my plugin adds items to that player's inventory. The player doesn't see the icons/numbers as if they are invisible. If they log out and back in they can see it. Also if they right click to use the item it appears.

    I'm looking for some type of solution/method to refresh or reload a player's inventory.

    p.updateInventory() is deprecated and doesn't seem to work.
     
  2. Offline

    Squish000

    How are you adding the items into the inventory?
     
  3. Offline

    funkystudios

    Code:
    Player p = event.getPlayer();
    p.getInventory().addItem(i);
     
  4. look how command book manages it to do, as fa I know commandbook dont have this bug (and commandbook is on github)
     
  5. Offline

    Njol

    player.updateInventory()
     
  6. Offline

    CorrieKay

    updateInventory() is deprecated because its only a workaround for a severe bug that (hopefully) theyre working on. However, i find it very interesting that it doesnt work.

    If you can find out if the inventory is correct serverside, try this:
    p.getInventory().setContents(p.getInventory().getContents());

    That might work, no guarentees though.
     
  7. Offline

    funkystudios

    Well here's something I noticed...

    The full code for placing items in the players inventory looks something like....
    Code:
    for (ItemStack i : giveBack) {
      p.getInventory().addItem(i);
    }
    p.updateInventory();
    When there is more than one item in the giveBack list, the items show up on the client's side. If there is only one, then that one item doesn't display on the client's side.

    I'll try it and let you know if it works, thanks!

    No luck :/

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

    CorrieKay

    Thats really odd. Really really... odd..

    Okay, what exactly are you doing? they close a chest and you put items in their inventory?

    Can you post any and all code relevent to this?
     
  9. Offline

    r0306

    funkystudios
    If none of the above methods work, then try packet sending:
    Code:
        public Packet104WindowItems sendItemUpdate(Player player, ItemStack[] item)
        {
     
            Packet104WindowItems packet = new Packet104WindowItems();
                               
            packet.a = 0;
            packet.b = toItemStack(item);
       
            return packet;
       
        }
     
        public net.minecraft.server.ItemStack[] toItemStack(ItemStack[] item)
        {
       
            net.minecraft.server.ItemStack[] serverItems = new net.minecraft.server.ItemStack[item.length];
       
            for (int i = 0; i < item.length; i ++)
            {
           
                serverItems[i] = new net.minecraft.server.ItemStack(item[i].getTypeId(), item[i].getAmount(), item[i].getDurability(), null);
           
            }
       
            return serverItems;
       
        }
    Send the update to the players with this line:
    Code:
    ((CraftPlayer)player).getHandle().netServerHandler.sendPacket(sendItemUpdate(player, itemstack);
    If you only gave one itemstack to the player, you will still have to convert it into an array before passing it on as a parameter though. I'm not too sure on how to convert bukkit itemstack to minecraft server itemstack so give my code a try.

    Oh, and also you will need to add the normal craftbukkit.jar server into your build paths, not just the api.
     
  10. Offline

    messageofdeath

    You don't need to update the inventory. It should work fine without updating it. I think they made it where it auto-updates it.
     
  11. Offline

    funkystudios

    I found it seems to depend on your own computer. I was on my netbook testing it (terrible CPU and graphics card) and it wouldn't update the inventory. I tried on my nice PC and it works fine... Strange issue...

    Thanks for all your help, everyone!
     
Thread Status:
Not open for further replies.

Share This Page