This repository has been archived by the owner on May 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Création de SchedulerTaskRunnable qui est une inspiration de BukkitRunnable
- Loading branch information
Showing
5 changed files
with
251 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,7 +4,7 @@ plugins { | |
} | ||
|
||
group = "fr.euphyllia" | ||
version = "1.1.7" | ||
version = "1.1.8" | ||
|
||
repositories { | ||
mavenCentral() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/main/java/fr/euphyllia/energie/model/AbstractSchedulerTaskRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package fr.euphyllia.energie.model; | ||
|
||
import org.bukkit.Location; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.plugin.Plugin; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
public abstract class AbstractSchedulerTaskRunnable { | ||
protected SchedulerTaskInter task; | ||
|
||
/** | ||
* Returns true if this task has been cancelled. | ||
* | ||
* @return true if the task has been cancelled | ||
* @throws IllegalStateException if task was not scheduled yet | ||
*/ | ||
protected synchronized boolean isCancelled() throws IllegalStateException { | ||
checkScheduled(); | ||
return task.isCancelled(); | ||
} | ||
|
||
/** | ||
* Attempts to cancel this task. | ||
* | ||
* @throws IllegalStateException if task was not scheduled yet | ||
*/ | ||
public abstract void cancel() throws IllegalStateException; | ||
|
||
@NotNull | ||
public abstract SchedulerTaskInter runTask(@NotNull Plugin plugin, SchedulerType schedulerType) throws IllegalArgumentException, IllegalStateException; | ||
|
||
public abstract @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk); | ||
|
||
public abstract @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location); | ||
|
||
public abstract @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired); | ||
|
||
@NotNull | ||
public abstract SchedulerTaskInter runDelayed(@NotNull Plugin plugin, SchedulerType schedulerType, long delay) throws IllegalArgumentException, IllegalStateException; | ||
|
||
public abstract @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long delayTicks); | ||
|
||
public abstract @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long delayTicks); | ||
|
||
public abstract @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long delayTicks); | ||
|
||
@NotNull | ||
public abstract SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, SchedulerType schedulerType, long delay, long period) throws IllegalArgumentException, IllegalStateException; | ||
|
||
public abstract @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long initialDelayTicks, long periodTicks); | ||
|
||
public abstract @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long initialDelayTicks, long periodTicks); | ||
|
||
public abstract @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks); | ||
|
||
/** | ||
* Gets the task id for this runnable. | ||
* | ||
* @return the task id that this runnable was scheduled as | ||
* @throws IllegalStateException if task was not scheduled yet | ||
*/ | ||
protected synchronized int getTaskId() throws IllegalStateException { | ||
checkScheduled(); | ||
return task.getTaskId(); | ||
} | ||
|
||
protected void checkScheduled() { | ||
if (task == null) { | ||
throw new IllegalStateException("Not scheduled yet"); | ||
} | ||
} | ||
|
||
protected void checkNotYetScheduled() { | ||
if (task != null) { | ||
throw new IllegalStateException("Already scheduled as " + task.getTaskId()); | ||
} | ||
} | ||
|
||
@NotNull | ||
protected SchedulerTaskInter setupTask(@NotNull final SchedulerTaskInter task) { | ||
this.task = task; | ||
return task; | ||
} | ||
} |
6 changes: 6 additions & 0 deletions
6
src/main/java/fr/euphyllia/energie/model/SchedulerRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package fr.euphyllia.energie.model; | ||
|
||
public interface SchedulerRunnable { | ||
|
||
void run(); | ||
} |
159 changes: 159 additions & 0 deletions
159
src/main/java/fr/euphyllia/energie/utils/SchedulerTaskRunnable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,159 @@ | ||
package fr.euphyllia.energie.utils; | ||
|
||
import fr.euphyllia.energie.Energie; | ||
import fr.euphyllia.energie.folia.FoliaSchedulerTask; | ||
import fr.euphyllia.energie.legacy.LegacySchedulerTask; | ||
import fr.euphyllia.energie.model.*; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.Location; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.plugin.Plugin; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public abstract class SchedulerTaskRunnable extends AbstractSchedulerTaskRunnable implements SchedulerRunnable { | ||
|
||
@Override | ||
public void cancel() throws IllegalStateException { | ||
task.cancel(); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, SchedulerType schedulerType) throws IllegalArgumentException, IllegalStateException { | ||
checkNotYetScheduled(); | ||
if (Energie.isFolia()) { | ||
if (schedulerType.equals(SchedulerType.ASYNC)) { | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getAsyncScheduler().runNow(plugin, task1 -> this.run()), false)); | ||
} else { | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getGlobalRegionScheduler().run(plugin, task1 -> this.run()), true)); | ||
} | ||
} else { | ||
if (schedulerType.equals(SchedulerType.ASYNC)) { | ||
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskAsynchronously(plugin, this::run))); | ||
} else { | ||
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTask(plugin, this::run))); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().run(plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task1 -> this.run()), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().run(plugin, location, task1 -> this.run()), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runTask(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(entity.getScheduler().run(plugin, task1 -> this.run(), retired), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, SchedulerType schedulerType, long delay) throws IllegalArgumentException, IllegalStateException { | ||
checkNotYetScheduled(); | ||
if (Energie.isFolia()) { | ||
if (schedulerType.equals(SchedulerType.ASYNC)) { | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getAsyncScheduler().runDelayed(plugin, task1 -> this.run(), delay * 50, TimeUnit.MILLISECONDS), false)); | ||
} else { | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getGlobalRegionScheduler().runDelayed(plugin, task1 -> this.run(), delay), true)); | ||
} | ||
} else { | ||
if (schedulerType.equals(SchedulerType.ASYNC)) { | ||
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, this::run, delay))); | ||
} else { | ||
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskLater(plugin, this::run, delay))); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long delayTicks) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runDelayed(plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task1 -> this.run(), delayTicks), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long delayTicks) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runDelayed(plugin, location, task1 -> this.run(), delayTicks), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runDelayed(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long delayTicks) { | ||
checkNotYetScheduled(); | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(entity.getScheduler().runDelayed(plugin, task1 -> this.run(), retired, delayTicks), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, SchedulerType schedulerType, long delay, long period) throws IllegalArgumentException, IllegalStateException { | ||
checkNotYetScheduled(); | ||
if (Energie.isFolia()) { | ||
if (schedulerType.equals(SchedulerType.ASYNC)) { | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getAsyncScheduler().runAtFixedRate(plugin, task1 -> this.run(), delay * 50, period * 50, TimeUnit.MILLISECONDS), false)); | ||
} else { | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getGlobalRegionScheduler().runAtFixedRate(plugin, task1 -> this.run(), delay, period), true)); | ||
} | ||
} else { | ||
if (schedulerType.equals(SchedulerType.ASYNC)) { | ||
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, this::run, delay, period))); | ||
} else { | ||
return setupTask(new LegacySchedulerTask(Bukkit.getScheduler().runTaskTimer(plugin, this::run, delay, period))); | ||
} | ||
} | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, MultipleRecords.WorldChunk worldChunk, long initialDelayTicks, long periodTicks) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runAtFixedRate(plugin, worldChunk.world(), worldChunk.chunkX(), worldChunk.chunkZ(), task1 -> this.run(), initialDelayTicks, periodTicks), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Location location, long initialDelayTicks, long periodTicks) { | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(Bukkit.getRegionScheduler().runAtFixedRate(plugin, location, task1 -> this.run(), initialDelayTicks, periodTicks), true)); | ||
} | ||
|
||
@Override | ||
public @NotNull SchedulerTaskInter runAtFixedRate(@NotNull Plugin plugin, @NotNull SchedulerType schedulerType, Entity entity, @Nullable Runnable retired, long initialDelayTicks, long periodTicks) { | ||
checkNotYetScheduled(); | ||
if (!Energie.isFolia() || schedulerType.equals(SchedulerType.ASYNC)) { | ||
return runTask(plugin, schedulerType); | ||
} | ||
checkNotYetScheduled(); | ||
return setupTask(new FoliaSchedulerTask(entity.getScheduler().runAtFixedRate(plugin, task1 -> this.run(), retired, initialDelayTicks, periodTicks), true)); | ||
} | ||
} |