Skip to content

Commit af85f9b

Browse files
committed
Clarified scheduler behavior
- Updated Scheduler class - Updated usages of Scheduler
1 parent 391470f commit af85f9b

File tree

6 files changed

+44
-26
lines changed

6 files changed

+44
-26
lines changed

src/main/java/io/josemmo/bukkit/plugin/commands/ImageCommandBridge.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public static void register(@NotNull String commandName, @NotNull List<String> c
5353
fixPermissions(alias);
5454
}
5555
LOGGER.fine("Fixed command permissions");
56-
});
56+
}, 0);
5757
}
5858

5959
/**

src/main/java/io/josemmo/bukkit/plugin/renderer/ImageRenderer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,8 @@ public void onPlayerTeleport(@NotNull PlayerTeleportEvent event) {
454454
Location location = event.getTo();
455455
YamipaPlugin.getInstance().getScheduler().runInGame(
456456
() -> onPlayerLocationChange(event.getPlayer(), location),
457-
location
457+
location,
458+
0
458459
);
459460
}
460461

src/main/java/io/josemmo/bukkit/plugin/renderer/ItemService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,8 @@ public boolean onAttack(@NotNull Player player, @NotNull Block block, @NotNull B
222222
Location dropLocation = location.clone().add(0.5, -0.5, 0.5).add(face.getDirection());
223223
YamipaPlugin.getInstance().getScheduler().runInGame(
224224
() -> block.getWorld().dropItem(dropLocation, imageItem),
225-
dropLocation
225+
dropLocation,
226+
0
226227
);
227228
}
228229

src/main/java/io/josemmo/bukkit/plugin/utils/InteractWithEntityListener.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public final void onPacketReceiving(@NotNull PacketEvent event) {
6767
} finally {
6868
ProtocolLibrary.getProtocolManager().getAsynchronousManager().signalPacketTransmission(event);
6969
}
70-
}, event.getPlayer().getLocation());
70+
}, event.getPlayer().getLocation(), -1);
7171
}
7272

7373
/**

src/main/java/io/josemmo/bukkit/plugin/utils/Permissions.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ private static boolean queryGriefPrevention(@NotNull Player player, @NotNull Loc
160160
} catch (Exception e) {
161161
future.completeExceptionally(e);
162162
}
163-
}, location);
163+
}, location, -1);
164164

165165
// Get result
166166
try {

src/main/java/io/josemmo/bukkit/plugin/utils/Scheduler.java

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.bukkit.Bukkit;
55
import org.bukkit.Location;
66
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Range;
78
import java.util.concurrent.Executors;
89
import java.util.concurrent.ScheduledExecutorService;
910
import java.util.concurrent.ScheduledFuture;
@@ -33,51 +34,66 @@ public void run(@NotNull Runnable command) {
3334
}
3435

3536
/**
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)
3840
*/
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+
}
4552
}
4653

4754
// Run in game thread
4855
YamipaPlugin plugin = YamipaPlugin.getInstance();
56+
long effectiveDelayInTicks = Math.max(0, delayInTicks);
4957
if (Internals.IS_FOLIA) {
50-
Bukkit.getGlobalRegionScheduler().run(plugin, __ -> command.run());
58+
Bukkit.getGlobalRegionScheduler().runDelayed(plugin, __ -> command.run(), effectiveDelayInTicks);
5159
} else {
52-
Bukkit.getScheduler().runTask(plugin, command);
60+
Bukkit.getScheduler().runTaskLater(plugin, command, effectiveDelayInTicks);
5361
}
5462
}
5563

5664
/**
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)
6069
*/
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+
) {
6275
Runnable wrappedCommand = () -> {
6376
if (isRunning) {
6477
command.run();
6578
}
6679
};
6780

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+
}
7388
}
7489

7590
// Run in another thread
7691
YamipaPlugin plugin = YamipaPlugin.getInstance();
92+
long effectiveDelayInTicks = Math.max(0, delayInTicks);
7793
if (Internals.IS_FOLIA) {
78-
Bukkit.getRegionScheduler().run(plugin, location, __ -> wrappedCommand.run());
94+
Bukkit.getRegionScheduler().runDelayed(plugin, location, __ -> wrappedCommand.run(), effectiveDelayInTicks);
7995
} else {
80-
Bukkit.getScheduler().runTask(plugin, wrappedCommand);
96+
Bukkit.getScheduler().runTaskLater(plugin, wrappedCommand, effectiveDelayInTicks);
8197
}
8298
}
8399

0 commit comments

Comments
 (0)