From aadc92ce7abc30167823628754a8ae8ca09b45d9 Mon Sep 17 00:00:00 2001 From: mathias Date: Thu, 1 Aug 2019 17:58:50 +0300 Subject: [PATCH] Check if chunks are loaded --- .../extras/modules/entity/EntitySpawn.java | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java b/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java index 1ce7311..c62c93c 100644 --- a/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java +++ b/src/main/java/pw/kaboom/extras/modules/entity/EntitySpawn.java @@ -111,11 +111,14 @@ class EntitySpawn implements Listener { @EventHandler void onEntityAddToWorld(EntityAddToWorldEvent event) { if (event.getEntityType() != EntityType.PLAYER) { - final Entity entity = event.getEntity(); - final int count = entity.getLocation().getChunk().getEntities().length; + if (event.getEntity().getLocation().isGenerated() == true && + event.getEntity().getLocation().isChunkLoaded() == true) { + final Entity entity = event.getEntity(); + final int count = entity.getLocation().getChunk().getEntities().length; - if (count > 50) { - entity.remove(); + if (count > 50) { + entity.remove(); + } } } } @@ -123,10 +126,13 @@ class EntitySpawn implements Listener { @EventHandler void onEntitySpawn(EntitySpawnEvent event) { if (event.getEntityType() != EntityType.PLAYER) { - final int entityCount = event.getLocation().getChunk().getEntities().length; + if (event.getLocation().isGenerated() == true && + event.getLocation().isChunkLoaded() == true) { + final int entityCount = event.getLocation().getChunk().getEntities().length; - if (entityCount > 50) { - event.setCancelled(true); + if (entityCount > 50) { + event.setCancelled(true); + } } } } @@ -143,12 +149,17 @@ class EntitySpawn implements Listener { @EventHandler void onPreCreatureSpawn(PreCreatureSpawnEvent event) { if (event.getType() != EntityType.PLAYER) { - final int entityCount = event.getSpawnLocation().getChunk().getEntities().length; + if (event.getSpawnLocation().isGenerated() == true && + event.getSpawnLocation().isChunkLoaded() == true) { + final int entityCount = event.getSpawnLocation().getChunk().getEntities().length; - if (entityCount > 50) { - event.setCancelled(true); - return; - } else if (event.getType() == EntityType.ENDER_DRAGON) { + if (entityCount > 50) { + event.setCancelled(true); + return; + } + } + + if (event.getType() == EntityType.ENDER_DRAGON) { final int dragonCount = event.getSpawnLocation().getWorld().getEntitiesByClass(EnderDragon.class).size(); if (dragonCount > 25) {