More performance boosts.

This commit is contained in:
Brianna 2019-08-01 13:47:20 -04:00
parent 048eb55d97
commit 1ef0f01f1b
4 changed files with 117 additions and 46 deletions

View File

@ -15,10 +15,7 @@ import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.scheduler.BukkitRunnable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.UUID;
import java.util.*;
import java.util.stream.Collectors;
public class StackingTask extends BukkitRunnable {
@ -74,7 +71,9 @@ public class StackingTask extends BukkitRunnable {
}
}
processed.clear();
// Clear caches in preparation for the next run.
this.processed.clear();
plugin.getEntityUtils().clearChunkCache();
}
public boolean isWorldDisabled(World world) {
@ -134,7 +133,8 @@ public class StackingTask extends BukkitRunnable {
// Get similar entities around our entity and make sure those entities are both compatible and stackable.
List<LivingEntity> stackableFriends = plugin.getEntityUtils().getSimilarEntitiesAroundEntity(livingEntity, location)
.stream().filter(entity -> isEntityStackable(entity, location)).collect(Collectors.toList());
.stream().filter(entity -> isEntityStackable(entity, location))
.collect(Collectors.toCollection(LinkedList::new));
// Loop through our similar stackable entities.
for (LivingEntity entity : stackableFriends) {

View File

@ -0,0 +1,60 @@
package com.songoda.ultimatestacker.utils;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.World;
import java.util.Objects;
public class CachedChunk {
private final String world;
private final int x;
private final int z;
public CachedChunk(Chunk chunk) {
this(chunk.getWorld().getName(), chunk.getX(), chunk.getZ());
}
public CachedChunk(String world, int x, int z) {
this.world = world;
this.x = x;
this.z = z;
}
public String getWorld() {
return this.world;
}
public int getX() {
return this.x;
}
public int getZ() {
return this.z;
}
public Chunk getChunk() {
World world = Bukkit.getWorld(this.world);
if (world == null)
return null;
return world.getChunkAt(this.x, this.z);
}
@Override
public boolean equals(Object o) {
if (o instanceof Chunk) {
Chunk other = (Chunk) o;
return this.world.equals(other.getWorld().getName()) && this.x == other.getX() && this.z == other.getZ();
} else if (o instanceof CachedChunk) {
CachedChunk other = (CachedChunk) o;
return this.world.equals(other.getWorld()) && this.x == other.getX() && this.z == other.getZ();
} else return false;
}
@Override
public int hashCode() {
return Objects.hash(this.world, this.x, this.z);
}
}

View File

@ -1,6 +1,5 @@
package com.songoda.ultimatestacker.utils;
import com.google.common.collect.Lists;
import com.songoda.ultimatestacker.UltimateStacker;
import com.songoda.ultimatestacker.entity.Check;
import com.songoda.ultimatestacker.entity.EntityStack;
@ -10,9 +9,7 @@ import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.*;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
public class EntityUtils {
@ -23,6 +20,53 @@ public class EntityUtils {
private boolean keepPotion = Setting.KEEP_POTION.getBoolean();
private int searchRadius = Setting.SEARCH_RADIUS.getInt();
private final Map<CachedChunk, Entity[]> cachedChunks = new HashMap<>();
public void clearChunkCache() {
this.cachedChunks.clear();
}
private Set<CachedChunk> getNearbyChunks(Location location, double radius) {
World world = location.getWorld();
Set<CachedChunk> chunks = new HashSet<>();
if (world == null) return chunks;
Chunk firstChunk = location.getChunk();
chunks.add(new CachedChunk(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;
chunks.add(new CachedChunk(world.getName(), x, z));
}
}
return chunks;
}
private List<LivingEntity> getNearbyEntities(Location location, double radius) {
List<LivingEntity> entities = new ArrayList<>();
for (CachedChunk chunk : getNearbyChunks(location, radius)) {
Entity[] entityArray;
if (cachedChunks.containsKey(chunk)) {
entityArray = cachedChunks.get(chunk);
} else {
entityArray = chunk.getChunk().getEntities();
cachedChunks.put(chunk, entityArray);
}
for (Entity e : entityArray) {
if (!(e instanceof LivingEntity)
|| location.distance(e.getLocation()) >= radius) continue;
entities.add((LivingEntity) e);
}
}
return entities;
}
public LivingEntity newEntity(LivingEntity toClone) {
LivingEntity newEntity = (LivingEntity) toClone.getWorld().spawnEntity(toClone.getLocation(), toClone.getType());
newEntity.setVelocity(toClone.getVelocity());
@ -174,40 +218,6 @@ public class EntityUtils {
return newEntity;
}
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.
List<LivingEntity> entityList = getNearbyEntities(location, searchRadius)

View File

@ -66,10 +66,11 @@ public class Methods {
}
public static int getActualItemAmount(Item item) {
if (item.getItemStack().getAmount() >= 32 && item.hasMetadata("US_AMT")) {
int amount = item.getItemStack().getAmount();
if (amount >= 32 && item.hasMetadata("US_AMT")) {
return item.getMetadata("US_AMT").get(0).asInt();
} else {
return item.getItemStack().getAmount();
return amount;
}
}