Solved dropping certain items on death

Discussion in 'Plugin Development' started by MordorKing78, Jan 27, 2015.

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

    MordorKing78

    So I want to drop certain items when someone dies.

    Here is the code I tried:

    Code:
            if(e instanceof Player){
                p.sendMessage("§eYou earned§b " + number + "§e levels for killing §b" + e.getEntity().getCustomName());   
               
                for(ItemStack drop : e.getDrops())
                {
                    if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Golden Apple")) {
                        e.getDrops().remove(drop);
                    }else if(drop.getItemMeta().getDisplayName().equals("§5Dark Sword")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§bIcy Sword")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Sword")) {
                        if(!(drop.getEnchantments().size() > 1)){
                            e.getDrops().remove(e.getDrops().indexOf(drop));
                        }
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Bow")) {
                        if(!(drop.getEnchantments().size() > 1)){
                            e.getDrops().remove(e.getDrops().indexOf(drop));
                        }
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Arrow")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§6Flame Helmet")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§6Flame Chestplate")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§6Flame Leggings")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§6Flame Boots")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Helmet")) {
                        if(!(drop.getEnchantments().size() > 1)){
                            e.getDrops().remove(e.getDrops().indexOf(drop));
                        }
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Chestplate")) {
                        if(!(drop.getEnchantments().size() > 1)){
                            e.getDrops().remove(e.getDrops().indexOf(drop));
                        }
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Leggings")) {
                        if(!(drop.getEnchantments().size() > 1)){
                            e.getDrops().remove(e.getDrops().indexOf(drop));
                        }
                    }else if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Boots")) {
                        if(!(drop.getEnchantments().size() > 1)){
                            e.getDrops().remove(e.getDrops().indexOf(drop));
                        }
                    }else if(drop.getItemMeta().getDisplayName().equals("§5Dark Helmet")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§5Dark Chestplate")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§5Dark Leggings")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§5Dark Boots")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§bIcy Helmet")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§bIcy Chestplate")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§bIcy Leggings")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§bIcy Boots")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }else if(drop.getItemMeta().getDisplayName().equals("§bCold Drink")) {
                        e.getDrops().remove(e.getDrops().indexOf(drop));
                    }
                }
            }
     
  2. Offline

    CraftCreeper6

  3. Offline

    MordorKing78

    Last edited: Jan 27, 2015
  4. Offline

    caderape

    if(e instanceof Player)

    Your code will never work like that. An evenement can't be a player
     
    Konato_K likes this.
  5. Offline

    Ridan

    do if(e.getEntity() instanceof Player){} instead of if(e instanceof Player)
     
  6. Offline

    MordorKing78

    @caderape @Ridan Changed, But still not working

    I cant seem to figure it out, I got some new code but it does only work for 1 itemstack the others just drop,

    NEW CODE:

    Code:
       List<ItemStack> drops = e.getDrops();
           
            ListIterator<ItemStack> litr = drops.listIterator();
            
            while(litr.hasNext() )
            {
            ItemStack drop = litr.next();
           
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Golden Apple")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§5Dark Sword")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§bIcy Sword")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Sword")) {
                if(!(drop.getEnchantments().size() > 0)){
                    litr.remove();
                }
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Bow")) {
                if(!(drop.getEnchantments().size() > 0)){
                    litr.remove();
                }
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Arrow")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§6Flame Helmet")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§6Flame Chestplate")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§6Flame Leggings")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§6Flame Boots")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Helmet")) {
                if(!(drop.getEnchantments().size() > 0)){
                    litr.remove();
                }
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Chestplate")) {
                if(!(drop.getEnchantments().size() > 0)){
                    litr.remove();
                }
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Leggings")) {
                if(!(drop.getEnchantments().size() > 0)){
                    litr.remove();
                }
            }
            if(drop.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Boots")) {
                if(!(drop.getEnchantments().size() > 0)){
                    litr.remove();
                }
            }
            if(drop.getItemMeta().getDisplayName().equals("§5Dark Helmet")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§5Dark Chestplate")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§5Dark Leggings")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§5Dark Boots")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§bIcy Helmet")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§bIcy Chestplate")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§bIcy Leggings")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§bIcy Boots")) {
                litr.remove();
            }
            if(drop.getItemMeta().getDisplayName().equals("§bCold Drink")) {
                litr.remove();
            }
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 13, 2016
  7. @MordorKing78
    Why not use:
    for (ItemStack drop : drops) { }?

    and use e.getDrops().remove(ItemStack); instead of litr.remove();

    I think it should be best to create itemstacks on accessory methods, then just make a list with them, iterate though that list, check if drops contains that item, and remove it from drops.
     
  8. Offline

    MordorKing78

    @Juancomaster1998 Then damaged items won't work if I use itemstacks.


    Look, It only removes 1 item (the first one) so if my drops would contain golden apples and dia sword (custom named) only the golden apple will be removed.
     
  9. Offline

    guitargun

    I

    Use the loop mentioned above then compare the item name the use the event.getdrops.remove(drop) that should work right?
     
  10. Offline

    MordorKing78

    @guitargun How would I loop trough all the items with an custom name?
     
  11. Offline

    guitargun

    if you store those custom names in a list or so and check with list.contains(drop.getitemMeta.getDisplayname()){} then it should work (it is what I mostly use atm)
     
  12. Offline

    MordorKing78

    @guitargun Hmm.. So I store all the items in a lit and check if this drop is in the list? And then..?
     
  13. Offline

    guitargun

    then you remove the drop that is what you wanted
     
  14. Offline

    Funergy

    @MordorKing78
    @guitargun
    for(ItemStack is : e.getDrops()){
    if(is.getItemMeta().getDisplayName().equalsIgnoreCase(<The name>)){
    e.getDrops().remove(is);
    }
    }
     
  15. Offline

    MordorKing78

    @Funergy Did you even read everything? :3 (I already tried that)
     
    Last edited: Jan 29, 2015
  16. Offline

    Funergy

  17. Offline

    Konato_K

    @Funergy Call me crazy, but I think that may throw a ConcurrentModificationException, using an iterator should be better
     
  18. Offline

    sirrus86

    That's correct, if you want to iterate over a list or set and remove items from it during the iteration, you'll either want to use the iterator() method or a for(int i = 0; i < list.size(); i ++) loop.
     
  19. Offline

    MordorKing78

    @Funergy well, it doesn't ;p

    @sirrus86 Uhh, I never used something to iterate over a list. Could you help me with that plz?? Or show me an example.
     
  20. Offline

    Konato_K

  21. Offline

    MordorKing78

    @Konato_K I have no qlue how to combine this with my problem
     
  22. Offline

    Konato_K

    @MordorKing78 Use e.getDrops().getIterator(); That will give you an Iterator<ItemStack> and you can use the iterator methods and iterate like in the SO question
     
  23. Offline

    MordorKing78

    @Konato_Kw .. remove(e.getDrops.getIterator()) ??
     
  24. Offline

    Konato_K

    @MordorKing78 No
    Code:
    for(Iterator<Potato> iterator = potatoList.iterator(); iterator.hasNext(); )
    { Potato potato = iterator.next();
      if(potato.isPoisonous())
      { iterator.remove();
      }
    }
    
     
  25. Offline

    MordorKing78

    @Konato_K How would I do this with custom names and remove the drops!?!???
     
  26. Offline

    Konato_K

    @MordorKing78 Instead of checking if the ItemStack "is poisonous" use a different conditional
     
  27. Offline

    MordorKing78

    @Konato_K
    Code:
    for(Iterator<Potato> iterator = potatoList.iterator(); iterator.hasNext(); )
    { Potato potato = iterator.next();
      if(potato.getItemMeta..?)
      { iterator.remove();
      }
    }
    Also. What is patatoList ?
     
  28. Offline

    Konato_K

    @MordorKing78 A potatoList it's a List that contains some Potato elements, it's just an example, use ItemStack in your case, check the ItemMeta stuff in your case and that, I just gave you an example on how to use it.
     
  29. Offline

    MordorKing78

    @Konato_K Ooh, God finnaly. I love you! (NoHomo) Here some slices of cake,
    [cake][cake][cake][cake][cake][cake][cake]

    Also for the people who need this, Here is the code:
    Code:
        @EventHandler
        public void onDeath(PlayerDeathEvent e) { 
            Player p = e.getEntity();
    
            for(Iterator<ItemStack> iterator = e.getDrops().iterator(); iterator.hasNext(); ){
                ItemStack potato = iterator.next();
                if(potato.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Golden Apple")){
                    iterator.remove();
                }
                if(potato.getItemMeta().getDisplayName().equals("§c[NTPVP]§b Diamond Sword")){
                    iterator.remove();
                }
            }    
    (Replace the item meta ^_^)
     
  30. Offline

    1Rogue Retired Staff

    Instead of manual iteration you should keep a collection of the names that you don't allow, and then just use .removeAll() on the drop list.
     
Thread Status:
Not open for further replies.

Share This Page