1
0
mirror of https://github.com/songoda/UltimateStacker.git synced 2025-03-25 12:59:21 +01:00

Test boost

This commit is contained in:
Brianna 2019-07-31 15:02:05 -04:00
parent 8789288f99
commit 9e38374cb1
2 changed files with 51 additions and 15 deletions
src/main/java/com/songoda/ultimatestacker

View File

@ -6,6 +6,7 @@ import com.songoda.ultimatestacker.entity.EntityStackManager;
import com.songoda.ultimatestacker.utils.Methods; import com.songoda.ultimatestacker.utils.Methods;
import com.songoda.ultimatestacker.utils.settings.Setting; import com.songoda.ultimatestacker.utils.settings.Setting;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.ArmorStand; import org.bukkit.entity.ArmorStand;
@ -60,8 +61,10 @@ public class StackingTask extends BukkitRunnable {
// Loop through the entities. // Loop through the entities.
for (Entity entity : 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. // 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. // Make sure our entity has not already been processed.
// Skip it if it has been. // Skip it if it has been.
if (this.processed.contains(entity.getUniqueId())) continue; if (this.processed.contains(entity.getUniqueId())) continue;
@ -70,14 +73,14 @@ public class StackingTask extends BukkitRunnable {
LivingEntity livingEntity = (LivingEntity) entity; LivingEntity livingEntity = (LivingEntity) entity;
// Process the entity. // Process the entity.
this.processEntity(livingEntity); this.processEntity(livingEntity, location);
} }
} }
processed.clear(); 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. // Make sure we have the correct entity type and that it is valid.
if (!entity.isValid() if (!entity.isValid()
|| !(entity instanceof LivingEntity) || !(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. // 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() return !Setting.ONLY_STACK_ON_SURFACE.getBoolean()
|| Methods.canFly(livingEntity) || 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 // Get the stack from the entity. It should be noted that this value will
// be null if our entity is not a stack. // be null if our entity is not a stack.
EntityStack stack = plugin.getEntityStackManager().getStack(livingEntity); EntityStack stack = plugin.getEntityStackManager().getStack(livingEntity);
@ -128,8 +131,8 @@ public class StackingTask extends BukkitRunnable {
int maxEntityStackSize = getEntityStackSize(livingEntity); int maxEntityStackSize = getEntityStackSize(livingEntity);
// Get similar entities around our entity and make sure those entities are both compatible and stackable. // Get similar entities around our entity and make sure those entities are both compatible and stackable.
List<LivingEntity> stackableFriends = plugin.getEntityUtils().getSimilarEntitiesAroundEntity(livingEntity) List<LivingEntity> stackableFriends = plugin.getEntityUtils().getSimilarEntitiesAroundEntity(livingEntity, location)
.stream().filter(this::isEntityStackable).collect(Collectors.toList()); .stream().filter(entity -> isEntityStackable(entity, location)).collect(Collectors.toList());
// Loop through our similar stackable entities. // Loop through our similar stackable entities.
for (LivingEntity entity : stackableFriends) { for (LivingEntity entity : stackableFriends) {
@ -173,7 +176,7 @@ public class StackingTask extends BukkitRunnable {
&& (stack.getAmount() + 1) <= maxEntityStackSize && (stack.getAmount() + 1) <= maxEntityStackSize
&& Methods.canFly(entity) && Methods.canFly(entity)
&& Setting.ONLY_STACK_FLYING_DOWN.getBoolean() && 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. // Create a new stack with the current stacks amount and add one to it.
EntityStack newStack = stackManager.addStack(entity, stack.getAmount() + 1); EntityStack newStack = stackManager.addStack(entity, stack.getAmount() + 1);

View File

@ -5,13 +5,12 @@ import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.entity.Check; import com.songoda.ultimatestacker.entity.Check;
import com.songoda.ultimatestacker.entity.EntityStack; import com.songoda.ultimatestacker.entity.EntityStack;
import com.songoda.ultimatestacker.utils.settings.Setting; import com.songoda.ultimatestacker.utils.settings.Setting;
import org.bukkit.Axis;
import org.bukkit.Chunk; import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.*; import org.bukkit.entity.*;
import org.bukkit.util.BoundingBox;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -175,11 +174,45 @@ public class EntityUtils {
return newEntity; return newEntity;
} }
public List<LivingEntity> getSimilarEntitiesAroundEntity(LivingEntity initalEntity) { private List<LivingEntity> getNearbyEntities(Location location, double radius) {
List<LivingEntity> 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<Chunk> getNearbyChunks(Location location, double radius) {
World world = location.getWorld();
if (world == null) return new ArrayList<>();
Chunk firstChunk = location.getChunk();
List<Chunk> 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<LivingEntity> getSimilarEntitiesAroundEntity(LivingEntity initalEntity, Location location) {
// Create a list of all entities around the initial entity of the same type. // Create a list of all entities around the initial entity of the same type.
List<LivingEntity> entityList = initalEntity.getNearbyEntities(searchRadius, searchRadius, searchRadius) List<LivingEntity> entityList = getNearbyEntities(location, searchRadius)
.stream().filter(entity -> entity.getType() == initalEntity.getType() && entity != initalEntity) .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)) if (stackFlyingDown && Methods.canFly(initalEntity))
entityList.removeIf(entity -> entity.getLocation().getY() > initalEntity.getLocation().getY()); entityList.removeIf(entity -> entity.getLocation().getY() > initalEntity.getLocation().getY());