From d63c29c7f314c4c435e5c4604da1d1d8eebd304e Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 7 Aug 2019 14:24:49 -0400 Subject: [PATCH] Added per chunk per type stack limits. --- .../ultimatestacker/tasks/StackingTask.java | 12 ++- .../ultimatestacker/utils/EntityUtils.java | 77 +++++++++++-------- .../utils/settings/Setting.java | 3 + 3 files changed, 58 insertions(+), 34 deletions(-) diff --git a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java index dffbfce..d2b2bd6 100644 --- a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java +++ b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java @@ -31,6 +31,8 @@ public class StackingTask extends BukkitRunnable { private int maxEntityStackSize = Setting.MAX_STACK_ENTITIES.getInt(); private int minEntityStackSize = Setting.MIN_STACK_ENTITIES.getInt(); + private int minPerTypeStacksPerChunk = Setting.MIN_PER_TYPE_STACKS_PER_CHUNK.getInt(); + public StackingTask(UltimateStacker plugin) { this.plugin = plugin; this.stackManager = plugin.getEntityStackManager(); @@ -117,6 +119,15 @@ public class StackingTask extends BukkitRunnable { // Is this entity stacked? boolean isStack = stack != null; + if (isStack && minPerTypeStacksPerChunk != -1) { + if (plugin.getEntityUtils().getSimilarStacksInChunk(livingEntity) > minPerTypeStacksPerChunk) { + stack.setAmount(1); + livingEntity.remove(); + this.processed.add(livingEntity.getUniqueId()); + return; + } + } + // The amount that is stackable. int amountToStack = isStack ? stack.getAmount() : 1; @@ -280,5 +291,4 @@ public class StackingTask extends BukkitRunnable { maxEntityStackSize = configurationSection.getInt("Mobs." + initialEntity.getType().name() + ".Max Stack Size"); return maxEntityStackSize; } - } diff --git a/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java b/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java index a33e11d..e90874e 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java @@ -14,6 +14,8 @@ import java.util.stream.Collectors; public class EntityUtils { + UltimateStacker plugin = UltimateStacker.getInstance(); + private List checks = Setting.STACK_CHECKS.getStringList(); private boolean stackFlyingDown = Setting.ONLY_STACK_FLYING_DOWN.getBoolean(); private boolean keepFire = Setting.KEEP_FIRE.getBoolean(); @@ -28,7 +30,7 @@ public class EntityUtils { } - private Set getNearbyChunks(Location location, double radius) { + private Set getNearbyChunks(Location location, double radius, boolean singleChunk) { World world = location.getWorld(); Set chunks = new HashSet<>(); if (world == null) return chunks; @@ -36,7 +38,7 @@ public class EntityUtils { Chunk firstChunk = location.getChunk(); chunks.add(new CachedChunk(firstChunk)); - if (stackWholeChunk) return chunks; + if (singleChunk) return chunks; int minX = (int) Math.floor(((location.getX() - radius) - 2.0D) / 16.0D); int maxX = (int) Math.floor(((location.getX() + radius) + 2.0D) / 16.0D); @@ -52,9 +54,9 @@ public class EntityUtils { return chunks; } - private List getNearbyEntities(Location location, double radius) { + private List getNearbyEntities(Location location, double radius, boolean singleChunk) { List entities = new ArrayList<>(); - for (CachedChunk chunk : getNearbyChunks(location, radius)) { + for (CachedChunk chunk : getNearbyChunks(location, radius, singleChunk)) { Entity[] entityArray; if (cachedChunks.containsKey(chunk)) { entityArray = cachedChunks.get(chunk); @@ -65,13 +67,22 @@ public class EntityUtils { for (Entity e : entityArray) { if (e.getWorld() != location.getWorld() || !(e instanceof LivingEntity) - || (!stackWholeChunk && location.distanceSquared(e.getLocation()) >= radius * radius)) continue; + || (!singleChunk && location.distanceSquared(e.getLocation()) >= radius * radius)) continue; entities.add((LivingEntity) e); } } return entities; } + public int getSimilarStacksInChunk(LivingEntity entity) { + int count = 0; + for (LivingEntity e : getNearbyEntities(entity.getLocation(), -1, true)) { + if (plugin.getEntityStackManager().isStacked(e)) + count++; + } + return count; + } + public LivingEntity newEntity(LivingEntity toClone) { LivingEntity newEntity = (LivingEntity) toClone.getWorld().spawnEntity(toClone.getLocation(), toClone.getType()); newEntity.setVelocity(toClone.getVelocity()); @@ -85,7 +96,7 @@ public class EntityUtils { break; } case NERFED: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) break; + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_9)) break; if (!toClone.hasAI()) newEntity.setAI(false); } case IS_TAMED: { @@ -98,7 +109,7 @@ public class EntityUtils { } case SKELETON_TYPE: { if (!(toClone instanceof Skeleton) - || UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_12)) break; + || plugin.isServerVersionAtLeast(ServerVersion.V1_12)) break; ((Skeleton) newEntity).setSkeletonType(((Skeleton) toClone).getSkeletonType()); break; } @@ -113,13 +124,13 @@ public class EntityUtils { break; } case LLAMA_COLOR: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_11) || !(toClone instanceof Llama)) break; ((Llama) newEntity).setColor(((Llama) toClone).getColor()); break; } case LLAMA_STRENGTH: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_11) || !(toClone instanceof Llama)) break; ((Llama) newEntity).setStrength(((Llama) toClone).getStrength()); break; @@ -135,7 +146,7 @@ public class EntityUtils { break; } case HORSE_JUMP: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_11) || !(toClone instanceof AbstractHorse)) break; ((AbstractHorse) newEntity).setJumpStrength(((AbstractHorse) toClone).getJumpStrength()); break; @@ -162,11 +173,11 @@ public class EntityUtils { } case OCELOT_TYPE: { if (!(toClone instanceof Ocelot) - || UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_14)) break; + || plugin.isServerVersionAtLeast(ServerVersion.V1_14)) break; ((Ocelot) newEntity).setCatType(((Ocelot) toClone).getCatType()); } case CAT_TYPE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_14) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_14) || !(toClone instanceof Cat)) break; ((Cat) newEntity).setCatType(((Cat) toClone).getCatType()); break; @@ -177,37 +188,37 @@ public class EntityUtils { break; } case PARROT_TYPE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_12) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_12) || !(toClone instanceof Parrot)) break; ((Parrot) newEntity).setVariant(((Parrot) toClone).getVariant()); break; } case PUFFERFISH_STATE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(toClone instanceof PufferFish)) break; ((PufferFish) newEntity).setPuffState(((PufferFish) toClone).getPuffState()); break; } case TROPICALFISH_PATTERN: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(toClone instanceof TropicalFish)) break; ((TropicalFish) newEntity).setPattern(((TropicalFish) toClone).getPattern()); break; } case TROPICALFISH_PATTERN_COLOR: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(toClone instanceof TropicalFish)) break; ((TropicalFish) newEntity).setPatternColor(((TropicalFish) toClone).getPatternColor()); break; } case TROPICALFISH_BODY_COLOR: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(toClone instanceof TropicalFish)) break; ((TropicalFish) newEntity).setBodyColor(((TropicalFish) toClone).getBodyColor()); break; } case PHANTOM_SIZE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(toClone instanceof Phantom)) break; ((Phantom) newEntity).setSize(((Phantom) toClone).getSize()); break; @@ -225,7 +236,7 @@ public class EntityUtils { public List getSimilarEntitiesAroundEntity(LivingEntity initalEntity, Location location) { // Create a list of all entities around the initial entity of the same type. - List entityList = getNearbyEntities(location, searchRadius) + List entityList = getNearbyEntities(location, searchRadius, stackWholeChunk) .stream().filter(entity -> entity.getType() == initalEntity.getType() && entity != initalEntity) .collect(Collectors.toCollection(LinkedList::new)); @@ -250,7 +261,7 @@ public class EntityUtils { break; } case NERFED: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_9)) break; + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_9)) break; entityList.removeIf(entity -> entity.hasAI() != initalEntity.hasAI()); } case IS_TAMED: { @@ -294,14 +305,14 @@ public class EntityUtils { break; } case LLAMA_COLOR: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_11) || !(initalEntity instanceof Llama)) break; Llama llama = ((Llama) initalEntity); entityList.removeIf(entity -> ((Llama) entity).getColor() != llama.getColor()); break; } case LLAMA_STRENGTH: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_11) || !(initalEntity instanceof Llama)) break; Llama llama = ((Llama) initalEntity); entityList.removeIf(entity -> ((Llama) entity).getStrength() != llama.getStrength()); @@ -320,7 +331,7 @@ public class EntityUtils { break; } case HORSE_CARRYING_CHEST: { - if (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11)) { + if (plugin.isServerVersionAtLeast(ServerVersion.V1_11)) { if (!(initalEntity instanceof ChestedHorse)) break; entityList.removeIf(entity -> ((ChestedHorse) entity).isCarryingChest()); } else { @@ -335,7 +346,7 @@ public class EntityUtils { break; } case HORSE_HAS_SADDLE: { - if (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (plugin.isServerVersionAtLeast(ServerVersion.V1_13) && initalEntity instanceof AbstractHorse) { entityList.removeIf(entity -> ((AbstractHorse) entity).getInventory().getSaddle() != null); break; @@ -345,7 +356,7 @@ public class EntityUtils { break; } case HORSE_JUMP: { - if (UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_11)) { + if (plugin.isServerVersionAtLeast(ServerVersion.V1_11)) { if (!(initalEntity instanceof AbstractHorse)) break; AbstractHorse horse = ((AbstractHorse) initalEntity); entityList.removeIf(entity -> ((AbstractHorse) entity).getJumpStrength() != horse.getJumpStrength()); @@ -387,7 +398,7 @@ public class EntityUtils { entityList.removeIf(entity -> ((Ocelot) entity).getCatType() != ocelot.getCatType()); } case CAT_TYPE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_14) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_14) || !(initalEntity instanceof Cat)) break; Cat cat = (Cat) initalEntity; entityList.removeIf(entity -> ((Cat) entity).getCatType() != cat.getCatType()); @@ -400,42 +411,42 @@ public class EntityUtils { break; } case PARROT_TYPE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_12) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_12) || !(initalEntity instanceof Parrot)) break; Parrot parrot = (Parrot) initalEntity; entityList.removeIf(entity -> ((Parrot) entity).getVariant() != parrot.getVariant()); break; } case PUFFERFISH_STATE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(initalEntity instanceof PufferFish)) break; PufferFish pufferFish = (PufferFish) initalEntity; entityList.removeIf(entity -> ((PufferFish) entity).getPuffState() != pufferFish.getPuffState()); break; } case TROPICALFISH_PATTERN: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(initalEntity instanceof TropicalFish)) break; TropicalFish tropicalFish = (TropicalFish) initalEntity; entityList.removeIf(entity -> ((TropicalFish) entity).getPattern() != tropicalFish.getPattern()); break; } case TROPICALFISH_PATTERN_COLOR: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(initalEntity instanceof TropicalFish)) break; TropicalFish tropicalFish = (TropicalFish) initalEntity; entityList.removeIf(entity -> ((TropicalFish) entity).getPatternColor() != tropicalFish.getPatternColor()); break; } case TROPICALFISH_BODY_COLOR: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(initalEntity instanceof TropicalFish)) break; TropicalFish tropicalFish = (TropicalFish) initalEntity; entityList.removeIf(entity -> ((TropicalFish) entity).getBodyColor() != tropicalFish.getBodyColor()); break; } case PHANTOM_SIZE: { - if (!UltimateStacker.getInstance().isServerVersionAtLeast(ServerVersion.V1_13) + if (!plugin.isServerVersionAtLeast(ServerVersion.V1_13) || !(initalEntity instanceof Phantom)) break; Phantom phantom = (Phantom) initalEntity; entityList.removeIf(entity -> ((Phantom) entity).getSize() != phantom.getSize()); @@ -452,7 +463,7 @@ public class EntityUtils { } public void splitFromStack(LivingEntity entity) { - UltimateStacker instance = UltimateStacker.getInstance(); + UltimateStacker instance = plugin; EntityStack stack = instance.getEntityStackManager().getStack(entity); if (stack.getAmount() <= 1) return; diff --git a/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java b/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java index 85881b9..0d47e78 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/settings/Setting.java @@ -34,6 +34,9 @@ public enum Setting { "The minimum amount required before a stack can be formed.", "Do not set this to lower than 2."), + MIN_PER_TYPE_STACKS_PER_CHUNK("Entities.Min Per Type Stacks Per Chunk", -1, + "The minimum amount of each entity type stack allowed in a chunk."), + STACK_WHOLE_CHUNK("Entities.Stack Whole Chunk", false, "Should all qualifying entities in each chunk be stacked?", "This will override the stacking radius."),