Reformatted and optimized code

This commit is contained in:
Artuto 2020-02-19 21:19:08 -06:00
parent b2d4911102
commit 7492c5dd42
No known key found for this signature in database
GPG Key ID: 2E1D2D5383203954

View File

@ -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<Entity> entityList = entity.getNearbyEntities((double) this.range, 255.0d, (double) this.range);
private boolean entityLimit(Entity entity, int limit)
{
List<Entity> 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;
}
}