-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Folia support via reflections to support older versions.
- Loading branch information
Showing
10 changed files
with
316 additions
and
31 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
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
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
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
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
70 changes: 70 additions & 0 deletions
70
src/main/java/eu/decentsoftware/holograms/api/utils/scheduler/SchedulerAdapter.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,70 @@ | ||
package eu.decentsoftware.holograms.api.utils.scheduler; | ||
|
||
import org.bukkit.entity.Entity; | ||
import org.bukkit.scheduler.BukkitTask; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
public interface SchedulerAdapter { | ||
|
||
/** | ||
* Schedules the specified task to be executed asynchronously immediately. | ||
* | ||
* @param runnable The task to execute. | ||
*/ | ||
void runAsync(Runnable runnable); | ||
|
||
/** | ||
* Schedules the specified task to be executed asynchronously after the time delay has passed. | ||
* | ||
* @param runnable The task to execute. | ||
* @param delay The time delay to pass before the task should be executed. | ||
* @param unit The time unit for the initial delay and period. | ||
*/ | ||
void runAsyncDelayed(Runnable runnable, long delay, TimeUnit unit); | ||
|
||
/** | ||
* Schedules the specified task to be executed asynchronously after the delay has passed, | ||
* and then periodically executed with the specified period. | ||
* | ||
* @param runnable The task to execute. | ||
* @param delay The time delay to pass before the task should be executed. | ||
* @param period The time between task executions after the first execution of the task. | ||
* @param unit The time unit for the initial delay and period. | ||
* @return The BukkitTask that represents the scheduled task. | ||
*/ | ||
BukkitTask runAsyncRate(Runnable runnable, long delay, long period, TimeUnit unit); | ||
|
||
/** | ||
* Schedules a task. If the task failed to schedule because the scheduler is retired (entity removed), | ||
* then returns {@code false}. Otherwise, either the run callback will be invoked after the specified delay, | ||
* or the retired callback will be invoked if the scheduler is retired. | ||
* Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, | ||
* remove other entities, load chunks, load worlds, modify ticket levels, etc. | ||
* | ||
* <p> | ||
* It is guaranteed that the task and retired callback are invoked on the region which owns the entity. | ||
* </p> | ||
* | ||
* @param entity The entity relative to which the scheduler is obtained. | ||
* @param runnable The task to execute. | ||
*/ | ||
void executeAtEntity(Entity entity, Runnable runnable); | ||
|
||
/** | ||
* Schedules a task with the given delay. If the task failed to schedule because the scheduler is retired (entity removed), | ||
* then returns {@code false}. Otherwise, either the run callback will be invoked after the specified delay, | ||
* or the retired callback will be invoked if the scheduler is retired. | ||
* Note that the retired callback is invoked in critical code, so it should not attempt to remove the entity, | ||
* remove other entities, load chunks, load worlds, modify ticket levels, etc. | ||
* | ||
* <p> | ||
* It is guaranteed that the task and retired callback are invoked on the region which owns the entity. | ||
* </p> | ||
* | ||
* @param entity The entity relative to which the scheduler is obtained. | ||
* @param runnable The task to execute. | ||
* @param delay The time delay to pass before the task should be executed, in ticks. | ||
*/ | ||
void runAtEntityDelayed(Entity entity, Runnable runnable, long delay); | ||
} |
42 changes: 42 additions & 0 deletions
42
...java/eu/decentsoftware/holograms/api/utils/scheduler/adapters/BukkitSchedulerAdapter.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,42 @@ | ||
package eu.decentsoftware.holograms.api.utils.scheduler.adapters; | ||
|
||
import eu.decentsoftware.holograms.api.utils.scheduler.SchedulerAdapter; | ||
import lombok.NonNull; | ||
import lombok.RequiredArgsConstructor; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.entity.Entity; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.scheduler.BukkitTask; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@RequiredArgsConstructor | ||
public class BukkitSchedulerAdapter implements SchedulerAdapter { | ||
|
||
private @NonNull Plugin plugin; | ||
|
||
@Override | ||
public void runAsync(Runnable runnable) { | ||
Bukkit.getScheduler().runTaskAsynchronously(plugin, runnable); | ||
} | ||
|
||
@Override | ||
public void runAsyncDelayed(Runnable runnable, long delay, TimeUnit unit) { | ||
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, runnable, unit.toMillis(delay) / 50); | ||
} | ||
|
||
@Override | ||
public BukkitTask runAsyncRate(Runnable runnable, long delay, long period, TimeUnit unit) { | ||
return Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, runnable, unit.toMillis(delay) / 50, unit.toMillis(period) / 50); | ||
} | ||
|
||
@Override | ||
public void executeAtEntity(Entity entity, Runnable runnable) { | ||
Bukkit.getScheduler().runTask(plugin, runnable); | ||
} | ||
|
||
@Override | ||
public void runAtEntityDelayed(Entity entity, Runnable runnable, long delay) { | ||
Bukkit.getScheduler().runTaskLater(plugin, runnable, delay); | ||
} | ||
} |
Oops, something went wrong.