From 9ffe0c740aeb00e43b726a9e5fba89031347bd6d Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 9 Jan 2024 17:47:24 +0900 Subject: [PATCH] Refactor code to enable golem spawning. #180 --- .../limits/listeners/EntityLimitListener.java | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java b/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java index 3f3cf3e..f5da3e6 100644 --- a/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java +++ b/src/main/java/world/bentobox/limits/listeners/EntityLimitListener.java @@ -180,7 +180,7 @@ public class EntityLimitListener implements Listener { if (island.isSpawn() || !res.hit()) { // Allowed if (async) { - Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> l.getWorld().spawn(l, e.getClass(), entity -> preSpawn(entity, reason, l))); + Bukkit.getScheduler().runTask(BentoBox.getInstance(), () -> preSpawn(e.getType(), reason, l)); } // else do nothing } else { if (async) { @@ -195,21 +195,23 @@ public class EntityLimitListener implements Listener { return true; } - private void preSpawn(Entity entity, SpawnReason reason, Location l) { - justSpawned.add(entity.getUniqueId()); + private void preSpawn(EntityType entityType, SpawnReason reason, Location l) { + // Check for entities that need cleanup switch (reason) { case BUILD_IRONGOLEM -> detectIronGolem(l); case BUILD_SNOWMAN -> detectSnowman(l); case BUILD_WITHER -> { detectWither(l); + } + default -> throw new IllegalArgumentException("Unexpected value: " + reason); + } + Entity entity = l.getWorld().spawnEntity(l, entityType); + justSpawned.add(entity.getUniqueId()); + if (reason == SpawnReason.BUILD_WITHER) { // Create explosion l.getWorld().createExplosion(l, 7F, true, true, entity); } - default -> { - // Do nothing - } - } } private void detectIronGolem(Location l) { @@ -246,7 +248,6 @@ public class EntityLimitListener implements Listener { } } } - } private void detectSnowman(Location l) {