Solved Looting on custom drops

Discussion in 'Spigot Plugin Development' started by GB_6, Apr 26, 2017.

  1. Hey fellow devs!

    I wanted to know how I would go about letting the looting enchant work on the drops that I define. What i tried:
    Code (Text):
    public class EntityDeathEvents implements Listener {

        @EventHandler(priority = EventPriority.HIGHEST)
        public void onEDeath(EntityDeathEvent e) {
            if (e.getEntityType() == EntityType.CREEPER && (Math.random() <= 0.15)) {
                e.getDrops().add(new ItemStack(Material.TNT));
            }
            if (e.getEntityType() == EntityType.IRON_GOLEM) {
                Iterator<ItemStack> iterator = e.getDrops().iterator();
                while (iterator.hasNext()) {
                    ItemStack stack = iterator.next();
                    switch (stack.getType()) {
                    case RED_ROSE:
                        iterator.remove();
                        break;
                    default:
                        break;
                    }
                }
            }
            if (e.getEntityType() == EntityType.MUSHROOM_COW) {
                e.getDrops().clear();
                e.getDrops().add(new ItemStack(Material.GOLD_INGOT));
            }
            if (e.getEntityType() == EntityType.MAGMA_CUBE) {
                e.getDrops().clear();
                e.setDroppedExp(20);
                if (Math.random() <= 0.90)
                    e.getDrops().add(new ItemStack(Material.DIAMOND));
            }

            if (e.getEntityType() == EntityType.VILLAGER) {
                e.getDrops().clear();
                e.getDrops().add(new ItemStack(Material.EMERALD));
            }
        }
    }
     
  2. Check if the item in hand of the killer containsEnchantment(Enchantment) in this case you would put looting in that parameter.

    If you want to check for the level, add that, then make an integer using the item.getEnchantmentLevel(Enchantment) method, to get the level of looting, then drop items based on that by adding items to the drops list of itemstacks.
     
    • Agree Agree x 2
  3. Ah yeah that was my plan B I thought there would be an easier way. Thanks for the reply
     
  4. It takes only like 3 lines of code to make the whole check, including the level variable. I don't think it could get much better than that :)
     
  5. Mark as resolved? :p
     
  6. Yeah I guess you are right.
     
  7. Then drop a rating! :D
     
    • Funny Funny x 1