Solved cant remove item from inventory :(

Discussion in 'Plugin Development' started by moroved, Aug 7, 2015.

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

    moroved

    for some reason when im trying to remove an item with custom displayname it doesnt works....
    Code here:
    Code:
    @EventHandler
         public void reloading(PlayerInteractEvent e){
             Player p =e.getPlayer();
             if(e.getAction() == Action.LEFT_CLICK_BLOCK ||e.getAction() == Action.LEFT_CLICK_AIR){
                 //Reloading Confirm\\
                 if(p.getItemInHand().getType() == Material.WOOD_HOE){
                    if(p.getItemInHand().getItemMeta().hasDisplayName()){
                        if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"M9")){
                           
                            ItemStack m9ammo = new ItemStack(Material.COAL);
                            ItemMeta m9m = m9ammo.getItemMeta();
                            m9m.setDisplayName(ChatColor.RED+"Pistol Ammo");
                            m9ammo.setItemMeta(m9m);
                             if(p.getInventory().containsAtLeast(m9ammo, 1)){
                                 p.getItemInHand().setDurability((short) 0);
                                 p.getInventory().removeItem(m9ammo);
                                 p.updateInventory();
    
                             }
                           
                           
                           
                        }
                    }
                 }
             }
         }
     
  2. Offline

    AcePilot10

    Is there any error? What happens when you test it?
     
  3. Offline

    moroved

    nothing, like any normal left click with a hoe... like there is no plugin.
     
  4. Offline

    Epicballzy

    Send the player a message after every check to see what is and isn't running.
     
  5. Offline

    moroved

    i did, its working until the last condition:
    if(p.getInventory().containsAtLeast(m9ammo, 1)){
     
  6. @moroved
    Just a tip, you need to make way more checks for the item or NullPointerExceptions wíll be thrown. e.g. p.getItemInHand() can be null and ItemStack#getItemMeta() can be null also. The reason it's not working might be due to the item in your inventory having a lore of enchantment, make sure that's set. What you can also do is store the pistol ammo somewhere in a field so you can refer to it when you give the item and when you check the item, so it gives better results.
     
  7. Offline

    moroved

    @megamichiel
    The item doesnt have Enchantment lore, its still not working..
    btw
    i fixed the null problems as you said..
     
  8. Offline

    moroved

    Fixed it using this code:
    Code:
    public boolean removeItem(Material material, Player p, int num, String name, String lore){
            ItemStack item = new ItemStack(material, num);
            ItemMeta m = item.getItemMeta();
            m.setDisplayName(name);
            ArrayList<String> lore1 = new ArrayList<String>();
            lore1.add(lore);
            m.setLore(lore1);
            item.setItemMeta(m);
            p.getInventory().removeItem(item);
            p.updateInventory();
           
            return false;   
        }
    Code:
     @EventHandler
         public void reloading(PlayerInteractEvent e){
             Player p =e.getPlayer();
             if(e.getAction() == Action.LEFT_CLICK_BLOCK ||e.getAction() == Action.LEFT_CLICK_AIR){
                 //Reloading Confirm\\
                 if(p.getItemInHand().getType() == Material.WOOD_HOE){
                    if(p.getItemInHand().getItemMeta().hasDisplayName()){
                        if(p.getItemInHand().getItemMeta().getDisplayName().equalsIgnoreCase(ChatColor.RED+"M9")){
                            ItemStack item = new ItemStack(Material.COAL, 1);
                            ItemMeta m = item.getItemMeta();
                            m.setDisplayName(ChatColor.RED+"Pistol Ammo");
                            ArrayList<String> lore1 = new ArrayList<String>();
                            lore1.add(ChatColor.GREEN+"15 9mm Bullets");
                            m.setLore(lore1);
                            item.setItemMeta(m);
                            if(p.getInventory().containsAtLeast(item, 1)){
                                p.sendMessage("Works!");
                                removeItem(Material.COAL, p, 1, ChatColor.RED+"Pistol Ammo", ChatColor.GREEN+"15 9mm Bullets");
                            }
                             }
                          
                           
                           
                           
                        }
                    }
                 }
             }
    
     
Thread Status:
Not open for further replies.

Share This Page