Weird ItemStack behavior

Discussion in 'Plugin Development' started by raGan., Jul 18, 2012.

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


    I was trying to give player some diamonds every time he types a command. I set ItemStack to 240 diamonds. First run always works, but it modifies ItemStack's amount and next run will give player less diamonds. If player's inventory is empty, he will get 3 full stacks + 48 diamonds. But next time command is run, player will get only 48 diamonds. If player had some diamonds before, next run will give him amount of diamonds same as in the last stack command gave him for the first time. So if he had 10 dia already, it would be 10 + 3x64 + 48 = 3x64 + 58 -> 58 diamonds. He will be getting that amount until server is reloaded/restarted. Is that bukkit bug or is error somewhere in my code ? I doubt it since item variable is final. I run Bukkit-1.2.5-R4.0-b2222jnks.
    Here's the code:
    final ItemStack item = new ItemStack(264, 240);
    public boolean giveReward(Player player) {
            player.sendMessage("Amt " + this.item.getAmount());
            return true;
        public boolean checkReward(Player player) {
            int maxSize = this.item.getMaxStackSize();
            int numSpaces = 0;
            ItemStack[] contents = player.getInventory().getContents();
            for (ItemStack i : contents) {
                if (i == null) {
                    numSpaces += maxSize;
                } else if (i.getType().equals(this.item.getType())) {
                    numSpaces += (maxSize - i.getAmount());
            contents = null;
            return (numSpaces >= this.item.getAmount());
                        pl.sendMessage("Yay, you got some diamonds !!!");
                    } else {
                        pl.sendMessage("Not enough space. :(");
  2. Offline


    Making a variable final doesn't prevent the object from being modified. Your ItemStack is being modified by the Inventory.addItem method.

    Edit: change it to:

    raGan. likes this.
  3. Offline


Thread Status:
Not open for further replies.

Share This Page