Solved Removing item with certain ItemMeta.

Discussion in 'Plugin Development' started by 87pen, Nov 17, 2015.

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

    87pen

    I'm trying to check if the player clicks on a sword in a inv menu that if he has the items required to forge the item clicked it subtracts the requirements of his inventory and gives him the sword.
    Code:
                if(event.getCurrentItem().equals(x(p))){
                    p.sendMessage("You attempt to forge excalibur.");
                    if(p.getInventory().contains(Material.EMERALD, 1) && p.getInventory().contains(Material.GOLD_INGOT, 10)){
                        for(ItemStack hh : p.getInventory().getContents()){
                            if(hh != null){
                                if(hh.getAmount() >= 10 && hh.getType().equals(Material.GOLD_INGOT)){
                                    p.sendMessage("This is working.");
                                    hh.setAmount(hh.getAmount() - 10);
                                }
                                if(hh.getAmount() >= 1 && hh.getType().equals(Material.EMERALD)){
                                    hh.setAmount(hh.getAmount() - 1);
                                }
                            }
                        }
                        p.getInventory().addItem(x(p));
                    }
                }
    Code:
        public ItemStack x(Player p){
            ItemStack x = new ItemStack(Material.GOLD_SWORD, 1);
            ItemMeta im = x.getItemMeta();
            im.setDisplayName(ChatColor.GOLD + "Excalibur"); 
            im.setLore(Arrays.asList(new String[] { ChatColor.GRAY + "Forged by: " + p.getName(), ChatColor.GRAY + "Tier: 10" }));
            im.addEnchant(Enchantment.DAMAGE_ALL, 5, true);
            x.setItemMeta(im);
            return x;
        }
    It's all good but it does not subtract the amount it just stays the same. I also tried p.getInventory().remove(new ItemStack(Material.GOLD_INGOT, 10)); but it would only remove itemstacks without meta. I have no idea why setAmount is not working. There is no stacktraces. It just gives me the sword and doesn't take the items.
     
  2. Offline

    Zombie_Striker

    @87pen
    Set amount only works if the about is greater than 1. It does not actually remove the item.\

    Instead of creating a new itemstack for removing, try getting that itemstack's instance (the one in the player's inventory) and removing that.
     
    87pen likes this.
  3. Offline

    87pen

    Worked thanks
     
Thread Status:
Not open for further replies.

Share This Page