diff --git a/src/main/java/com/playmonumenta/structures/StructuresAPI.java b/src/main/java/com/playmonumenta/structures/StructuresAPI.java index 1089598..be14b49 100644 --- a/src/main/java/com/playmonumenta/structures/StructuresAPI.java +++ b/src/main/java/com/playmonumenta/structures/StructuresAPI.java @@ -620,7 +620,7 @@ public boolean equals(Object obj) { } private static final HashMap CHUNK_TICKET_REFERENCE_COUNT = new HashMap<>(); - private static Deque PENDING_TASKS = new ConcurrentLinkedDeque<>(); + private static final LinkedBlockingDeque PENDING_TASKS = new LinkedBlockingDeque<>(); private static @Nullable BukkitRunnable RUNNING_TASK = null; private static final EnumSet keptEntities = EnumSet.of( @@ -683,24 +683,21 @@ private static void ensureTask(Plugin plugin) { @Override public void run() { while (true) { - PendingTask task = PENDING_TASKS.pollFirst(); - - if (task != null) { - try { - Bukkit.getScheduler().runTask(plugin, task.mStartTask); - task.mSignal.get(45, TimeUnit.SECONDS); - } catch (Exception ex) { - plugin.getLogger().severe("Structure task took longer than 45s to complete! Continuing to the next task. Exception: " + ex.getMessage()); - } - } - + PendingTask task; try { - Thread.sleep(50); - } catch (Exception ex) { + task = PENDING_TASKS.take(); + } catch (InterruptedException ignored) { plugin.getLogger().info("Structure loading task sleep was interrupted"); RUNNING_TASK = null; break; } + + try { + Bukkit.getScheduler().runTask(plugin, task.mStartTask); + task.mSignal.get(45, TimeUnit.SECONDS); + } catch (Exception ex) { + plugin.getLogger().severe("Structure task took longer than 45s to complete! Continuing to the next task. Exception: " + ex.getMessage()); + } } } };