-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
178 additions
and
14 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
36 changes: 36 additions & 0 deletions
36
src/main/java/me/caseload/knockbacksync/scheduler/AbstractTaskHandle.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,36 @@ | ||
package me.caseload.knockbacksync.scheduler; | ||
|
||
import io.papermc.paper.threadedregions.scheduler.ScheduledTask; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.scheduler.BukkitTask; | ||
import org.jetbrains.annotations.NotNull; | ||
|
||
public class AbstractTaskHandle { | ||
private BukkitTask bukkitTask; | ||
private ScheduledTask scheduledTask; | ||
|
||
public AbstractTaskHandle(@NotNull BukkitTask bukkitTask) { | ||
this.bukkitTask = bukkitTask; | ||
} | ||
|
||
public AbstractTaskHandle(@NotNull ScheduledTask scheduledTask) { | ||
this.scheduledTask = scheduledTask; | ||
} | ||
|
||
public Plugin getOwner() { | ||
return this.bukkitTask != null ? this.bukkitTask.getOwner() : this.scheduledTask.getOwningPlugin(); | ||
} | ||
|
||
public boolean isCancelled() { | ||
return this.bukkitTask != null ? this.bukkitTask.isCancelled() : this.scheduledTask.isCancelled(); | ||
} | ||
|
||
public void cancel() { | ||
if (this.bukkitTask != null) { | ||
this.bukkitTask.cancel(); | ||
} else { | ||
this.scheduledTask.cancel(); | ||
} | ||
|
||
} | ||
} |
45 changes: 45 additions & 0 deletions
45
src/main/java/me/caseload/knockbacksync/scheduler/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,45 @@ | ||
package me.caseload.knockbacksync.scheduler; | ||
|
||
import org.bukkit.Bukkit; | ||
import org.bukkit.plugin.Plugin; | ||
import org.bukkit.scheduler.BukkitScheduler; | ||
|
||
public class BukkitSchedulerAdapter implements SchedulerAdapter { | ||
private final Plugin plugin; | ||
private final BukkitScheduler scheduler; | ||
|
||
public BukkitSchedulerAdapter(Plugin plugin) { | ||
this.plugin = plugin; | ||
this.scheduler = Bukkit.getScheduler(); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTask(Runnable task) { | ||
return new AbstractTaskHandle(scheduler.runTask(plugin, task)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskAsynchronously(Runnable task) { | ||
return new AbstractTaskHandle(scheduler.runTaskAsynchronously(plugin, task)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskLater(Runnable task, long delayTicks) { | ||
return new AbstractTaskHandle(scheduler.runTaskLater(plugin, task, delayTicks)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks) { | ||
return new AbstractTaskHandle(scheduler.runTaskTimer(plugin, task, delayTicks, periodTicks)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay) { | ||
return new AbstractTaskHandle(scheduler.runTaskLaterAsynchronously(plugin, task, delay)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period) { | ||
return new AbstractTaskHandle(scheduler.runTaskTimerAsynchronously(plugin, task, delay, period)); | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
src/main/java/me/caseload/knockbacksync/scheduler/FoliaSchedulerAdapter.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,56 @@ | ||
// FoliaSchedulerAdapter.java | ||
package me.caseload.knockbacksync.scheduler; | ||
|
||
import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler; | ||
import org.bukkit.Bukkit; | ||
import org.bukkit.plugin.Plugin; | ||
|
||
import java.lang.reflect.InvocationTargetException; | ||
import java.lang.reflect.Method; | ||
|
||
public class FoliaSchedulerAdapter implements SchedulerAdapter { | ||
private final Plugin plugin; | ||
private GlobalRegionScheduler scheduler = null; | ||
|
||
public FoliaSchedulerAdapter(Plugin plugin) { | ||
this.plugin = plugin; | ||
try { | ||
// Attempt to find and call the `getGlobalRegionScheduler` method | ||
Method getSchedulerMethod = Bukkit.getServer().getClass().getMethod("getGlobalRegionScheduler"); | ||
scheduler = (GlobalRegionScheduler) getSchedulerMethod.invoke(Bukkit.getServer()); | ||
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) { | ||
plugin.getLogger().severe("Failed to access GlobalRegionScheduler: " + e.getMessage()); | ||
} | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTask(Runnable task) { | ||
// scheduler.execute(plugin, task); | ||
return new AbstractTaskHandle(scheduler.run(plugin, scheduledTask -> task.run())); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskAsynchronously(Runnable task) { | ||
return new AbstractTaskHandle(scheduler.run(plugin, scheduledTask -> task.run())); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskLater(Runnable task, long delayTicks) { | ||
return new AbstractTaskHandle(scheduler.runDelayed(plugin, scheduledTask -> task.run(), delayTicks)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks) { | ||
return new AbstractTaskHandle(scheduler.runAtFixedRate(plugin, scheduledTask -> task.run(), delayTicks, periodTicks)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay) { | ||
return new AbstractTaskHandle(scheduler.runDelayed(plugin, scheduledTask -> task.run(), delay)); | ||
} | ||
|
||
@Override | ||
public AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period) { | ||
return new AbstractTaskHandle(scheduler.runAtFixedRate(plugin, scheduledTask -> task.run(), delay, period)); | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/me/caseload/knockbacksync/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,10 @@ | ||
package me.caseload.knockbacksync.scheduler; | ||
|
||
public interface SchedulerAdapter { | ||
AbstractTaskHandle runTask(Runnable task); | ||
AbstractTaskHandle runTaskAsynchronously(Runnable task); | ||
AbstractTaskHandle runTaskLater(Runnable task, long delayTicks); | ||
AbstractTaskHandle runTaskTimer(Runnable task, long delayTicks, long periodTicks); | ||
AbstractTaskHandle runTaskLaterAsynchronously(Runnable task, long delay); | ||
AbstractTaskHandle runTaskTimerAsynchronously(Runnable task, long delay, long period); | ||
} |
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