|
4 | 4 | import org.bukkit.Bukkit; |
5 | 5 | import org.bukkit.Location; |
6 | 6 | import org.jetbrains.annotations.NotNull; |
| 7 | +import org.jetbrains.annotations.Range; |
7 | 8 | import java.util.concurrent.Executors; |
8 | 9 | import java.util.concurrent.ScheduledExecutorService; |
9 | 10 | import java.util.concurrent.ScheduledFuture; |
@@ -33,51 +34,66 @@ public void run(@NotNull Runnable command) { |
33 | 34 | } |
34 | 35 |
|
35 | 36 | /** |
36 | | - * Run in next game tick |
37 | | - * @param command Task to execute |
| 37 | + * Run in game thread |
| 38 | + * @param command Task to execute |
| 39 | + * @param delayInTicks Delay in ticks (<code>-1</code> to run immediately if possible, <code>0</code> to run in next tick) |
38 | 40 | */ |
39 | | - public void runInGame(@NotNull Runnable command) { |
40 | | - // Are we already running in the expected thread? |
41 | | - boolean alreadyInThread = Internals.IS_FOLIA ? Bukkit.isGlobalTickThread() : Bukkit.isPrimaryThread(); |
42 | | - if (alreadyInThread) { |
43 | | - command.run(); |
44 | | - return; |
| 41 | + public void runInGame( |
| 42 | + @NotNull Runnable command, |
| 43 | + @Range(from = -1, to = Long.MAX_VALUE) long delayInTicks |
| 44 | + ) { |
| 45 | + // Attempt to run immediately if we're running in the target thread |
| 46 | + if (delayInTicks == -1) { |
| 47 | + boolean alreadyInThread = Internals.IS_FOLIA ? Bukkit.isGlobalTickThread() : Bukkit.isPrimaryThread(); |
| 48 | + if (alreadyInThread) { |
| 49 | + command.run(); |
| 50 | + return; |
| 51 | + } |
45 | 52 | } |
46 | 53 |
|
47 | 54 | // Run in game thread |
48 | 55 | YamipaPlugin plugin = YamipaPlugin.getInstance(); |
| 56 | + long effectiveDelayInTicks = Math.max(0, delayInTicks); |
49 | 57 | if (Internals.IS_FOLIA) { |
50 | | - Bukkit.getGlobalRegionScheduler().run(plugin, __ -> command.run()); |
| 58 | + Bukkit.getGlobalRegionScheduler().runDelayed(plugin, __ -> command.run(), effectiveDelayInTicks); |
51 | 59 | } else { |
52 | | - Bukkit.getScheduler().runTask(plugin, command); |
| 60 | + Bukkit.getScheduler().runTaskLater(plugin, command, effectiveDelayInTicks); |
53 | 61 | } |
54 | 62 | } |
55 | 63 |
|
56 | 64 | /** |
57 | | - * Run in next game tick |
58 | | - * @param command Task to execute |
59 | | - * @param location Location to find region task scheduler (for Folia) |
| 65 | + * Run in game thread |
| 66 | + * @param command Task to execute |
| 67 | + * @param location Location to find region task scheduler (for Folia) |
| 68 | + * @param delayInTicks Delay in ticks (<code>-1</code> to run immediately if possible, <code>0</code> to run in next tick) |
60 | 69 | */ |
61 | | - public void runInGame(@NotNull Runnable command, @NotNull Location location) { |
| 70 | + public void runInGame( |
| 71 | + @NotNull Runnable command, |
| 72 | + @NotNull Location location, |
| 73 | + @Range(from = -1, to = Long.MAX_VALUE) long delayInTicks |
| 74 | + ) { |
62 | 75 | Runnable wrappedCommand = () -> { |
63 | 76 | if (isRunning) { |
64 | 77 | command.run(); |
65 | 78 | } |
66 | 79 | }; |
67 | 80 |
|
68 | | - // Are we already running in the expected thread? |
69 | | - boolean alreadyInThread = Internals.IS_FOLIA ? Bukkit.isOwnedByCurrentRegion(location) : Bukkit.isPrimaryThread(); |
70 | | - if (alreadyInThread) { |
71 | | - wrappedCommand.run(); |
72 | | - return; |
| 81 | + // Attempt to run immediately if we're running in the target thread |
| 82 | + if (delayInTicks == -1) { |
| 83 | + boolean alreadyInThread = Internals.IS_FOLIA ? Bukkit.isOwnedByCurrentRegion(location) : Bukkit.isPrimaryThread(); |
| 84 | + if (alreadyInThread) { |
| 85 | + wrappedCommand.run(); |
| 86 | + return; |
| 87 | + } |
73 | 88 | } |
74 | 89 |
|
75 | 90 | // Run in another thread |
76 | 91 | YamipaPlugin plugin = YamipaPlugin.getInstance(); |
| 92 | + long effectiveDelayInTicks = Math.max(0, delayInTicks); |
77 | 93 | if (Internals.IS_FOLIA) { |
78 | | - Bukkit.getRegionScheduler().run(plugin, location, __ -> wrappedCommand.run()); |
| 94 | + Bukkit.getRegionScheduler().runDelayed(plugin, location, __ -> wrappedCommand.run(), effectiveDelayInTicks); |
79 | 95 | } else { |
80 | | - Bukkit.getScheduler().runTask(plugin, wrappedCommand); |
| 96 | + Bukkit.getScheduler().runTaskLater(plugin, wrappedCommand, effectiveDelayInTicks); |
81 | 97 | } |
82 | 98 | } |
83 | 99 |
|
|
0 commit comments