From 3a0f9ca01d1622087478143ca4aa403ec5b5b3e5 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Fri, 22 Mar 2024 17:01:11 +0100 Subject: [PATCH 1/8] =?UTF-8?q?Suppression=20des=20anciens=20schedules=20d?= =?UTF-8?q?=C3=A9pr=C3=A9ci=C3=A9=20Ne=20plus=20utilise=20les=20AtomicRefe?= =?UTF-8?q?rence=20car=20il=20peut=20=C3=AAtre=20null?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- build.gradle.kts | 2 +- .../energie/executors/ExecutorsScheduler.java | 54 ----- .../energie/folia/FoliaScheduler.java | 189 ++++-------------- .../energie/folia/FoliaSchedulerTask.java | 8 +- .../energie/legacy/LegacyScheduler.java | 53 ----- .../fr/euphyllia/energie/model/Scheduler.java | 27 --- .../energie/model/SchedulerTaskInter.java | 4 + 7 files changed, 50 insertions(+), 287 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 0de7818..0d92be9 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -4,7 +4,7 @@ plugins { } group = "fr.euphyllia" -version = "1.1.9" +version = "1.2.0" repositories { mavenCentral() diff --git a/src/main/java/fr/euphyllia/energie/executors/ExecutorsScheduler.java b/src/main/java/fr/euphyllia/energie/executors/ExecutorsScheduler.java index b2fc858..3a3f6c9 100644 --- a/src/main/java/fr/euphyllia/energie/executors/ExecutorsScheduler.java +++ b/src/main/java/fr/euphyllia/energie/executors/ExecutorsScheduler.java @@ -218,60 +218,6 @@ public int scheduleSyncRepeating(@NotNull SchedulerType schedulerType, Entity en throw new UnsupportedOperationException(); } - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } - - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - throw new UnsupportedOperationException(); - } - - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - throw new UnsupportedOperationException(); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, long delayTicks, SchedulerCallBack callBack) { - this.runDelayed(schedulerType, callBack, delayTicks); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long delayTicks, SchedulerCallBack callBack) { - throw new UnsupportedOperationException(); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long delayTicks, SchedulerCallBack callBack) { - throw new UnsupportedOperationException(); - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) { - this.runTask(schedulerType, callBack); - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, SchedulerCallBack callBack) { - throw new UnsupportedOperationException(); - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, SchedulerCallBack callBack) { - throw new UnsupportedOperationException(); - } - @Override public List getPendingTasks() { return new ArrayList<>(mapSchedulerTask.values()); diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java index c385c48..fcc6b59 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java @@ -1,6 +1,7 @@ package fr.euphyllia.energie.folia; import fr.euphyllia.energie.model.*; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.Bukkit; import org.bukkit.Chunk; import org.bukkit.Location; @@ -12,6 +13,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; @@ -38,26 +40,23 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, S initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - + SchedulerTaskInter inter; if (schedulerType.equals(SchedulerType.ASYNC)) { - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getAsyncScheduler().runAtFixedRate(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), initialDelayTicks * 50, periodTicks * 50, TimeUnit.MILLISECONDS), - false - )); + inter = new FoliaSchedulerTask(null, false); + ScheduledTask interTask = Bukkit.getAsyncScheduler().runAtFixedRate(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, initialDelayTicks * 50, periodTicks * 50, TimeUnit.MILLISECONDS); + inter.setSchedulerTask(interTask); } else { - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getGlobalRegionScheduler().runAtFixedRate(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), initialDelayTicks, periodTicks), - true - )); + inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, initialDelayTicks, periodTicks); + inter.setSchedulerTask(interTask); } - - return foliaSchedulerTaskRef.get(); + return inter; } @Override @@ -68,18 +67,13 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, M initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getRegionScheduler().runAtFixedRate(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> taskRunnable.run(foliaSchedulerTaskRef.get()), initialDelayTicks, periodTicks), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getRegionScheduler().runAtFixedRate(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, initialDelayTicks, periodTicks); + inter.setSchedulerTask(interTask); + return inter; } } @@ -91,18 +85,13 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, L initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getRegionScheduler().runAtFixedRate(this.plugin, location, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), initialDelayTicks, periodTicks), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getRegionScheduler().runAtFixedRate(this.plugin, location, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, initialDelayTicks, periodTicks); + inter.setSchedulerTask(interTask); + return inter; } } @@ -113,19 +102,14 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, E } else { initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - entity.getScheduler().runAtFixedRate(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), retired, initialDelayTicks, periodTicks), - true - )); - - return foliaSchedulerTaskRef.get(); + + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = entity.getScheduler().runAtFixedRate(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, retired, initialDelayTicks, periodTicks); + inter.setSchedulerTask(interTask); + return inter; } } @@ -426,103 +410,6 @@ public int scheduleSyncRepeating(@NotNull SchedulerType schedulerType, Entity en } } - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } - - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - if (chunkOrLoc instanceof Location) { - this.runAtFixedRate(schedulerType, (Location) chunkOrLoc, callBack, initialDelayTicks, periodTicks); - } else if (chunkOrLoc instanceof MultipleRecords.WorldChunk) { - this.runAtFixedRate(schedulerType, (MultipleRecords.WorldChunk) chunkOrLoc, callBack, initialDelayTicks, periodTicks); - } else if (chunkOrLoc instanceof Chunk) { - Chunk chunk = (Chunk) chunkOrLoc; - MultipleRecords.WorldChunk worldChunk = new MultipleRecords.WorldChunk(chunk.getWorld(), chunk.getX(), chunk.getZ()); - this.runAtFixedRate(schedulerType, worldChunk, callBack, initialDelayTicks, periodTicks); - } else { - this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } - } - - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - if (chunkOrLocOrEntity instanceof Entity) { - Entity entity = (Entity) chunkOrLocOrEntity; - this.runAtFixedRate(schedulerType, entity, callBack, retired, initialDelayTicks, periodTicks); - } else { - this.runAtFixedRate(schedulerType, chunkOrLocOrEntity, initialDelayTicks, periodTicks, callBack); - } - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, long delayTicks, SchedulerCallBack callBack) { - this.runDelayed(schedulerType, callBack, delayTicks); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long delayTicks, SchedulerCallBack callBack) { - if (chunkOrLoc instanceof Location) { - this.runDelayed(schedulerType, (Location) chunkOrLoc, callBack, delayTicks); - } else if (chunkOrLoc instanceof MultipleRecords.WorldChunk) { - this.runDelayed(schedulerType, (MultipleRecords.WorldChunk) chunkOrLoc, callBack, delayTicks); - } else if (chunkOrLoc instanceof Chunk) { - Chunk chunk = (Chunk) chunkOrLoc; - MultipleRecords.WorldChunk worldChunk = new MultipleRecords.WorldChunk(chunk.getWorld(), chunk.getX(), chunk.getZ()); - this.runDelayed(schedulerType, worldChunk, callBack, delayTicks); - } else { - this.runDelayed(schedulerType, callBack, delayTicks); - } - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long delayTicks, SchedulerCallBack callBack) { - if (chunkOrLocOrEntity instanceof Entity) { - this.runDelayed(schedulerType, (Entity) chunkOrLocOrEntity, callBack, retired, delayTicks); - } else { - this.runDelayed(schedulerType, chunkOrLocOrEntity, delayTicks, callBack); - } - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) { - this.scheduleSyncDelayed(schedulerType, callBack); - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, SchedulerCallBack callBack) { - if (chunkOrLoc instanceof Location) { - this.scheduleSyncDelayed(schedulerType, (Location) chunkOrLoc, callBack); - } else if (chunkOrLoc instanceof MultipleRecords.WorldChunk) { - this.scheduleSyncDelayed(schedulerType, (MultipleRecords.WorldChunk) chunkOrLoc, callBack); - } else if (chunkOrLoc instanceof Chunk) { - Chunk chunk = (Chunk) chunkOrLoc; - MultipleRecords.WorldChunk worldChunk = new MultipleRecords.WorldChunk(chunk.getWorld(), chunk.getX(), chunk.getZ()); - this.scheduleSyncDelayed(schedulerType, worldChunk, callBack); - } else { - this.scheduleSyncDelayed(schedulerType, callBack); - } - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, SchedulerCallBack callBack) { - if (chunkOrLocOrEntity instanceof Entity) { - this.execute(schedulerType, (Entity) chunkOrLocOrEntity, retired, callBack); - } else { - this.execute(schedulerType, chunkOrLocOrEntity, callBack); - } - } - @Override public List getPendingTasks() { return new ArrayList<>(mapSchedulerTask.values()); diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java b/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java index b13b510..9c284ef 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java @@ -4,10 +4,11 @@ import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public class FoliaSchedulerTask implements SchedulerTaskInter { - private final ScheduledTask schedulerTask; + private ScheduledTask schedulerTask; private boolean isSynchronous = false; public FoliaSchedulerTask(ScheduledTask schedulerTask, boolean sync) { @@ -40,4 +41,9 @@ public int getTaskId() { public boolean isSync() { return this.isSynchronous; } + + @Override + public void setSchedulerTask(@Nullable ScheduledTask schedulerTask) { + this.schedulerTask = schedulerTask; + } } diff --git a/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java b/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java index 5cf0940..9649f15 100644 --- a/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java +++ b/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java @@ -182,59 +182,6 @@ public int scheduleSyncRepeating(@NotNull SchedulerType schedulerType, Entity en return this.scheduleSyncRepeating(schedulerType, callBack, delay, period); } - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } - - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } - - @Override - @Deprecated - public void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack) { - this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, long delayTicks, SchedulerCallBack callBack) { - this.runDelayed(schedulerType, callBack, delayTicks); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long delayTicks, SchedulerCallBack callBack) { - this.runDelayed(schedulerType, callBack, delayTicks); - } - - @Override - @Deprecated - public void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long delayTicks, SchedulerCallBack callBack) { - this.runDelayed(schedulerType, callBack, delayTicks); - } - - @Override - public void execute(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) { - this.scheduleSyncDelayed(schedulerType, callBack); - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, SchedulerCallBack callBack) { - this.scheduleSyncDelayed(schedulerType, callBack); - } - - @Override - @Deprecated - public void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, SchedulerCallBack callBack) { - this.scheduleSyncDelayed(schedulerType, callBack); - } - @Override public List getPendingTasks() { return Bukkit.getScheduler().getPendingTasks().stream().map(LegacySchedulerTask::new).collect(Collectors.toList()); diff --git a/src/main/java/fr/euphyllia/energie/model/Scheduler.java b/src/main/java/fr/euphyllia/energie/model/Scheduler.java index 06001df..32f7984 100644 --- a/src/main/java/fr/euphyllia/energie/model/Scheduler.java +++ b/src/main/java/fr/euphyllia/energie/model/Scheduler.java @@ -59,33 +59,6 @@ public interface Scheduler { int scheduleSyncRepeating(@NotNull SchedulerType schedulerType, Entity entity, SchedulerCallBack callBack, @Nullable Runnable retired, long delay, long period); - @Deprecated - void runAtFixedRate(@NotNull SchedulerType schedulerType, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack); - - @Deprecated - void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack); - - @Deprecated - void runAtFixedRate(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks, SchedulerCallBack callBack); - - @Deprecated - void runDelayed(@NotNull SchedulerType schedulerType, long delayTicks, SchedulerCallBack callBack); - - @Deprecated - void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, long delayTicks, SchedulerCallBack callBack); - - @Deprecated - void runDelayed(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, long delayTicks, SchedulerCallBack callBack); - - @Deprecated - void execute(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack); - - @Deprecated - void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLoc, SchedulerCallBack callBack); - - @Deprecated - void execute(@NotNull SchedulerType schedulerType, @Nullable Object chunkOrLocOrEntity, @Nullable Runnable retired, SchedulerCallBack callBack); - List getPendingTasks(); void cancelAllTask(); diff --git a/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java b/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java index e98ed51..42b451f 100644 --- a/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java +++ b/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java @@ -1,7 +1,9 @@ package fr.euphyllia.energie.model; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; public interface SchedulerTaskInter { @@ -14,4 +16,6 @@ public interface SchedulerTaskInter { int getTaskId(); boolean isSync(); + + void setSchedulerTask(@Nullable ScheduledTask interTask); } From 6812e1498d0ccb785987c7f529daedf7a48d2797 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Fri, 22 Mar 2024 17:27:15 +0100 Subject: [PATCH 2/8] progress --- .../energie/folia/FoliaScheduler.java | 159 +++++++----------- 1 file changed, 63 insertions(+), 96 deletions(-) diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java index fcc6b59..f2b5de7 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java @@ -116,26 +116,24 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, E @Override public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack, long delayTicks) { delayTicks = Math.max(1, delayTicks); - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; + SchedulerTaskInter inter; if (schedulerType.equals(SchedulerType.ASYNC)) { - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getAsyncScheduler().runDelayed(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), delayTicks * 50, TimeUnit.MILLISECONDS), - false - )); + inter = new FoliaSchedulerTask(null, false); + ScheduledTask interTask = Bukkit.getAsyncScheduler().runDelayed(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, delayTicks * 50, TimeUnit.MILLISECONDS); + inter.setSchedulerTask(interTask); } else { - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getGlobalRegionScheduler().runDelayed(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), delayTicks), - true - )); + inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runDelayed(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, delayTicks); + inter.setSchedulerTask(interTask); } - - return foliaSchedulerTaskRef.get(); + return inter; } @Override @@ -145,18 +143,13 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Multi } else { delayTicks = Math.max(1, delayTicks); - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getRegionScheduler().runDelayed(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> taskRunnable.run(foliaSchedulerTaskRef.get()), delayTicks), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getRegionScheduler().runDelayed(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, delayTicks); + inter.setSchedulerTask(interTask); + return inter; } } @@ -167,18 +160,13 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Locat } else { delayTicks = Math.max(1, delayTicks); - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getRegionScheduler().runDelayed(this.plugin, location, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), delayTicks), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getRegionScheduler().runDelayed(this.plugin, location, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, delayTicks); + inter.setSchedulerTask(interTask); + return inter; } } @@ -206,26 +194,23 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Entit @Override public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) { - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - + SchedulerTaskInter inter; if (schedulerType.equals(SchedulerType.ASYNC)) { - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getAsyncScheduler().runNow(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get())), - false - )); + inter = new FoliaSchedulerTask(null, false); + ScheduledTask interTask = Bukkit.getAsyncScheduler().runNow(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }); + inter.setSchedulerTask(interTask); } else { - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getGlobalRegionScheduler().run(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get())), - true - )); + inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().run(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }); + inter.setSchedulerTask(interTask); } - - return foliaSchedulerTaskRef.get(); + return inter; } @Override @@ -233,19 +218,13 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Multiple if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); } else { - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getRegionScheduler().run(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> taskRunnable.run(foliaSchedulerTaskRef.get())), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getRegionScheduler().run(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }); + inter.setSchedulerTask(interTask); + return inter; } } @@ -254,19 +233,13 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Location if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); } else { - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - Bukkit.getRegionScheduler().run(this.plugin, location, task -> taskRunnable.run(foliaSchedulerTaskRef.get())), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = Bukkit.getRegionScheduler().run(this.plugin, location, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }); + inter.setSchedulerTask(interTask); + return inter; } } @@ -275,19 +248,13 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Entity e if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); } else { - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - entity.getScheduler().run(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), retired), - true - )); - - return foliaSchedulerTaskRef.get(); + SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = entity.getScheduler().run(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, retired); + inter.setSchedulerTask(interTask); + return inter; } } From 943877c763689538732add0853eb43398044ce9a Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Fri, 22 Mar 2024 17:34:42 +0100 Subject: [PATCH 3/8] Finish --- .../energie/folia/FoliaScheduler.java | 43 ++++++++----------- .../energie/folia/FoliaSchedulerTask.java | 1 - .../energie/model/SchedulerTaskInter.java | 4 -- 3 files changed, 19 insertions(+), 29 deletions(-) diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java index f2b5de7..edaa33d 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java @@ -40,7 +40,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, S initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - SchedulerTaskInter inter; + FoliaSchedulerTask inter; if (schedulerType.equals(SchedulerType.ASYNC)) { inter = new FoliaSchedulerTask(null, false); ScheduledTask interTask = Bukkit.getAsyncScheduler().runAtFixedRate(this.plugin, task -> { @@ -67,7 +67,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, M initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().runAtFixedRate(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -85,7 +85,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, L initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().runAtFixedRate(this.plugin, location, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -102,8 +102,8 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, E } else { initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); - - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = entity.getScheduler().runAtFixedRate(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -117,7 +117,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, E public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack, long delayTicks) { delayTicks = Math.max(1, delayTicks); - SchedulerTaskInter inter; + FoliaSchedulerTask inter; if (schedulerType.equals(SchedulerType.ASYNC)) { inter = new FoliaSchedulerTask(null, false); ScheduledTask interTask = Bukkit.getAsyncScheduler().runDelayed(this.plugin, task -> { @@ -143,7 +143,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Multi } else { delayTicks = Math.max(1, delayTicks); - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().runDelayed(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -160,7 +160,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Locat } else { delayTicks = Math.max(1, delayTicks); - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().runDelayed(this.plugin, location, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -177,24 +177,19 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Entit } else { delayTicks = Math.max(1, delayTicks); - final AtomicReference foliaSchedulerTaskRef = new AtomicReference<>(); - - TaskRunnable taskRunnable = (task) -> { - mapSchedulerTask.put(task.getTaskId(), task); - callBack.run(task); - }; - foliaSchedulerTaskRef.set(new FoliaSchedulerTask( - entity.getScheduler().runDelayed(this.plugin, task -> taskRunnable.run(foliaSchedulerTaskRef.get()), retired, delayTicks), - true - )); - - return foliaSchedulerTaskRef.get(); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); + ScheduledTask interTask = entity.getScheduler().runDelayed(this.plugin, task -> { + mapSchedulerTask.put(task.hashCode(), inter); + callBack.run(inter); + }, retired, delayTicks); + inter.setSchedulerTask(interTask); + return inter; } } @Override public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) { - SchedulerTaskInter inter; + FoliaSchedulerTask inter; if (schedulerType.equals(SchedulerType.ASYNC)) { inter = new FoliaSchedulerTask(null, false); ScheduledTask interTask = Bukkit.getAsyncScheduler().runNow(this.plugin, task -> { @@ -218,7 +213,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Multiple if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); } else { - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().run(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -233,7 +228,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Location if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); } else { - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().run(this.plugin, location, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); @@ -248,7 +243,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Entity e if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); } else { - SchedulerTaskInter inter = new FoliaSchedulerTask(null, true); + FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = entity.getScheduler().run(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java b/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java index 9c284ef..10a8dd1 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaSchedulerTask.java @@ -42,7 +42,6 @@ public boolean isSync() { return this.isSynchronous; } - @Override public void setSchedulerTask(@Nullable ScheduledTask schedulerTask) { this.schedulerTask = schedulerTask; } diff --git a/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java b/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java index 42b451f..e98ed51 100644 --- a/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java +++ b/src/main/java/fr/euphyllia/energie/model/SchedulerTaskInter.java @@ -1,9 +1,7 @@ package fr.euphyllia.energie.model; -import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.plugin.Plugin; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; public interface SchedulerTaskInter { @@ -16,6 +14,4 @@ public interface SchedulerTaskInter { int getTaskId(); boolean isSync(); - - void setSchedulerTask(@Nullable ScheduledTask interTask); } From a121ebda98eb4124246ba2bee10364361e444ab6 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Sat, 23 Mar 2024 01:02:17 +0100 Subject: [PATCH 4/8] Suppression Deprecated fonction --- src/main/java/fr/euphyllia/energie/Energie.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/main/java/fr/euphyllia/energie/Energie.java b/src/main/java/fr/euphyllia/energie/Energie.java index 6acde69..9164db9 100644 --- a/src/main/java/fr/euphyllia/energie/Energie.java +++ b/src/main/java/fr/euphyllia/energie/Energie.java @@ -45,19 +45,4 @@ public Scheduler getMinecraftScheduler() { } return this.legacyScheduler; } - - @Deprecated - public Scheduler getScheduler(SchedulerSoft schedulerSoft) { - if (schedulerSoft == SchedulerSoft.NATIVE) { - return this.getNativeScheduler(); - } else if (schedulerSoft == SchedulerSoft.MINECRAFT) { - return this.getMinecraftScheduler(); - } - throw new UnsupportedOperationException(); - } - - @Deprecated - public enum SchedulerSoft { - NATIVE, MINECRAFT - } } \ No newline at end of file From 9e3fbc2ef9b0b9a3d3f345aa53bd3dfd3fb43d0d Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Sat, 23 Mar 2024 01:09:31 +0100 Subject: [PATCH 5/8] Utiliser le synchrone quand c'est necessaire et ne plus utiliser les anciens types --- .../energie/folia/FoliaScheduler.java | 64 ++++++++++++++----- .../energie/model/SchedulerType.java | 6 -- 2 files changed, 48 insertions(+), 22 deletions(-) diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java index edaa33d..7ecabb8 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java @@ -48,13 +48,15 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, S callBack.run(inter); }, initialDelayTicks * 50, periodTicks * 50, TimeUnit.MILLISECONDS); inter.setSchedulerTask(interTask); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); }, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } return inter; } @@ -63,7 +65,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, S public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, SchedulerCallBack callBack, long initialDelayTicks, long periodTicks) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); @@ -74,6 +76,8 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, M }, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -81,7 +85,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, M public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, Location location, SchedulerCallBack callBack, long initialDelayTicks, long periodTicks) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); @@ -92,6 +96,8 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, L }, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -99,7 +105,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, L public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, Entity entity, SchedulerCallBack callBack, @Nullable Runnable retired, long initialDelayTicks, long periodTicks) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runAtFixedRate(schedulerType, callBack, initialDelayTicks, periodTicks); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { initialDelayTicks = Math.max(1, initialDelayTicks); periodTicks = Math.max(1, periodTicks); @@ -110,6 +116,8 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, E }, retired, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -125,13 +133,15 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Sched callBack.run(inter); }, delayTicks * 50, TimeUnit.MILLISECONDS); inter.setSchedulerTask(interTask); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runDelayed(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); }, delayTicks); inter.setSchedulerTask(interTask); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } return inter; } @@ -140,7 +150,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Sched public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, SchedulerCallBack callBack, long delayTicks) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runDelayed(schedulerType, callBack, delayTicks); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { delayTicks = Math.max(1, delayTicks); FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); @@ -150,6 +160,8 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Multi }, delayTicks); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -157,7 +169,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Multi public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Location location, SchedulerCallBack callBack, long delayTicks) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runDelayed(schedulerType, callBack, delayTicks); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { delayTicks = Math.max(1, delayTicks); FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); @@ -167,6 +179,8 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Locat }, delayTicks); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -174,7 +188,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Locat public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Entity entity, SchedulerCallBack callBack, @Nullable Runnable retired, long delayTicks) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runDelayed(schedulerType, callBack, delayTicks); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { delayTicks = Math.max(1, delayTicks); FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); @@ -184,6 +198,8 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Entit }, retired, delayTicks); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -197,13 +213,15 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Schedule callBack.run(inter); }); inter.setSchedulerTask(interTask); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().run(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); }); inter.setSchedulerTask(interTask); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } return inter; } @@ -212,7 +230,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Schedule public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, SchedulerCallBack callBack) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().run(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task -> { mapSchedulerTask.put(task.hashCode(), inter); @@ -220,6 +238,8 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Multiple }); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -227,7 +247,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Multiple public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Location location, SchedulerCallBack callBack) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = Bukkit.getRegionScheduler().run(this.plugin, location, task -> { mapSchedulerTask.put(task.hashCode(), inter); @@ -235,6 +255,8 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Location }); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -242,7 +264,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Location public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Entity entity, SchedulerCallBack callBack, @Nullable Runnable retired) { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { FoliaSchedulerTask inter = new FoliaSchedulerTask(null, true); ScheduledTask interTask = entity.getScheduler().run(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); @@ -250,6 +272,8 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Entity e }, retired); inter.setSchedulerTask(interTask); return inter; + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -258,8 +282,10 @@ public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, SchedulerCa try { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, callBack).getTaskId(); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { Bukkit.getGlobalRegionScheduler().execute(this.plugin, () -> callBack.run(null)); + } else { + return -1; } return 0; } catch (Exception ignored) { @@ -272,8 +298,10 @@ public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, MultipleRec try { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, worldChunk, callBack).getTaskId(); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { Bukkit.getRegionScheduler().execute(this.plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), () -> callBack.run(null)); + } else { + return -1; } return 0; } catch (Exception ignored) { @@ -286,8 +314,10 @@ public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, Location lo try { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runTask(schedulerType, location, callBack).getTaskId(); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { Bukkit.getRegionScheduler().execute(this.plugin, location, () -> callBack.run(null)); + } else { + return -1; } return 0; } catch (Exception ignored) { @@ -327,8 +357,10 @@ public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, Entity enti try { if (schedulerType.equals(SchedulerType.ASYNC)) { return this.runDelayed(schedulerType, entity, callBack, retired, delay).getTaskId(); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { entity.getScheduler().execute(this.plugin, () -> callBack.run(null), retired, delay); + } else { + return -1; } return 0; } catch (Exception ignored) { diff --git a/src/main/java/fr/euphyllia/energie/model/SchedulerType.java b/src/main/java/fr/euphyllia/energie/model/SchedulerType.java index 9963b1d..d918244 100644 --- a/src/main/java/fr/euphyllia/energie/model/SchedulerType.java +++ b/src/main/java/fr/euphyllia/energie/model/SchedulerType.java @@ -1,12 +1,6 @@ package fr.euphyllia.energie.model; public enum SchedulerType { - @Deprecated - GLOBAL, - @Deprecated - REGION, - @Deprecated - ENTITY, ASYNC, SYNC } From a21efa88580fb15c7cb5fc46dbfa976a5399cfdc Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Sat, 23 Mar 2024 01:11:53 +0100 Subject: [PATCH 6/8] Identique pour LegacyScheduler --- .../energie/folia/FoliaScheduler.java | 33 +++++++++---------- .../energie/legacy/LegacyScheduler.java | 24 ++++++++++---- 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java index 7ecabb8..c843d87 100644 --- a/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java +++ b/src/main/java/fr/euphyllia/energie/folia/FoliaScheduler.java @@ -3,7 +3,6 @@ import fr.euphyllia.energie.model.*; import io.papermc.paper.threadedregions.scheduler.ScheduledTask; import org.bukkit.Bukkit; -import org.bukkit.Chunk; import org.bukkit.Location; import org.bukkit.entity.Entity; import org.bukkit.plugin.Plugin; @@ -13,11 +12,9 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.concurrent.CompletableFuture; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.Future; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; public class FoliaScheduler implements Scheduler { @@ -50,12 +47,12 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, S inter.setSchedulerTask(interTask); } else if (schedulerType.equals(SchedulerType.SYNC)) { inter = new FoliaSchedulerTask(null, true); - ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(this.plugin, task -> { + ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runAtFixedRate(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); }, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } return inter; @@ -76,7 +73,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, M }, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -96,7 +93,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, L }, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -116,7 +113,7 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, E }, retired, initialDelayTicks, periodTicks); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -135,12 +132,12 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Sched inter.setSchedulerTask(interTask); } else if (schedulerType.equals(SchedulerType.SYNC)) { inter = new FoliaSchedulerTask(null, true); - ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runDelayed(this.plugin, task -> { + ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().runDelayed(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); }, delayTicks); inter.setSchedulerTask(interTask); - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } return inter; @@ -160,7 +157,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Multi }, delayTicks); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -179,7 +176,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Locat }, delayTicks); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -198,7 +195,7 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Entit }, retired, delayTicks); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -215,12 +212,12 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Schedule inter.setSchedulerTask(interTask); } else if (schedulerType.equals(SchedulerType.SYNC)) { inter = new FoliaSchedulerTask(null, true); - ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().run(this.plugin, task -> { + ScheduledTask interTask = Bukkit.getGlobalRegionScheduler().run(this.plugin, task -> { mapSchedulerTask.put(task.hashCode(), inter); callBack.run(inter); }); inter.setSchedulerTask(interTask); - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } return inter; @@ -238,7 +235,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Multiple }); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -255,7 +252,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Location }); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -272,7 +269,7 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Entity e }, retired); inter.setSchedulerTask(interTask); return inter; - } else { + } else { throw new UnsupportedOperationException("SchedulerType not supported"); } } diff --git a/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java b/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java index 9649f15..f7a17bd 100644 --- a/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java +++ b/src/main/java/fr/euphyllia/energie/legacy/LegacyScheduler.java @@ -35,8 +35,10 @@ public SchedulerTaskInter runAtFixedRate(@NotNull SchedulerType schedulerType, S if (schedulerType.equals(SchedulerType.ASYNC)) { schedulerTaskRef.set(new LegacySchedulerTask(Bukkit.getScheduler().runTaskTimerAsynchronously(this.plugin, taskRunnable, initialDelayTicks, periodTicks))); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { schedulerTaskRef.set(new LegacySchedulerTask(Bukkit.getScheduler().runTaskTimer(this.plugin, taskRunnable, initialDelayTicks, periodTicks))); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } return schedulerTaskRef.get(); @@ -64,8 +66,10 @@ public SchedulerTaskInter runDelayed(@NotNull SchedulerType schedulerType, Sched if (schedulerType.equals(SchedulerType.ASYNC)) { schedulerTaskRef.set(new LegacySchedulerTask(Bukkit.getScheduler().runTaskLaterAsynchronously(this.plugin, taskRunnable, delayTicks))); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { schedulerTaskRef.set(new LegacySchedulerTask(Bukkit.getScheduler().runTaskLater(this.plugin, taskRunnable, delayTicks))); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } return schedulerTaskRef.get(); @@ -93,8 +97,10 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Schedule if (schedulerType.equals(SchedulerType.ASYNC)) { schedulerTaskRef.set(new LegacySchedulerTask(Bukkit.getScheduler().runTaskAsynchronously(this.plugin, taskRunnable))); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { schedulerTaskRef.set(new LegacySchedulerTask(Bukkit.getScheduler().runTask(this.plugin, taskRunnable))); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } return schedulerTaskRef.get(); @@ -119,8 +125,10 @@ public SchedulerTaskInter runTask(@NotNull SchedulerType schedulerType, Entity e public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack) { if (schedulerType.equals(SchedulerType.ASYNC)) { return Bukkit.getScheduler().scheduleAsyncDelayedTask(this.plugin, () -> callBack.run(null)); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { return Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> callBack.run(null)); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -138,8 +146,10 @@ public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, Location lo public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack, long delay) { if (schedulerType.equals(SchedulerType.ASYNC)) { return Bukkit.getScheduler().scheduleAsyncDelayedTask(this.plugin, () -> callBack.run(null), delay); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { return Bukkit.getScheduler().scheduleSyncDelayedTask(this.plugin, () -> callBack.run(null), delay); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } @@ -162,8 +172,10 @@ public int scheduleSyncDelayed(@NotNull SchedulerType schedulerType, Entity enti public int scheduleSyncRepeating(@NotNull SchedulerType schedulerType, SchedulerCallBack callBack, long delay, long period) { if (schedulerType.equals(SchedulerType.ASYNC)) { return Bukkit.getScheduler().scheduleAsyncRepeatingTask(this.plugin, () -> callBack.run(null), delay, period); - } else { + } else if (schedulerType.equals(SchedulerType.SYNC)) { return Bukkit.getScheduler().scheduleSyncRepeatingTask(this.plugin, () -> callBack.run(null), delay, period); + } else { + throw new UnsupportedOperationException("SchedulerType not supported"); } } From cf37b3e75308e0db7e7254782e2fed9a1567b801 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Tue, 26 Mar 2024 04:29:37 +0100 Subject: [PATCH 7/8] Update Readme.md --- readme.md | 132 +++++++++++++++++++++++++++++++++++++++--------------- 1 file changed, 96 insertions(+), 36 deletions(-) diff --git a/readme.md b/readme.md index 6107d29..33a2d72 100644 --- a/readme.md +++ b/readme.md @@ -1,45 +1,105 @@ -## API +## What is Energie ? +Energie is a library allowing you to maintain compatibility between Spigot and Folia. The library is still under development, but can be used. -Bukkit/Spigot/Paper : +## Setup +```kotlin +import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar -```java -// Example Scheduler -Bukkit.getScheduler().runTask(this.plugin, () -> { - // code -}); -``` +plugins { + id("java") + id("com.github.johnrengelman.shadow") version "8.1.1" +} -Folia : +val jitpack = "https://jitpack.io" + +repositories { + mavenCentral() + maven(jitpack) +} + +dependencies { + implementation("com.github.Euphillya:Energie:TAG") +} + +tasks.withType { + relocate("fr.euphyllia.energie", "[your plugin].dependency.energie") +} -```java -// Example Scheduler -Bukkit.getGlobalRegionScheduler().run(this.plugin, task -> { - // code -}); -Bukkit.getRegionScheduler().run(this.plugin, location, task -> { - // code -}); -Entity.getScheduler().run(this.plugin, task -> { - // code -}, retired); ``` +## API +The API remains quite simple to use: + +### Bukkit Implementation +```java +// before bukkit +private void runTaskBukkitExample() { + // Sync + Bukkit.getScheduler().runTask(JavaPlugin, Callback); + // Async + Bukkit.getScheduler().runTaskAsynchronously(JavaPlugin, Callback); +} + +private void runTaskLaterExample() { + Bukkit.getScheduler().runTaskLater(JavaPlugin, Callback, Long); +} + +private void runTaskTimerExample() { + Bukkit.getScheduler().runTaskTimer(JavaPlugin, Callback, Long, Long); +} -Energie : +private void runnableExample() { + new BukkitRunnable() { + @Override + public void run() { + } + }.runTask(JavaPlugin); +} +``` + +### Energie Implementation ```java -Energie energie = new Energie(this.plugin); - -Scheduler minecraftScheduler = energie.getScheduler(SchedulerSoft.MINECRAFT); -// Example getGlobalRegionScheduler -minecraftScheduler.runTask(SchedulerType.SYNC, task -> { - // code -}); -// Example getRegionScheduler -minecraftScheduler.runTask(SchedulerType.SYNC, (location/MultiRecords.WorldChunk), task -> { - // code -}); -// Example EntityScheduler -minecraftScheduler.runTask(SchedulerType.SYNC, entity, task -> { - // code -}, retired); + +private @NotNull Energie energie; +private @NotNull Scheduler scheduler; + +@Override +public void onEnable() { + this.energie = new Energie(JavaPlugin); + this.scheduler = scheduler.getMinecraftScheduler(); +} + +public Scheduler getScheduler() { + return scheduler; +} + + +private void runTaskBukkitExample() { + // Sync + getScheduler().runTask(SchedulerType.SYNC, Callback); // GlobalScheduler + getScheduler().runTask(SchedulerType.SYNC, Location/MultipleRecords.WorldChunk, Callback); // RegionScheduler + getScheduler().runTask(SchedulerType.SYNC, Entity, Callback, Retired); // EntityScheduler + + // Async + getScheduler().runTask(SchedulerType.ASYNC, Callback); +} + +private void runTaskLaterExample() { + getScheduler().runDelayed(SchedulerType.SYNC, Callback, Long); // global + ... +} + +private void runTaskTimerExample() { + getScheduler().runAtFixedRate(SchedulerType.SYNC, Callback, Long, Long); + ... +} + +private void runnableExample() { + new SchedulerTaskRunnable() { + @Override + public void run() { + + } + }.runTask(JavaPlugin); +} ``` From ea075ffb4e958910ee1954d13a48bfbd5166fee7 Mon Sep 17 00:00:00 2001 From: Euphyllia Bierque Date: Tue, 26 Mar 2024 12:52:10 +0100 Subject: [PATCH 8/8] Add list plugin in readme --- readme.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/readme.md b/readme.md index 33a2d72..d0a18f6 100644 --- a/readme.md +++ b/readme.md @@ -1,6 +1,12 @@ ## What is Energie ? Energie is a library allowing you to maintain compatibility between Spigot and Folia. The library is still under development, but can be used. +## Lists of plugins that officially use Energie + +| Name | Github | Download | +|---------|------------------------------------------------|------------------------------------------------------------------------| +| Skyllia | [Github](https://github.com/Euphillya/Skyllia) | [Modrinth](https://modrinth.com/plugin/skyllia/versions#all-versions) | + ## Setup ```kotlin import com.github.jengelman.gradle.plugins.shadow.tasks.ShadowJar