From d980635cc315db7f1377e895e9215db1a09a80dd Mon Sep 17 00:00:00 2001 From: Artuto Date: Wed, 19 Feb 2020 21:19:08 -0600 Subject: [PATCH] Reformatted and optimized code --- .../java/net/teamonster/tealimit/Main.java | 90 ++++++++++++------- 1 file changed, 59 insertions(+), 31 deletions(-) diff --git a/src/main/java/net/teamonster/tealimit/Main.java b/src/main/java/net/teamonster/tealimit/Main.java index 807208f..c257f67 100644 --- a/src/main/java/net/teamonster/tealimit/Main.java +++ b/src/main/java/net/teamonster/tealimit/Main.java @@ -1,58 +1,86 @@ package net.teamonster.tealimit; -import java.util.List; import org.bukkit.entity.Entity; import org.bukkit.entity.EntityType; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; import org.bukkit.event.entity.CreatureSpawnEvent; -import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason; import org.bukkit.plugin.java.JavaPlugin; -public class Main extends JavaPlugin implements Listener { - int breedLimit = getConfig().getInt("breed-limit"); - int naturalLimit = getConfig().getInt("natural-limit"); - int range = getConfig().getInt("range"); - int spawnEggLimit = getConfig().getInt("spawnegg-limit"); - int spawnerLimit = getConfig().getInt("spawner-limit"); +import java.util.List; - public void onEnable() { +public class Main extends JavaPlugin implements Listener +{ + private int breedLimit; + private int naturalLimit; + private int range; + private int spawnEggLimit; + private int spawnerLimit; + + @Override + public void onEnable() + { getServer().getPluginManager().registerEvents(this, this); getConfig().options().copyDefaults(true); saveConfig(); + + this.breedLimit = getConfig().getInt("breed-limit"); + this.naturalLimit = getConfig().getInt("natural-limit"); + this.range = getConfig().getInt("range"); + this.spawnEggLimit = getConfig().getInt("spawnegg-limit"); + this.spawnerLimit = getConfig().getInt("spawner-limit"); } @EventHandler - public void onEntitySpawn(CreatureSpawnEvent e) { - if (e.getSpawnReason().equals(SpawnReason.BREEDING) || e.getSpawnReason().equals(SpawnReason.EGG) || e.getSpawnReason().equals(SpawnReason.DISPENSE_EGG)) { - if (entityBreedLimit(e.getEntity(), this.breedLimit)) { - e.setCancelled(true); + public void onCreatureSpawn(CreatureSpawnEvent event) + { + switch(event.getSpawnReason()) + { + case BREEDING: + case EGG: + case DISPENSE_EGG: + { + if(entityLimit(event.getEntity(), this.breedLimit)) + event.setCancelled(true); + break; } - } else if (e.getSpawnReason().equals(SpawnReason.NATURAL) || e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) { - if (entityBreedLimit(e.getEntity(), this.naturalLimit)) { - e.setCancelled(true); + + case NATURAL: + case NETHER_PORTAL: + { + if(entityLimit(event.getEntity(), this.naturalLimit)) + event.setCancelled(true); + break; + } + + case SPAWNER: + { + if(entityLimit(event.getEntity(), this.spawnerLimit)) + event.setCancelled(true); + break; + } + + case SPAWNER_EGG: + { + if(entityLimit(event.getEntity(), this.spawnEggLimit)) + event.setCancelled(true); + break; } - } else if (e.getSpawnReason().equals(SpawnReason.SPAWNER)) { - if (entityBreedLimit(e.getEntity(), this.spawnerLimit)) { - e.setCancelled(true); - } - } else if (e.getSpawnReason().equals(SpawnReason.SPAWNER_EGG) && entityBreedLimit(e.getEntity(), this.spawnEggLimit)) { - e.setCancelled(true); } } - public boolean entityBreedLimit(Entity entity, int limit) { - List entityList = entity.getNearbyEntities((double) this.range, 255.0d, (double) this.range); + private boolean entityLimit(Entity entity, int limit) + { + List entityList = entity.getNearbyEntities(this.range, 255.0d, this.range); EntityType entityType = entity.getType(); int count = 0; - for (int c = 0; c < entityList.size(); c++) { - if (((Entity) entityList.get(c)).getType() == entityType) { + + for(Entity value : entityList) + { + if(value.getType() == entityType) count++; - } } - if (count > limit) { - return true; - } - return false; + + return count > limit; } }