Solved ItemPickup Problem

Discussion in 'Plugin Development' started by Pik0, Apr 10, 2014.

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

    Pik0

    Hello guys, i am trying to give a player an axe based on player heads picked up:
    Code:
    @EventHandler
    public void pickup(PlayerPickupEvent e){
    Player p = e.getPlayer();
            if (!plugin.am.playerArena.containsKey(p.getName()))
                return;
            if (e.getItem().getItemStack().getType() == Material.SKULL_ITEM) {
                p.updateInventory();
                int size = 0;
                for (Map.Entry<Integer, ? extends ItemStack> set : p.getInventory()
                        .all(Material.SKULL_ITEM).entrySet()) {
                    size += set.getValue().getAmount();
                }
                plugin.am.playerArena.get(p.getName()).score.put(p.getName(), size);
                plugin.am.updateAxe(p);
            }
    }
    updateAxe is:
    Code:
    public void updateAxe(Player p) {
            p.updateInventory();
            int size = playerArena.get(p.getName()).score.get(p.getName());
            ItemStack axe = new ItemStack(Material.STONE_AXE);
            ItemMeta im = axe.getItemMeta();
            im.setDisplayName("Little axe");
            axe.setItemMeta(im);
            axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 1);
            p.getInventory().setItem(0, axe);
            if (size == 0) {
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 2) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 2);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 3) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 3);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 4) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 4);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 5) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5 - 1);
                p.setMaxHealth(22);
                p.setHealth(22);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 6) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 7) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5);
                p.setMaxHealth(24);
                p.setHealth(24);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 8) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 5);
                p.setMaxHealth(26);
                p.setHealth(26);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 9) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 6);
                p.setMaxHealth(28);
                p.setHealth(28);
                p.getInventory().setItem(0, axe);
                return;
            }
            if (size == 10) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 7);
                p.setMaxHealth(30);
                p.setHealth(30);
                p.getInventory().setItem(0, axe);
                return;
            } else if (size > 10) {
                axe.addUnsafeEnchantment(Enchantment.DAMAGE_ALL, 7);
                p.setMaxHealth(30);
                p.setHealth(30);
                p.getInventory().setHelmet(new ItemStack(Material.DIAMOND_BLOCK));
                p.getInventory().setItem(0, axe);
                return;
        }
    }
    However, here is where the problem comes, when i pickup stacks of items, like 2 items together, it doesnt update very well, doesnt recognize the real amount of skulls the player picked up

    bump

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  2. This won't help you with that error, but you should read the JavaDocs more.
    You're calling the updateInventory() method way too much, even when not needed. You should only call it when you're adding multiple items in a short period if time, such as a for loop.
     
  3. Offline

    Pik0

    KingFaris11 Should i remove the updateInventory()?
     
  4. Yes.

    Code:
    @EventHandler
    public void onPlayerPickup(PlayerPickupItemEvent event) {
        // Do what you want.
        if (event.getItem().getItemStack().getType() == Material.SKULL_ITEM) {
            int size = event.getItem().getItemStack().getAmount();
            for (Entry<Integer, ? extends ItemStack> itemStack : event.getPlayer().getInventory().all(Material.SKULL_ITEM).entrySet()) {
                size += itemStack.getValue().getAmount();
            }
            // Update Map and update axe.
        }
    }
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  5. Offline

    Pik0

    KingFaris11 I fixed the problem by removing the updateInventory() and i added an delayed task for the update task. Now works fine, thanks for your help
     
    KingFaris11 likes this.
  6. You shouldn't need a scheduler. The reason the scheduler looked like it was the fix was because basically PlayerPickupItemEvent is called, but the item is not actually added into the inventory YET, so by making a scheduler, you're allowing the item to be in the player's inventory, and then check how many skulls he has. You should do what I said, doing:
    Code:
    int size = event.getItem().getItemStack().getAmount();
    Once called updateAxe(), if it's not updating the inventory, call player.updateInventory() AFTER you've set the axe item.
     
Thread Status:
Not open for further replies.

Share This Page