Removing Items from inventories

Discussion in 'Plugin Development' started by gamerguy14, Dec 11, 2011.

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

    gamerguy14

    In the plugin that I am working on, I have to remove and add items to inventories. Wen I remove an item, it behaves like I removed it but the item's icon stays in the inventory and will appear in the player's until the player tries to place it, an item gets picked up and goes in that slot or when the player opens up a chest. These are the only ways I found that does it but there might be more. Does anyone know how to force it to update the inventory?

    This is the way I remove the item:

    Code:
    ItemStack item = new ItemStack(someMaterial, someAmount);
    ItemStack[] stack = player.getInventory().getContents();
    stack[player.getInventory().first(item)] = null;
    player.getInventory().setContents(stack);
     
  2. Yeah... Bukkit (read Minecraft server) can't handle inventories that well. There is a way to update the clients inventory but I don't have the code at hand. If you want it give me a reply so I can see this topic again when I get home :)
     
  3. Offline

    gamerguy14

    Thanks. It isn't that big a deal, it just doesn't look professional.
     
  4. Well... Here it is! Probably got it from somewhere here on the forums ;)
    Code:
        private static void updateInventory(Player p) {
            CraftPlayer c = (CraftPlayer) p;
            for (int i = 0;i < 36;i++) {
                int nativeindex = i;
                if (i < 9) nativeindex = i + 36;
                ItemStack olditem =  c.getInventory().getItem(i);
                net.minecraft.server.ItemStack item = null;
                if (olditem != null && olditem.getType() != Material.AIR) {
                    item = new net.minecraft.server.ItemStack(0, 0, 0);
                    item.id = olditem.getTypeId();
                    item.count = olditem.getAmount();
                    item.b = olditem.getDurability();
                }
                Packet103SetSlot pack = new Packet103SetSlot(0, nativeindex, item);
                c.getHandle().netServerHandler.sendPacket(pack);
            }
        }
    
     
  5. Offline

    gamerguy14

    Doesn't work. There are a few problems. Here is a list

    • CraftPlayer doesn't exist anymore.
    • The variables ItemStack.id, ItemStack.count, and ItemStack.b are not variables in the ItemStack class and their real versions have private access.
    • If Packet103SetSlot exists, it needs to be put in the library.
    Also the fact that there are the packages before the objects means that it is likely outdated since we don't have to do that anymore.
     
  6. Offline

    Perdog

    There's already an updateInventory() method in craftbukkit? It's deprecated but it still does the job
     
  7. Offline

    gamerguy14

    Really? I never really looked at the craftbukkit API. What object is it in?
     
  8. Offline

    SirTyler

    just do player.updateInventory(); like they said its deprecated but its really the best choice for now.
     
  9. Offline

    ThatBox

    Can't you also do:
    Code:java
    1. player.getInventory().remove(ID of item)
     
  10. Using deprecated code... Hmmm... And on the next update of Bukkit your code breaks. Nice!
     
  11. Offline

    SpaceManiac

    The updateInventory() method has not changed since February. It is currently the best available workaround for the problem at hand and is not likely to go away in the near future. Using net.minecraft.server classes directly is far more likely to break.
     
  12. Ok... Okaaayhhh. Both methods work. So no more discussion about it :cool:
     
  13. Offline

    gamerguy14

    That removes every index of that item.
     
  14. Offline

    Perdog

    Code:java
    1. inventory.removeItem(new ItemStack (Mat, Int));

    That's how I've done it in Landmines, it works perfectly :)

    inventory is a variable of player.getInventory() and Mat1 and Int1 are variables being pulled from the config
     
  15. Offline

    kSafin

    Thanks Perdog, that did the trick.
     
Thread Status:
Not open for further replies.

Share This Page