From 63033243c43cc14259f7c936e91cf8104303fbaa Mon Sep 17 00:00:00 2001 From: Brianna O'Keefe Date: Tue, 12 Mar 2024 15:07:38 -0500 Subject: [PATCH 1/3] Async wait makes sense. --- .../tasks/StackingTask.java | 41 +++++++++++++------ 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java index 242a69c..053776c 100644 --- a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java +++ b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java @@ -20,6 +20,9 @@ import org.bukkit.persistence.PersistentDataType; import org.bukkit.scheduler.BukkitRunnable; import java.util.*; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Future; import java.util.stream.Collectors; 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(); - runTaskTimer(plugin, tickRate, tickRate); + runTaskTimerAsynchronously(plugin, tickRate, tickRate); } @Override @@ -75,7 +78,13 @@ public class StackingTask extends BukkitRunnable { for (SWorld sWorld : loadedWorlds) { List entities; // 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 entities.removeIf(this::isEntityNotStackable); @@ -87,18 +96,20 @@ public class StackingTask extends BukkitRunnable { entities.removeIf(entity1 -> entity1.getUniqueId().equals(entity.getUniqueId())); } - // Loop through the entities. - for (LivingEntity entity : entities) { - // Make sure our entity has not already been processed. - // Skip it if it has been. - if (processed.contains(entity.getUniqueId())) continue; + Bukkit.getScheduler().runTask(plugin, () -> { + // Loop through the entities. + for (LivingEntity entity : entities) { + // Make sure our entity has not already been processed. + // Skip it if it has been. + if (processed.contains(entity.getUniqueId())) continue; - // Get entity location to pass around as its faster this way. - Location location = entity.getLocation(); + // Get entity location to pass around as its faster this way. + Location location = entity.getLocation(); - // Process the entity. - processEntity(entity, location, entities); - } + // Process the entity. + processEntity(entity, location, entities); + } + }); } } catch (Exception e) { e.printStackTrace(); @@ -109,6 +120,12 @@ public class StackingTask extends BukkitRunnable { } } + private Future> getLivingEntitiesSync(SWorld sWorld) { + CompletableFuture> future = new CompletableFuture<>(); + Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> future.complete(sWorld.getLivingEntities())); + return future; + } + public boolean isWorldDisabled(World world) { return disabledWorlds.stream().anyMatch(worldStr -> world.getName().equalsIgnoreCase(worldStr)); } From 76758ca98601bdfdef162154150a4e94b1273156 Mon Sep 17 00:00:00 2001 From: Brianna O'Keefe Date: Tue, 12 Mar 2024 15:21:28 -0500 Subject: [PATCH 2/3] Cleaned imports --- .../com.craftaro.ultimatestacker/tasks/StackingTask.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java index 053776c..04270f2 100644 --- a/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java +++ b/UltimateStacker-Plugin/src/main/java/com.craftaro.ultimatestacker/tasks/StackingTask.java @@ -11,12 +11,13 @@ import com.craftaro.ultimatestacker.settings.Settings; import com.craftaro.ultimatestacker.stackable.entity.Check; import com.craftaro.ultimatestacker.stackable.entity.custom.CustomEntity; import com.craftaro.ultimatestacker.utils.CachedChunk; -import org.bukkit.*; +import org.bukkit.Bukkit; +import org.bukkit.Location; +import org.bukkit.Material; +import org.bukkit.World; import org.bukkit.configuration.ConfigurationSection; import org.bukkit.entity.*; import org.bukkit.inventory.EntityEquipment; -import org.bukkit.persistence.PersistentDataContainer; -import org.bukkit.persistence.PersistentDataType; import org.bukkit.scheduler.BukkitRunnable; import java.util.*; @@ -85,7 +86,6 @@ public class StackingTask extends BukkitRunnable { continue; } - //Filter non-stackable entities to improve performance on main thread entities.removeIf(this::isEntityNotStackable); @@ -116,7 +116,6 @@ public class StackingTask extends BukkitRunnable { } finally { // Make sure we clear the processed list. this.processed.clear(); - } } From 9231f1c11280bd3a6c610a626e381d17dbf4d467 Mon Sep 17 00:00:00 2001 From: Brianna O'Keefe Date: Tue, 12 Mar 2024 15:22:22 -0500 Subject: [PATCH 3/3] version 3.1.3 --- UltimateStacker-API/pom.xml | 2 +- UltimateStacker-Plugin/pom.xml | 2 +- pom.xml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/UltimateStacker-API/pom.xml b/UltimateStacker-API/pom.xml index d2914f8..1494d13 100644 --- a/UltimateStacker-API/pom.xml +++ b/UltimateStacker-API/pom.xml @@ -7,7 +7,7 @@ com.craftaro UltimateStacker-Parent - 3.1.2 + 3.1.3 UltimateStacker-API 1.0.0-SNAPSHOT diff --git a/UltimateStacker-Plugin/pom.xml b/UltimateStacker-Plugin/pom.xml index 62980ac..8e4d370 100644 --- a/UltimateStacker-Plugin/pom.xml +++ b/UltimateStacker-Plugin/pom.xml @@ -7,7 +7,7 @@ com.craftaro UltimateStacker-Parent - 3.1.2 + 3.1.3 UltimateStacker-Plugin diff --git a/pom.xml b/pom.xml index e03a41d..1cef2c9 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ com.craftaro UltimateStacker-Parent pom - 3.1.2 + 3.1.3 UltimateStacker-API