TNT optimization test

This commit is contained in:
mathiascode 2020-02-28 22:43:02 +02:00
parent fa4868908b
commit 7546fa0e1e

View file

@ -18,8 +18,8 @@ import org.bukkit.entity.minecart.ExplosiveMinecart;
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.AreaEffectCloudApplyEvent; import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.bukkit.event.entity.EntityExplodeEvent;
import org.bukkit.event.entity.EntitySpawnEvent; import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.ExplosionPrimeEvent;
import org.bukkit.event.entity.SpawnerSpawnEvent; import org.bukkit.event.entity.SpawnerSpawnEvent;
import org.bukkit.event.vehicle.VehicleCreateEvent; import org.bukkit.event.vehicle.VehicleCreateEvent;
import org.bukkit.event.weather.LightningStrikeEvent; import org.bukkit.event.weather.LightningStrikeEvent;
@ -69,23 +69,41 @@ public final class EntitySpawn implements Listener {
EntitySpawnEvent's event cancel EntitySpawnEvent's event cancel
*/ */
final int chunkEntityCount = switch (entityType) {
!isAddToWorldEvent ? chunk.getEntities().length + 1 case ENDER_DRAGON:
: chunk.getEntities().length;
final int chunkEntityCountLimit = 30;
final int worldDragonCount = final int worldDragonCount =
!isAddToWorldEvent ? world.getEntitiesByClass(EnderDragon.class).size() + 1 !isAddToWorldEvent ? world.getEntitiesByClass(EnderDragon.class).size() + 1
: world.getEntitiesByClass(EnderDragon.class).size(); : world.getEntitiesByClass(EnderDragon.class).size();
final int worldDragonCountLimit = 24; final int worldDragonCountLimit = 24;
if ((!EntityType.PLAYER.equals(entityType) if (worldDragonCount >= worldDragonCountLimit) {
&& chunkEntityCount >= chunkEntityCountLimit)
|| (EntityType.ENDER_DRAGON.equals(entityType)
&& worldDragonCount >= worldDragonCountLimit)) {
return true; return true;
} }
break;
case PRIMED_TNT:
final int worldTntCount =
!isAddToWorldEvent ? world.getEntitiesByClass(TNTPrimed.class).size() + 1
: world.getEntitiesByClass(TNTPrimed.class).size();
final int worldTntCountLimit = 100;
if (worldTntCount >= worldTntCountLimit) {
return true;
}
break;
default:
if (!EntityType.PLAYER.equals(entityType)) {
final int chunkEntityCount =
!isAddToWorldEvent ? chunk.getEntities().length + 1
: chunk.getEntities().length;
final int chunkEntityCountLimit = 30;
if (chunkEntityCount >= chunkEntityCountLimit) {
return true;
}
}
break;
}
return false; return false;
} }
@ -215,7 +233,7 @@ public final class EntitySpawn implements Listener {
} }
@EventHandler @EventHandler
void onEntityExplode(final EntityExplodeEvent event) { void onExplosionPrime(final ExplosionPrimeEvent event) {
if (EntityType.MINECART_TNT.equals(event.getEntityType()) if (EntityType.MINECART_TNT.equals(event.getEntityType())
&& event.getEntity().getWorld().getEntitiesByClass(ExplosiveMinecart.class).size() > 80) { && event.getEntity().getWorld().getEntitiesByClass(ExplosiveMinecart.class).size() > 80) {
event.setCancelled(true); event.setCancelled(true);
@ -233,10 +251,9 @@ public final class EntitySpawn implements Listener {
return; return;
} }
final Entity entity = event.getEntity(); final EntityType entityType = event.getEntityType();
final EntityType entityType = entity.getType(); final Chunk chunk = event.getLocation().getChunk();
final Chunk chunk = entity.getChunk(); final World world = event.getLocation().getWorld();
final World world = entity.getWorld();
final boolean isAddToWorldEvent = false; final boolean isAddToWorldEvent = false;
if (isEntityLimitReached(entityType, chunk, world, isAddToWorldEvent)) { if (isEntityLimitReached(entityType, chunk, world, isAddToWorldEvent)) {
@ -244,6 +261,8 @@ public final class EntitySpawn implements Listener {
return; return;
} }
final Entity entity = event.getEntity();
applyEntityChanges(entity); applyEntityChanges(entity);
} }
@ -259,9 +278,9 @@ public final class EntitySpawn implements Listener {
return; return;
} }
final EntityType entityType = lightning.getType(); final EntityType entityType = EntityType.LIGHTNING;
final Chunk chunk = lightning.getChunk(); final Chunk chunk = lightning.getChunk();
final World world = lightning.getWorld(); final World world = event.getWorld();
final boolean isAddToWorldEvent = false; final boolean isAddToWorldEvent = false;
if (isEntityLimitReached(entityType, chunk, world, isAddToWorldEvent)) { if (isEntityLimitReached(entityType, chunk, world, isAddToWorldEvent)) {
@ -306,7 +325,7 @@ public final class EntitySpawn implements Listener {
case EXPLOSION: case EXPLOSION:
case FIRE: case FIRE:
case REDSTONE: case REDSTONE:
if (event.getBlock().getWorld().getEntitiesByClass(TNTPrimed.class).size() > 120) { if (event.getBlock().getWorld().getEntitiesByClass(TNTPrimed.class).size() > 100) {
event.setCancelled(true); event.setCancelled(true);
} }
return; return;