From 662cef44d53d35046c2bdb9808efc9e2a102abc0 Mon Sep 17 00:00:00 2001 From: JeromSar Date: Fri, 26 Aug 2016 12:54:53 +0200 Subject: [PATCH] Automatically despawn item entities after some time. Resolves #901 --- .../totalfreedommod/EntityWiper.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/src/main/java/me/totalfreedom/totalfreedommod/EntityWiper.java b/src/main/java/me/totalfreedom/totalfreedommod/EntityWiper.java index 70a8287c..8e89211d 100644 --- a/src/main/java/me/totalfreedom/totalfreedommod/EntityWiper.java +++ b/src/main/java/me/totalfreedom/totalfreedommod/EntityWiper.java @@ -29,6 +29,7 @@ import org.bukkit.entity.ThrownPotion; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.block.BlockBreakEvent; +import org.bukkit.event.entity.ItemSpawnEvent; import org.bukkit.inventory.Inventory; import org.bukkit.inventory.InventoryHolder; import org.bukkit.scheduler.BukkitRunnable; @@ -37,7 +38,8 @@ import org.bukkit.scheduler.BukkitTask; public class EntityWiper extends FreedomService { - public static final long WIPE_RATE = 5 * 20L; + public static final long ENTITY_WIPE_RATE = 5 * 20L; + public static final long ITEM_DESPAWN_RATE = 20L * 20L; public static final int CHUNK_ENTITY_MAX = 30; // private final List> wipables = new ArrayList<>(); @@ -78,7 +80,7 @@ public class EntityWiper extends FreedomService { wipeEntities(); } - }.runTaskTimer(plugin, WIPE_RATE, WIPE_RATE); + }.runTaskTimer(plugin, ENTITY_WIPE_RATE, ENTITY_WIPE_RATE); } @@ -172,22 +174,21 @@ public class EntityWiper extends FreedomService return removed; } - @EventHandler(priority = EventPriority.HIGH, ignoreCancelled = true) - public void onContainerBreak(BlockBreakEvent event) + @EventHandler(priority = EventPriority.NORMAL, ignoreCancelled = true) + public void onItemSpawn(ItemSpawnEvent event) { - if (!ConfigEntry.AUTO_ENTITY_WIPE.getBoolean()) - { - return; - } + final Item entity = event.getEntity(); - BlockState state = event.getBlock().getState(); - if (!(state instanceof InventoryHolder)) + new BukkitRunnable() { - return; - } - Inventory inv = ((InventoryHolder) state).getInventory(); - inv.clear(); + @Override + public void run() + { + entity.remove(); + } + }.runTaskLater(plugin, ITEM_DESPAWN_RATE); + } }