Improve stacking task speed

This commit is contained in:
ceze88 2023-04-07 21:44:08 +02:00
parent 732062fb61
commit bc25500bce
2 changed files with 265 additions and 261 deletions

View File

@ -14,6 +14,7 @@ import com.songoda.ultimatestacker.stackable.entity.custom.CustomEntity;
import com.songoda.ultimatestacker.utils.Async; import com.songoda.ultimatestacker.utils.Async;
import com.songoda.ultimatestacker.utils.CachedChunk; import com.songoda.ultimatestacker.utils.CachedChunk;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location; import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
@ -78,8 +79,6 @@ public class StackingTask extends TimerTask {
private final ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile(); private final ConfigurationSection configurationSection = UltimateStacker.getInstance().getMobFile();
private final List<UUID> processed = new ArrayList<>(); private final List<UUID> processed = new ArrayList<>();
private final Map<CachedChunk, Entity[]> cachedChunks = new HashMap<>();
private final Map<EntityType, Integer> entityStackSizes = new HashMap<>(); private final Map<EntityType, Integer> entityStackSizes = new HashMap<>();
private final int maxEntityStackSize = Settings.MAX_STACK_ENTITIES.getInt(), private final int maxEntityStackSize = Settings.MAX_STACK_ENTITIES.getInt(),
minEntityStackSize = Settings.MIN_STACK_ENTITIES.getInt(), minEntityStackSize = Settings.MIN_STACK_ENTITIES.getInt(),
@ -134,6 +133,7 @@ public class StackingTask extends TimerTask {
} }
Collections.reverse(entities); Collections.reverse(entities);
Bukkit.getScheduler().runTask(plugin, () -> {
// Loop through the entities. // Loop through the entities.
for (LivingEntity entity : entities) { for (LivingEntity entity : entities) {
// Make sure our entity has not already been processed. // Make sure our entity has not already been processed.
@ -151,10 +151,10 @@ public class StackingTask extends TimerTask {
// Process the entity. // Process the entity.
this.processEntity(entity, sWorld, location); this.processEntity(entity, sWorld, location);
} }
});
} }
// Clear caches in preparation for the next run. // Clear caches in preparation for the next run.
this.processed.clear(); this.processed.clear();
this.cachedChunks.clear();
} catch (Exception ignored) {} } catch (Exception ignored) {}
} }
@ -167,8 +167,7 @@ public class StackingTask extends TimerTask {
private Future<Entity[]> getEntitiesInChunkSync(CachedChunk cachedChunk) { private Future<Entity[]> getEntitiesInChunkSync(CachedChunk cachedChunk) {
CompletableFuture<Entity[]> future = new CompletableFuture<>(); CompletableFuture<Entity[]> future = new CompletableFuture<>();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> future.complete(cachedChunk.getEntities())); Bukkit.getScheduler().runTask(this.plugin, () -> future.complete(cachedChunk.getEntities()));
return future; return future;
} }
@ -255,7 +254,8 @@ public class StackingTask extends TimerTask {
// 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 = new LinkedList<>(); List<LivingEntity> stackableFriends = new LinkedList<>();
for (LivingEntity entity : getSimilarEntitiesAroundEntity(baseEntity, sWorld, location)) { List<LivingEntity> list = getSimilarEntitiesAroundEntity(baseEntity, sWorld, location);
for (LivingEntity entity : list) {
// Check to see if entity is not stackable. // Check to see if entity is not stackable.
if (!isEntityStackable(entity)) if (!isEntityStackable(entity))
continue; continue;
@ -358,7 +358,7 @@ public class StackingTask extends TimerTask {
try { try {
Set<CachedChunk> chunks = getNearbyChunks(new SWorld(entity.getWorld()), entity.getLocation(), radius, singleChunk); Set<CachedChunk> chunks = getNearbyChunks(new SWorld(entity.getWorld()), entity.getLocation(), radius, singleChunk);
for (CachedChunk chunk : chunks) { for (CachedChunk chunk : chunks) {
Entity[] entityList = getEntitiesInChunkSync(chunk).get(); Entity[] entityList = chunk.getEntities();
for (Entity e : entityList) { for (Entity e : entityList) {
if (!processed.contains(e.getUniqueId()) && e.getType() == entity.getType() && e instanceof LivingEntity && e.isValid() && e.getLocation().distance(entity.getLocation()) <= radius) { if (!processed.contains(e.getUniqueId()) && e.getType() == entity.getType() && e instanceof LivingEntity && e.isValid() && e.getLocation().distance(entity.getLocation()) <= radius) {
entities.add((LivingEntity) e); entities.add((LivingEntity) e);
@ -373,6 +373,7 @@ public class StackingTask extends TimerTask {
} }
public List<LivingEntity> getSimilarEntitiesAroundEntity(LivingEntity initialEntity, SWorld sWorld, Location location) { public List<LivingEntity> getSimilarEntitiesAroundEntity(LivingEntity initialEntity, SWorld sWorld, Location location) {
try {
// 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 = new LinkedList<>(getFriendlyStacksNearby(initialEntity, searchRadius, stackWholeChunk)); List<LivingEntity> entityList = new LinkedList<>(getFriendlyStacksNearby(initialEntity, searchRadius, stackWholeChunk));
@ -623,8 +624,11 @@ public class StackingTask extends TimerTask {
if (initialEntity.hasMetadata("breedCooldown")) { if (initialEntity.hasMetadata("breedCooldown")) {
entityList.removeIf(entity -> !entity.hasMetadata("breedCooldown")); entityList.removeIf(entity -> !entity.hasMetadata("breedCooldown"));
} }
return entityList; return entityList;
} catch (Exception ex) {
ex.printStackTrace();
}
return new ArrayList<>();
} }
public boolean isEquipped(LivingEntity initialEntity) { public boolean isEquipped(LivingEntity initialEntity) {

View File

@ -49,7 +49,7 @@ public class CachedChunk {
return new Entity[0]; return new Entity[0];
} }
Chunk chunk = getChunk(); Chunk chunk = getChunk();
return chunk == null ? new Entity[0] : sWorld.getEntitiesFromChunk(x, z); return chunk == null ? new Entity[0] : chunk.getEntities();
} }
@Override @Override