From 3641df3f7e3fe32016502598d1b1c45560dfaaaf Mon Sep 17 00:00:00 2001 From: Govindass Date: Sat, 9 May 2020 12:22:17 +0300 Subject: [PATCH] Add PreCreatureSpawnEvent --- .../java/net/teamonster/tealimit/Main.java | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/src/main/java/net/teamonster/tealimit/Main.java b/src/main/java/net/teamonster/tealimit/Main.java index c257f67..25aa725 100644 --- a/src/main/java/net/teamonster/tealimit/Main.java +++ b/src/main/java/net/teamonster/tealimit/Main.java @@ -1,5 +1,7 @@ package net.teamonster.tealimit; +import com.destroystokyo.paper.event.entity.PreCreatureSpawnEvent; +import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; @@ -7,6 +9,7 @@ import org.bukkit.event.Listener; import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.plugin.java.JavaPlugin; +import java.util.Collection; import java.util.List; public class Main extends JavaPlugin implements Listener @@ -68,6 +71,43 @@ public class Main extends JavaPlugin implements Listener } } } + @EventHandler + public void onPreCreatureSpawn(PreCreatureSpawnEvent event) + { + switch(event.getReason()) + { + case BREEDING: + case EGG: + case DISPENSE_EGG: + { + if(entityLimit(event.getSpawnLocation(), this.breedLimit, event.getType())) + event.setCancelled(true); + break; + } + + case NATURAL: + case NETHER_PORTAL: + { + if(entityLimit(event.getSpawnLocation(), this.naturalLimit, event.getType())) + event.setCancelled(true); + break; + } + + case SPAWNER: + { + if(entityLimit(event.getSpawnLocation(), this.spawnerLimit, event.getType())) + event.setCancelled(true); + break; + } + + case SPAWNER_EGG: + { + if(entityLimit(event.getSpawnLocation(), this.spawnEggLimit, event.getType())) + event.setCancelled(true); + break; + } + } + } private boolean entityLimit(Entity entity, int limit) { @@ -81,6 +121,19 @@ public class Main extends JavaPlugin implements Listener count++; } + return count > limit; + } + private boolean entityLimit(Location location, int limit, EntityType entityType) + { + Collection entityList = location.getWorld().getNearbyEntities(location, this.range, 255.0d, this.range); + int count = 0; + + for(Entity value : entityList) + { + if(value.getType() == entityType) + count++; + } + return count > limit; } }