Do entity collection in Bukkit's main thread for StackingTask

Paper-Spigot got more strict in 1.19.2 about calling
stuff async.

I do not know what the whole StackingTask does and
don't feel like cleaning it up completely is possible right now.

So I only moved the getEntities-calls to the main
thread and have the async task wait for it.
Not great but it fixed the bug.


SD-9374
SD-9377
SD-9392
SD-9401
This commit is contained in:
Christian Koop 2022-10-26 22:04:01 +02:00
parent 610d423d01
commit c6320cb141
No known key found for this signature in database
GPG Key ID: 89A8181384E010A3
1 changed files with 55 additions and 11 deletions

View File

@ -17,7 +17,32 @@ import org.bukkit.Location;
import org.bukkit.Material; import org.bukkit.Material;
import org.bukkit.World; import org.bukkit.World;
import org.bukkit.configuration.ConfigurationSection; import org.bukkit.configuration.ConfigurationSection;
import org.bukkit.entity.*; import org.bukkit.entity.AbstractHorse;
import org.bukkit.entity.Ageable;
import org.bukkit.entity.ArmorStand;
import org.bukkit.entity.Cat;
import org.bukkit.entity.ChestedHorse;
import org.bukkit.entity.Entity;
import org.bukkit.entity.EntityType;
import org.bukkit.entity.Horse;
import org.bukkit.entity.HumanEntity;
import org.bukkit.entity.LivingEntity;
import org.bukkit.entity.Llama;
import org.bukkit.entity.Ocelot;
import org.bukkit.entity.Parrot;
import org.bukkit.entity.Phantom;
import org.bukkit.entity.Pig;
import org.bukkit.entity.PufferFish;
import org.bukkit.entity.Rabbit;
import org.bukkit.entity.Sheep;
import org.bukkit.entity.Skeleton;
import org.bukkit.entity.Slime;
import org.bukkit.entity.Snowman;
import org.bukkit.entity.Tameable;
import org.bukkit.entity.TropicalFish;
import org.bukkit.entity.Villager;
import org.bukkit.entity.Wolf;
import org.bukkit.entity.Zombie;
import org.bukkit.inventory.EntityEquipment; import org.bukkit.inventory.EntityEquipment;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
@ -30,6 +55,9 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.Set; import java.util.Set;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
public class StackingTask extends BukkitRunnable { public class StackingTask extends BukkitRunnable {
@ -42,7 +70,7 @@ public class StackingTask extends BukkitRunnable {
private final Map<CachedChunk, Entity[]> cachedChunks = new HashMap<>(); private final Map<CachedChunk, Entity[]> cachedChunks = new HashMap<>();
private final HashMap<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(),
searchRadius = Settings.SEARCH_RADIUS.getInt(), searchRadius = Settings.SEARCH_RADIUS.getInt(),
@ -83,11 +111,10 @@ public class StackingTask extends BukkitRunnable {
// Get the loaded entities from the current world and reverse them. // Get the loaded entities from the current world and reverse them.
List<LivingEntity> entities; List<LivingEntity> entities;
try { try {
entities = sWorld.getLivingEntities(); entities = getLivingEntitiesSync(sWorld).get();
} catch (Exception ignored) { } catch (ExecutionException | InterruptedException ex) {
ex.printStackTrace();
continue; continue;
// Sometimes accessing this method asynchronously throws an error. This is super rare and
// as such doesn't really affect the plugin so we're just going to ignore this failure.
} }
Collections.reverse(entities); Collections.reverse(entities);
@ -113,6 +140,20 @@ public class StackingTask extends BukkitRunnable {
this.cachedChunks.clear(); this.cachedChunks.clear();
} }
private Future<List<LivingEntity>> getLivingEntitiesSync(SWorld sWorld) {
CompletableFuture<List<LivingEntity>> future = new CompletableFuture<>();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> future.complete(sWorld.getLivingEntities()));
return future;
}
private Future<Entity[]> getEntitiesInChunkSync(CachedChunk cachedChunk) {
CompletableFuture<Entity[]> future = new CompletableFuture<>();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> future.complete(cachedChunk.getEntities()));
return future;
}
public boolean isWorldDisabled(World world) { public boolean isWorldDisabled(World world) {
return disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr)); return disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr));
} }
@ -367,19 +408,21 @@ public class StackingTask extends BukkitRunnable {
List<LivingEntity> entities = new ArrayList<>(); List<LivingEntity> entities = new ArrayList<>();
for (CachedChunk chunk : getNearbyChunks(sWorld, location, radius, singleChunk)) { for (CachedChunk chunk : getNearbyChunks(sWorld, location, radius, singleChunk)) {
if (chunk == null) continue; if (chunk == null) continue;
Entity[] entityArray = new Entity[0]; Entity[] entityArray;
if (cachedChunks.containsKey(chunk)) { if (cachedChunks.containsKey(chunk)) {
entityArray = cachedChunks.get(chunk); entityArray = cachedChunks.get(chunk);
} else { } else {
try { try {
entityArray = chunk.getEntities(); entityArray = getEntitiesInChunkSync(chunk).get();
cachedChunks.put(chunk, entityArray); cachedChunks.put(chunk, entityArray);
} catch (Exception ignored) { } catch (ExecutionException | InterruptedException ex) {
// Sometimes accessing this method asynchronously throws an error. This is super rare and ex.printStackTrace();
// as such doesn't really affect the plugin so we're just going to ignore this failure. continue;
} }
} }
if (entityArray == null) continue; if (entityArray == null) continue;
for (Entity e : entityArray) { for (Entity e : entityArray) {
if (e == null) continue; if (e == null) continue;
if (e.getWorld() != location.getWorld() if (e.getWorld() != location.getWorld()
@ -388,6 +431,7 @@ public class StackingTask extends BukkitRunnable {
entities.add((LivingEntity) e); entities.add((LivingEntity) e);
} }
} }
return entities; return entities;
} }