AddItemsFix - Fix for adding items with data values on a stack

Discussion in 'Resources' started by fabe, Mar 17, 2011.

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

    fabe

    Hey guys,
    before a month a tried to add red wool to my inventory. The inventory already included orange wool. But what was this?! After adding the red wool got in to the stack of the orange wool. So now I had 2 orange wools on one stack. It's possible that I've made sth. wrong, but I couldn't really find the problem causing this. I don't know if it got already fixed in the recent Bukkit version, but shortly after that I wrote following methode to fix this issue. Maybe somebody learns from that.

    Code:
    public void addItemFix(Player players, int ID, int amount, short dur) {
    
            if (players.getInventory().contains(ID) && (ID == 35 || ID == 351)) { // Wool or Dye
                HashMap<Integer, ? extends ItemStack> invItems = players.getInventory()
                        .all(ID);
    
                int restAmount = amount;
                for (Map.Entry<Integer, ? extends ItemStack> entry : invItems
                        .entrySet()) {
    
                    int index = entry.getKey();
                    ItemStack item = entry.getValue();
                    int stackAmount = item.getAmount();
    
                    // e.g. same wool in inventory => put in to stack
                    if (dur == item.getDurability()) {
    
                        if (stackAmount < 64) {
                            // Add to stack
                            int canGiveAmount = 64 - stackAmount;
                            int giveAmount;
    
                            if (canGiveAmount >= restAmount) {
                                giveAmount = restAmount;
                                restAmount = 0;
                            } else {
                                giveAmount = canGiveAmount;
                                restAmount = restAmount - giveAmount;
                            }
    
                            players.getInventory()
                                    .setItem(
                                            index,
                                            new ItemStack(ID, stackAmount
                                                    + giveAmount, dur));
    
                        }
                    }
                }
                // If there is still a rest, add the rest to the inventory
                if (restAmount > 0) {
                    int emptySlot = players.getInventory().firstEmpty();
                    players.getInventory().setItem(emptySlot,
                            new ItemStack(ID, restAmount, dur));
                }
            } else {
                // Standard usage of addItem
                players.getInventory().addItem(new ItemStack(ID, amount, dur));
            }
        }
     
  2. Offline

    Deathly

    Same thing occurs when removing items.. :)

    I just wanted to clear the slot anyway so I made a method for that, but you could add a fix for removing items :D
     
  3. Offline

    matejdro

    Is this still needed in new bukkit?
     
  4. Offline

    Pamagester

    I'm really new to bukkit development but as I needed a way to remove the amount of dyes or wools and return true or false if the person had enough of it, this helped me a lot. so here is the code
    Code:
        public boolean charge(Player player,int id, byte dur, int amount)
        {
            int count = 0;
            HashMap<Integer, ? extends ItemStack> invItems = player.getInventory().all(id);
            for (Map.Entry<Integer, ? extends ItemStack> entry : invItems.entrySet()) {
                ItemStack item = entry.getValue();
                int stackAmount = item.getAmount();
                if (dur == item.getDurability()) {
                    count += stackAmount;
                }
            }
            if(count >= amount)
            {
                for (Map.Entry<Integer, ? extends ItemStack> entry : invItems.entrySet()) {
                    int index = entry.getKey();
                    ItemStack item = entry.getValue();
                    int stackAmount = item.getAmount();
                    if (dur == item.getDurability()) {
                        if(stackAmount < amount)
                        {
                            player.getInventory().clear(index);
                            amount -= stackAmount;
                        }
                        else if(stackAmount == amount)
                        {
                            player.getInventory().clear(index);
                            return true;
                        }
                        else
                        {
                            player.getInventory()
                            .setItem(
                                    index,
                                    new ItemStack(id, stackAmount
                                            - amount, dur));
                return true;
                        }
                    }
                }
            }
            return false;
        }
     
Thread Status:
Not open for further replies.

Share This Page