Reformatted and optimized code

This commit is contained in:
Artuto 2020-02-19 21:19:08 -06:00
parent 40fe9bb8d9
commit d980635cc3

View File

@ -1,58 +1,86 @@
package net.teamonster.tealimit; package net.teamonster.tealimit;
import java.util.List;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType; import org.bukkit.entity.EntityType;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener; import org.bukkit.event.Listener;
import org.bukkit.event.entity.CreatureSpawnEvent; import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.plugin.java.JavaPlugin; import org.bukkit.plugin.java.JavaPlugin;
public class Main extends JavaPlugin implements Listener { import java.util.List;
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");
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); getServer().getPluginManager().registerEvents(this, this);
getConfig().options().copyDefaults(true); getConfig().options().copyDefaults(true);
saveConfig(); 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 @EventHandler
public void onEntitySpawn(CreatureSpawnEvent e) { public void onCreatureSpawn(CreatureSpawnEvent event)
if (e.getSpawnReason().equals(SpawnReason.BREEDING) || e.getSpawnReason().equals(SpawnReason.EGG) || e.getSpawnReason().equals(SpawnReason.DISPENSE_EGG)) { {
if (entityBreedLimit(e.getEntity(), this.breedLimit)) { switch(event.getSpawnReason())
e.setCancelled(true); {
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)) { case NATURAL:
e.setCancelled(true); case NETHER_PORTAL:
{
if(entityLimit(event.getEntity(), this.naturalLimit))
event.setCancelled(true);
break;
} }
} else if (e.getSpawnReason().equals(SpawnReason.SPAWNER)) {
if (entityBreedLimit(e.getEntity(), this.spawnerLimit)) { case SPAWNER:
e.setCancelled(true); {
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_EGG) && entityBreedLimit(e.getEntity(), this.spawnEggLimit)) {
e.setCancelled(true);
} }
} }
public boolean entityBreedLimit(Entity entity, int limit) { private boolean entityLimit(Entity entity, int limit)
List<Entity> entityList = entity.getNearbyEntities((double) this.range, 255.0d, (double) this.range); {
List<Entity> entityList = entity.getNearbyEntities(this.range, 255.0d, this.range);
EntityType entityType = entity.getType(); EntityType entityType = entity.getType();
int count = 0; 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++; count++;
}
} }
if (count > limit) {
return true; return count > limit;
}
return false;
} }
} }