Clean up an abomination of code

This commit is contained in:
mathiascode 2019-12-02 01:10:40 +02:00
parent 0410492f76
commit b4d43d474f
2 changed files with 205 additions and 287 deletions

View file

@ -7,6 +7,7 @@ import java.util.UUID;
import org.bukkit.Material;
import org.bukkit.block.BlockFace;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.plugin.java.JavaPlugin;
import com.destroystokyo.paper.profile.PlayerProfile;
@ -23,6 +24,7 @@ public class Main extends JavaPlugin {
static HashSet<Material> nonSolidSingularBlockList = new HashSet<>();
static HashSet<Material> nonSolidWallMountedBlockList = new HashSet<>();
static HashSet<Material> nonSolidWaterBlockList = new HashSet<>();
static HashSet<SpawnReason> spawnReasonList = new HashSet<>();
public void onLoad() {
/* Fill lists */
@ -650,6 +652,15 @@ public class Main extends JavaPlugin {
this.nonSolidBlockList.addAll(nonSolidDoubleBlockList);
this.nonSolidBlockList.addAll(nonSolidSingularBlockList);
this.nonSolidBlockList.addAll(nonSolidWallMountedBlockList);
Collections.addAll(
spawnReasonList,
SpawnReason.CUSTOM,
SpawnReason.DEFAULT,
SpawnReason.DISPENSE_EGG,
SpawnReason.SPAWNER,
SpawnReason.SPAWNER_EGG
);
saveResource("config.yml", false);
}

View file

@ -2,6 +2,7 @@ package pw.kaboom.extras;
import org.bukkit.Bukkit;
import org.bukkit.DyeColor;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.World;
@ -29,7 +30,6 @@ import org.bukkit.event.block.BlockDispenseEvent;
import org.bukkit.event.entity.AreaEffectCloudApplyEvent;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.CreatureSpawnEvent.SpawnReason;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.entity.ItemSpawnEvent;
import org.bukkit.event.entity.SpawnerSpawnEvent;
@ -50,10 +50,91 @@ import com.destroystokyo.paper.event.entity.PreSpawnerSpawnEvent;
import org.bukkit.block.banner.Pattern;
class EntitySpawn implements Listener {
@EventHandler
void onAreaEffectCloudApply(AreaEffectCloudApplyEvent event) {
final AreaEffectCloud cloud = event.getEntity();
private boolean checkDragonWorldLimit(World world) {
final int dragonCount = world.getEntitiesByClass(EnderDragon.class).size();
if (dragonCount > 25) {
return true;
}
return false;
}
private boolean checkEntityChunkLimit(Location location) {
if (location.isChunkLoaded()) {
final int count = location.getChunk().getEntities().length;
if (count > 50) {
return true;
}
}
return false;
}
private boolean checkEntityWorldLimitRemove(World world) {
if (world.getEntities().size() > 1024) {
for (Entity entity : world.getEntities()) {
if (entity.getType() != EntityType.PLAYER) {
entity.remove();
}
}
return true;
}
return false;
}
private void checkIllegalArmor(LivingEntity mob) {
try {
for (ItemStack item : mob.getEquipment().getArmorContents()) {
if (checkIllegalBanner(item)) {
mob.getEquipment().setArmorContents(
new ItemStack[] {null, null, null, null}
);
}
}
} catch (Exception exception) {
mob.getEquipment().setArmorContents(
new ItemStack[] {null, null, null, null}
);
}
try {
ItemStack item = mob.getEquipment().getItemInMainHand();
if (checkIllegalBanner(item)) {
mob.getEquipment().setItemInMainHand(null);
}
} catch (Exception exception) {
mob.getEquipment().setItemInMainHand(null);
}
try {
ItemStack item = mob.getEquipment().getItemInOffHand();
if (checkIllegalBanner(item)) {
mob.getEquipment().setItemInOffHand(null);
}
} catch (Exception exception) {
mob.getEquipment().setItemInOffHand(null);
}
}
private boolean checkIllegalBanner(ItemStack item) {
if (item != null &&
item.hasItemMeta()) {
if (item.getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) item.getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
return true;
}
}
}
}
return false;
}
private void limitAreaEffectCloudRadius(AreaEffectCloud cloud) {
if (cloud.getRadius() > 40) {
cloud.setRadius(40);
}
@ -66,16 +147,50 @@ class EntitySpawn implements Listener {
cloud.setRadiusPerTick(0);
}
}
private void limitFollowAttribute(LivingEntity mob) {
final AttributeInstance followAttribute = mob.getAttribute(Attribute.GENERIC_FOLLOW_RANGE);
if (followAttribute != null &&
followAttribute.getBaseValue() > 40) {
followAttribute.setBaseValue(40);
}
}
private void limitSlimeSize(Slime slime) {
if (slime.getSize() > 50) {
slime.setSize(50);
}
}
private void limitSpawner(CreatureSpawner spawner) {
if (spawner.getSpawnedType() == EntityType.FALLING_BLOCK) {
if (spawner.getDelay() > 100) {
spawner.setMaxSpawnDelay(100);
spawner.setDelay(100);
spawner.update();
}
}
if (spawner.getSpawnCount() > 200) {
spawner.setSpawnCount(200);
spawner.update();
}
if (spawner.getSpawnRange() > 50) {
spawner.setSpawnRange(50);
spawner.update();
}
}
@EventHandler
void onAreaEffectCloudApply(AreaEffectCloudApplyEvent event) {
limitAreaEffectCloudRadius(event.getEntity());
}
@EventHandler
void onBlockDispense(BlockDispenseEvent event) {
try {
/*BlockStateMeta stateMeta = (BlockStateMeta) event.getItem().getItemMeta();
/*if (stateMeta.getBlockState() instanceof ShulkerBox) {
ShulkerBox shulkerBox = (ShulkerBox) stateMeta.getBlockState();
}
stateMeta.getBlockState();*/
event.getBlock().getState();
event.getItem().getItemMeta();
} catch (Exception exception) {
@ -85,111 +200,38 @@ class EntitySpawn implements Listener {
@EventHandler
void onCreatureSpawn(CreatureSpawnEvent event) {
if (event.getSpawnReason() == SpawnReason.CUSTOM ||
event.getSpawnReason() == SpawnReason.DEFAULT ||
event.getSpawnReason() == SpawnReason.DISPENSE_EGG ||
event.getSpawnReason() == SpawnReason.SPAWNER ||
event.getSpawnReason() == SpawnReason.SPAWNER_EGG) {
final LivingEntity mob = event.getEntity();
final AttributeInstance followAttribute = mob.getAttribute(Attribute.GENERIC_FOLLOW_RANGE);
final LivingEntity mob = event.getEntity();
if (followAttribute != null &&
followAttribute.getBaseValue() > 40) {
followAttribute.setBaseValue(40);
}
if (Main.spawnReasonList.contains(event.getSpawnReason())) {
limitFollowAttribute(mob);
}
if (event.getEntityType() == EntityType.ARMOR_STAND ||
event.getEntityType() == EntityType.DROWNED ||
event.getEntityType() == EntityType.GIANT ||
event.getEntityType() == EntityType.HUSK ||
event.getEntityType() == EntityType.PIG_ZOMBIE ||
event.getEntityType() == EntityType.PLAYER ||
event.getEntityType() == EntityType.SKELETON ||
event.getEntityType() == EntityType.STRAY ||
event.getEntityType() == EntityType.WITHER_SKELETON ||
event.getEntityType() == EntityType.ZOMBIE ||
event.getEntityType() == EntityType.ZOMBIE_VILLAGER) {
final LivingEntity mob = (LivingEntity) event.getEntity();
switch (event.getEntityType()) {
case ARMOR_STAND:
case DROWNED:
case GIANT:
case HUSK:
case PIG_ZOMBIE:
case PLAYER:
case SKELETON:
case STRAY:
case WITHER_SKELETON:
case ZOMBIE:
case ZOMBIE_VILLAGER:
checkIllegalArmor(mob);
break;
case ENDER_DRAGON:
final World world = event.getLocation().getWorld();
try {
for (ItemStack item : mob.getEquipment().getArmorContents()) {
if (item != null &&
item.hasItemMeta()) {
if (item.getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) item.getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
mob.getEquipment().setArmorContents(
new ItemStack[] {null, null, null, null}
);
}
}
}
}
if (checkDragonWorldLimit(world)) {
event.setCancelled(true);
}
} catch (Exception exception) {
mob.getEquipment().setArmorContents(
new ItemStack[] {null, null, null, null}
);
}
break;
case MAGMA_CUBE:
case SLIME:
final Slime slime = (Slime) mob;
try {
ItemStack item = mob.getEquipment().getItemInMainHand();
if (item != null &&
item.hasItemMeta()) {
if (item.getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) item.getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
mob.getEquipment().setItemInMainHand(null);
}
}
}
}
} catch (Exception exception) {
mob.getEquipment().setItemInMainHand(null);
}
try {
ItemStack item = mob.getEquipment().getItemInMainHand();
if (item != null &&
item.hasItemMeta()) {
if (mob.getEquipment().getItemInOffHand().getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) mob.getEquipment().getItemInOffHand().getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
mob.getEquipment().setItemInOffHand(null);
}
}
}
}
} catch (Exception exception) {
mob.getEquipment().setItemInOffHand(null);
}
} else if (event.getEntityType() == EntityType.ENDER_DRAGON) {
final int dragonCount = event.getLocation().getWorld().getEntitiesByClass(EnderDragon.class).size();
if (dragonCount > 25) {
event.setCancelled(true);
}
} else if (event.getEntityType() == EntityType.MAGMA_CUBE) {
final MagmaCube magmacube = (MagmaCube) event.getEntity();
if (magmacube.getSize() > 50) {
magmacube.setSize(50);
}
} else if (event.getEntityType() == EntityType.SLIME) {
final Slime slime = (Slime) event.getEntity();
if (slime.getSize() > 50) {
slime.setSize(50);
}
limitSlimeSize(slime);
}
}
@ -197,146 +239,51 @@ class EntitySpawn implements Listener {
void onEntityAddToWorld(EntityAddToWorldEvent event) {
if (event.getEntityType() != EntityType.PLAYER) {
final World world = event.getEntity().getWorld();
final Entity entity = event.getEntity();
if (world.getEntities().size() > 1024) {
for (Entity entity : world.getEntities()) {
if (entity.getType() != EntityType.PLAYER) {
entity.remove();
}
}
return;
}
if (event.getEntity().getLocation().isGenerated() &&
event.getEntity().getLocation().isChunkLoaded()) {
final Entity entity = event.getEntity();
final int count = entity.getLocation().getChunk().getEntities().length;
if (count > 50) {
if (!checkEntityWorldLimitRemove(world)) {
if (checkEntityChunkLimit(entity.getLocation())) {
entity.remove();
return;
}
}
if (event.getEntityType() == EntityType.ARMOR_STAND ||
event.getEntityType() == EntityType.DROWNED ||
event.getEntityType() == EntityType.GIANT ||
event.getEntityType() == EntityType.HUSK ||
event.getEntityType() == EntityType.PIG_ZOMBIE ||
event.getEntityType() == EntityType.PLAYER ||
event.getEntityType() == EntityType.SKELETON ||
event.getEntityType() == EntityType.STRAY ||
event.getEntityType() == EntityType.WITHER_SKELETON ||
event.getEntityType() == EntityType.ZOMBIE ||
event.getEntityType() == EntityType.ZOMBIE_VILLAGER) {
final LivingEntity mob = (LivingEntity) event.getEntity();
try {
for (ItemStack item : mob.getEquipment().getArmorContents()) {
if (item != null &&
item.hasItemMeta()) {
if (item.getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) item.getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
mob.getEquipment().setArmorContents(
new ItemStack[] {null, null, null, null}
);
}
}
}
switch (event.getEntityType()) {
case ARMOR_STAND:
case DROWNED:
case GIANT:
case HUSK:
case PIG_ZOMBIE:
case PLAYER:
case SKELETON:
case STRAY:
case WITHER_SKELETON:
case ZOMBIE:
case ZOMBIE_VILLAGER:
final LivingEntity mob = (LivingEntity) entity;
checkIllegalArmor(mob);
break;
case AREA_EFFECT_CLOUD:
final AreaEffectCloud cloud = (AreaEffectCloud) entity;
limitAreaEffectCloudRadius(cloud);
break;
case PRIMED_TNT:
if (world.getEntitiesByClass(TNTPrimed.class).size() > 180) {
for (Entity tnt : world.getEntitiesByClass(TNTPrimed.class)) {
tnt.remove();
}
}
} catch (Exception exception) {
mob.getEquipment().setArmorContents(
new ItemStack[] {null, null, null, null}
);
}
try {
ItemStack item = mob.getEquipment().getItemInMainHand();
if (item != null &&
item.hasItemMeta()) {
if (item.getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) item.getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
mob.getEquipment().setItemInMainHand(null);
}
}
}
}
} catch (Exception exception) {
mob.getEquipment().setItemInMainHand(null);
}
try {
ItemStack item = mob.getEquipment().getItemInMainHand();
if (item != null &&
item.hasItemMeta()) {
if (mob.getEquipment().getItemInOffHand().getItemMeta() instanceof BannerMeta) {
final BannerMeta banner = (BannerMeta) mob.getEquipment().getItemInOffHand().getItemMeta();
for (Pattern pattern : banner.getPatterns()) {
if (pattern.getColor() == null) {
mob.getEquipment().setItemInOffHand(null);
}
}
}
}
} catch (Exception exception) {
mob.getEquipment().setItemInOffHand(null);
}
} else if (event.getEntityType() == EntityType.AREA_EFFECT_CLOUD) {
final AreaEffectCloud cloud = (AreaEffectCloud) event.getEntity();
if (cloud.getRadius() > 40) {
cloud.setRadius(40);
}
if (cloud.getRadiusOnUse() > 0.01f) {
cloud.setRadiusOnUse(0.1f);
}
if (cloud.getRadiusPerTick() > 0) {
cloud.setRadiusPerTick(0);
}
} else if (event.getEntityType() == EntityType.PRIMED_TNT) {
if (world.getEntitiesByClass(TNTPrimed.class).size() > 180) {
for (Entity entity : world.getEntitiesByClass(TNTPrimed.class)) {
entity.remove();
}
}
}
}
}
@EventHandler
void onEntitySpawn(EntitySpawnEvent event) {
if (event.getEntityType() != EntityType.PLAYER) {
final World world = event.getLocation().getWorld();
if (world.getEntities().size() > 1024) {
for (Entity entity : world.getEntities()) {
if (entity.getType() != EntityType.PLAYER) {
entity.remove();
}
}
return;
}
if (event.getLocation().isGenerated() &&
event.getLocation().isChunkLoaded()) {
final int entityCount = event.getLocation().getChunk().getEntities().length;
if (entityCount > 50) {
event.setCancelled(true);
}
}
if (event.getEntityType() != EntityType.PLAYER &&
checkEntityChunkLimit(event.getLocation())) {
event.setCancelled(true);
}
}
@ -346,44 +293,17 @@ class EntitySpawn implements Listener {
event.getEntity().getItemStack().getItemMeta();
} catch (Exception | StackOverflowError exception) {
event.setCancelled(true);
return;
}
/*System.out.println("better work");
event.getEntity().setPickupDelay(99999);
event.getEntity().setItemStack(new ItemStack(Material.TORCH));*/
}
@EventHandler
void onPreCreatureSpawn(PreCreatureSpawnEvent event) {
if (event.getType() != EntityType.PLAYER) {
final World world = event.getSpawnLocation().getWorld();
if (world.getEntities().size() > 1024) {
for (Entity entity : world.getEntities()) {
if (entity.getType() != EntityType.PLAYER) {
entity.remove();
}
}
return;
}
if (event.getSpawnLocation().isGenerated() &&
event.getSpawnLocation().isChunkLoaded()) {
final int entityCount = event.getSpawnLocation().getChunk().getEntities().length;
if (entityCount > 50) {
event.setCancelled(true);
return;
}
}
if (event.getType() == EntityType.ENDER_DRAGON) {
final int dragonCount = event.getSpawnLocation().getWorld().getEntitiesByClass(EnderDragon.class).size();
if (dragonCount > 25) {
event.setCancelled(true);
}
}
if ((event.getType() != EntityType.PLAYER &&
checkEntityChunkLimit(event.getSpawnLocation())) ||
(event.getType() == EntityType.ENDER_DRAGON &&
checkDragonWorldLimit(event.getSpawnLocation().getWorld()))) {
event.setCancelled(true);
}
}
@ -392,26 +312,12 @@ class EntitySpawn implements Listener {
try {
final CreatureSpawner spawner = (CreatureSpawner) event.getSpawnerLocation().getBlock().getState();
if (event.getType() == EntityType.MINECART_MOB_SPAWNER) {
if (spawner.getSpawnedType() == EntityType.MINECART_MOB_SPAWNER) {
event.setCancelled(true);
return;
} else if (event.getType() == EntityType.FALLING_BLOCK) {
if (spawner.getDelay() > 100) {
spawner.setMaxSpawnDelay(100);
spawner.setDelay(100);
spawner.update();
}
}
if (spawner.getSpawnCount() > 200) {
spawner.setSpawnCount(200);
spawner.update();
}
if (spawner.getSpawnRange() > 50) {
spawner.setSpawnRange(50);
spawner.update();
}
limitSpawner(spawner);
} catch (Exception exception) {
event.setCancelled(true);
}
@ -431,9 +337,10 @@ class EntitySpawn implements Listener {
@EventHandler
void onTNTPrime(TNTPrimeEvent event) {
final double tps = Bukkit.getTPS()[0];
final int tntCount = event.getBlock().getWorld().getEntitiesByClass(TNTPrimed.class).size();
if (tps < 10 ||
event.getBlock().getWorld().getEntitiesByClass(TNTPrimed.class).size() > 140) {
tntCount > 140) {
event.setCancelled(true);
}
}