Solved dropItem is dropping the item twice?

Discussion in 'Spigot Plugin Development' started by kinukin, Feb 1, 2020.

  1. Edit: So I checked and the event is running twice, why? I only registered the event once.

    So whenever a entity dies it drops an custom item but the problem is that it's dropping twice, why is that?

    I have two classes:

    This class is the custom item class.

    Code (Text):
    package items;

    import java.util.ArrayList;
    import java.util.List;

    import org.bukkit.Material;
    import org.bukkit.inventory.ItemStack;
    import org.bukkit.inventory.meta.ItemMeta;

    public class GoblinSword {
        private ItemStack itemStack;
     
        public void createItem() {
            this.itemStack = Sword();
        }
     
        public ItemStack Sword() {
            itemStack = new ItemStack(Material.DIAMOND_SWORD, 1);
            ItemMeta meta = itemStack.getItemMeta();
            meta.setDisplayName("Goblin Sword");
            List<String> list = new ArrayList<>();
            list.add("Level 1");
            meta.setLore(list);
            itemStack.setItemMeta(meta);
            return itemStack;
        }
     
     
        public ItemStack getItemStack() {
            return itemStack;
        }

    }
     
    I have this in another class with this code:
    Code (Text):
        @EventHandler

        public void onGoblinDeath(EntityDeathEvent e) {
            GoblinSword sword = new GoblinSword();
            sword.createItem();
            ItemStack goblinsword = sword.getItemStack();
            if(e.getEntity().getName().equals("Goblin")) {
                e.getEntity().getWorld().dropItemNaturally(e.getEntity().getLocation(), goblinsword);
            }
        }
     
    #1 kinukin, Feb 1, 2020
    Last edited: Feb 1, 2020
  2. md_5

    Administrator Developer

    Did you check you didn't accidentally register the listener twice
     
  3. I only have it registered once.
     
  4. One reason may be that your goblin entity held an identical copy to the one which would be created in the death event - so both were dropped.

    Also, why do you need a class just to create an item? use this method in your listener class instead.
     
    #4 HorrendousEntity, Feb 1, 2020
    Last edited: Feb 2, 2020
  5. md_5

    Administrator Developer

    Good point
    Probably so the same item can be easily created elsewhere
     
  6. I've tried checking if it drops when the entity dies but it's not that.
     
  7. You could use a EntityDeathEvent to set the dropped item from a certain or all entity's when they die?

    Or set a single entities drop using dropItem()

    EDIT: Saw your using that event,

    e.getDrops().add(stack);
     
  8. Items don't drop when using e.getDrops().add(stack);
     
  9. Try clearing them first, then adding one.

    e.getDrops().clear();
     
  10. Still doesn't drop.
     
  11. So I checked that the event is triggering twice, but I only registered the event once. Why is that?