From 9e38374cb12d5717a6744dd107fc5fd5e88f7823 Mon Sep 17 00:00:00 2001 From: Brianna Date: Wed, 31 Jul 2019 15:02:05 -0400 Subject: [PATCH] Test boost --- .../ultimatestacker/tasks/StackingTask.java | 19 ++++---- .../ultimatestacker/utils/EntityUtils.java | 47 ++++++++++++++++--- 2 files changed, 51 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java index b0fc163..2e6247d 100644 --- a/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java +++ b/src/main/java/com/songoda/ultimatestacker/tasks/StackingTask.java @@ -6,6 +6,7 @@ import com.songoda.ultimatestacker.entity.EntityStackManager; import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.settings.Setting; import org.bukkit.Bukkit; +import org.bukkit.Location; import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.ArmorStand; @@ -60,8 +61,10 @@ public class StackingTask extends BukkitRunnable { // Loop through the entities. for (Entity entity : entities) { + // Get entity location to pass around as its faster this way. + Location location = entity.getLocation(); // Check to see if entity is stackable. - if (!isEntityStackable(entity)) continue; + if (!isEntityStackable(entity, location)) continue; // Make sure our entity has not already been processed. // Skip it if it has been. if (this.processed.contains(entity.getUniqueId())) continue; @@ -70,14 +73,14 @@ public class StackingTask extends BukkitRunnable { LivingEntity livingEntity = (LivingEntity) entity; // Process the entity. - this.processEntity(livingEntity); + this.processEntity(livingEntity, location); } } processed.clear(); } - private boolean isEntityStackable(Entity entity) { + private boolean isEntityStackable(Entity entity, Location location) { // Make sure we have the correct entity type and that it is valid. if (!entity.isValid() || !(entity instanceof LivingEntity) @@ -101,11 +104,11 @@ public class StackingTask extends BukkitRunnable { // If only stack on surface is enabled make sure the entity is on a surface then entity is stackable. return !Setting.ONLY_STACK_ON_SURFACE.getBoolean() || Methods.canFly(livingEntity) - || (livingEntity.isOnGround() || livingEntity.getLocation().getBlock().isLiquid()); + || (livingEntity.isOnGround() || location.getBlock().isLiquid()); } - private void processEntity(LivingEntity livingEntity) { + private void processEntity(LivingEntity livingEntity, Location location) { // Get the stack from the entity. It should be noted that this value will // be null if our entity is not a stack. EntityStack stack = plugin.getEntityStackManager().getStack(livingEntity); @@ -128,8 +131,8 @@ public class StackingTask extends BukkitRunnable { int maxEntityStackSize = getEntityStackSize(livingEntity); // Get similar entities around our entity and make sure those entities are both compatible and stackable. - List stackableFriends = plugin.getEntityUtils().getSimilarEntitiesAroundEntity(livingEntity) - .stream().filter(this::isEntityStackable).collect(Collectors.toList()); + List stackableFriends = plugin.getEntityUtils().getSimilarEntitiesAroundEntity(livingEntity, location) + .stream().filter(entity -> isEntityStackable(entity, location)).collect(Collectors.toList()); // Loop through our similar stackable entities. for (LivingEntity entity : stackableFriends) { @@ -173,7 +176,7 @@ public class StackingTask extends BukkitRunnable { && (stack.getAmount() + 1) <= maxEntityStackSize && Methods.canFly(entity) && Setting.ONLY_STACK_FLYING_DOWN.getBoolean() - && livingEntity.getLocation().getY() > entity.getLocation().getY()) { + && location.getY() > entity.getLocation().getY()) { // Create a new stack with the current stacks amount and add one to it. EntityStack newStack = stackManager.addStack(entity, stack.getAmount() + 1); diff --git a/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java b/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java index efb1ae3..69995bc 100644 --- a/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java +++ b/src/main/java/com/songoda/ultimatestacker/utils/EntityUtils.java @@ -5,13 +5,12 @@ import com.songoda.ultimatestacker.UltimateStacker; import com.songoda.ultimatestacker.entity.Check; import com.songoda.ultimatestacker.entity.EntityStack; import com.songoda.ultimatestacker.utils.settings.Setting; -import org.bukkit.Axis; import org.bukkit.Chunk; +import org.bukkit.Location; +import org.bukkit.World; import org.bukkit.entity.*; -import org.bukkit.util.BoundingBox; import java.util.ArrayList; -import java.util.Arrays; import java.util.LinkedList; import java.util.List; import java.util.stream.Collectors; @@ -175,11 +174,45 @@ public class EntityUtils { return newEntity; } - public List getSimilarEntitiesAroundEntity(LivingEntity initalEntity) { - //Create a list of all entities around the initial entity of the same type. - List entityList = initalEntity.getNearbyEntities(searchRadius, searchRadius, searchRadius) + private List getNearbyEntities(Location location, double radius) { + List entities = new ArrayList<>(); + for (Chunk chunk : getNearbyChunks(location, radius)) { + for (Entity e : chunk.getEntities()) { + if (!(e instanceof LivingEntity) || location.distance(e.getLocation()) >= radius) continue; + entities.add((LivingEntity) e); + } + } + return entities; + } + + private List getNearbyChunks(Location location, double radius) { + World world = location.getWorld(); + if (world == null) return new ArrayList<>(); + + Chunk firstChunk = location.getChunk(); + List chunks = Lists.newArrayList(); + chunks.add(firstChunk); + int minX = (int) Math.floor(((location.getX() - radius) - 2.0D) / 16.0D); + int maxX = (int) Math.floor(((location.getX() + radius) + 2.0D) / 16.0D); + int minZ = (int) Math.floor(((location.getZ() - radius) - 2.0D) / 16.0D); + int maxZ = (int) Math.floor(((location.getZ() + radius) + 2.0D) / 16.0D); + + for (int x = minX; x <= maxX; ++x) { + for (int z = minZ; z <= maxZ; ++z) { + if (firstChunk.getX() == x && firstChunk.getZ() == z) continue; + Chunk chunk = world.getChunkAt(x, z); + if (chunk.isLoaded() && !chunks.contains(chunk)) + chunks.add(chunk); + } + } + return chunks; + } + + 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) .stream().filter(entity -> entity.getType() == initalEntity.getType() && entity != initalEntity) - .map(entity -> (LivingEntity) entity).collect(Collectors.toList()); + .collect(Collectors.toCollection(LinkedList::new)); if (stackFlyingDown && Methods.canFly(initalEntity)) entityList.removeIf(entity -> entity.getLocation().getY() > initalEntity.getLocation().getY());