Async wait makes sense.

This commit is contained in:
Brianna O'Keefe 2024-03-12 15:07:38 -05:00
parent 5c056ec545
commit 63033243c4

View File

@ -20,6 +20,9 @@ import org.bukkit.persistence.PersistentDataType;
import org.bukkit.scheduler.BukkitRunnable; import org.bukkit.scheduler.BukkitRunnable;
import java.util.*; import java.util.*;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static com.craftaro.ultimatestacker.stackable.entity.Check.getChecks; import static com.craftaro.ultimatestacker.stackable.entity.Check.getChecks;
@ -64,7 +67,7 @@ public class StackingTask extends BukkitRunnable {
} }
int tickRate = Settings.STACK_SEARCH_TICK_SPEED.getInt(); int tickRate = Settings.STACK_SEARCH_TICK_SPEED.getInt();
runTaskTimer(plugin, tickRate, tickRate); runTaskTimerAsynchronously(plugin, tickRate, tickRate);
} }
@Override @Override
@ -75,7 +78,13 @@ public class StackingTask extends BukkitRunnable {
for (SWorld sWorld : loadedWorlds) { for (SWorld sWorld : loadedWorlds) {
List<LivingEntity> entities; List<LivingEntity> entities;
// Get the loaded entities from the current world and reverse them. // Get the loaded entities from the current world and reverse them.
entities = sWorld.getLivingEntities(); try {
entities = getLivingEntitiesSync(sWorld).get();
} catch (ExecutionException | InterruptedException ex) {
ex.printStackTrace();
continue;
}
//Filter non-stackable entities to improve performance on main thread //Filter non-stackable entities to improve performance on main thread
entities.removeIf(this::isEntityNotStackable); entities.removeIf(this::isEntityNotStackable);
@ -87,18 +96,20 @@ public class StackingTask extends BukkitRunnable {
entities.removeIf(entity1 -> entity1.getUniqueId().equals(entity.getUniqueId())); entities.removeIf(entity1 -> entity1.getUniqueId().equals(entity.getUniqueId()));
} }
// Loop through the entities. Bukkit.getScheduler().runTask(plugin, () -> {
for (LivingEntity entity : entities) { // Loop through the entities.
// Make sure our entity has not already been processed. for (LivingEntity entity : entities) {
// Skip it if it has been. // Make sure our entity has not already been processed.
if (processed.contains(entity.getUniqueId())) continue; // Skip it if it has been.
if (processed.contains(entity.getUniqueId())) continue;
// Get entity location to pass around as its faster this way. // Get entity location to pass around as its faster this way.
Location location = entity.getLocation(); Location location = entity.getLocation();
// Process the entity. // Process the entity.
processEntity(entity, location, entities); processEntity(entity, location, entities);
} }
});
} }
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
@ -109,6 +120,12 @@ public class StackingTask extends BukkitRunnable {
} }
} }
private Future<List<LivingEntity>> getLivingEntitiesSync(SWorld sWorld) {
CompletableFuture<List<LivingEntity>> future = new CompletableFuture<>();
Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> future.complete(sWorld.getLivingEntities()));
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));
} }