Skip to content

Commit a6122cb

Browse files
committed
Replace more instances with agnostic scheduler
1 parent 65a9ef5 commit a6122cb

File tree

12 files changed

+191
-34
lines changed

12 files changed

+191
-34
lines changed

src/main/java/com/comphenix/protocol/ProtocolLib.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@
2525
import com.comphenix.protocol.injector.InternalManager;
2626
import com.comphenix.protocol.injector.PacketFilterManager;
2727
import com.comphenix.protocol.metrics.Statistics;
28+
import com.comphenix.protocol.scheduler.DefaultScheduler;
29+
import com.comphenix.protocol.scheduler.FoliaScheduler;
30+
import com.comphenix.protocol.scheduler.ProtocolScheduler;
31+
import com.comphenix.protocol.scheduler.Task;
2832
import com.comphenix.protocol.updater.Updater;
2933
import com.comphenix.protocol.updater.Updater.UpdateType;
3034
import com.comphenix.protocol.utility.*;
@@ -34,6 +38,7 @@
3438
import java.io.File;
3539
import java.io.IOException;
3640
import java.util.HashSet;
41+
import java.util.Optional;
3742
import java.util.Set;
3843
import java.util.concurrent.TimeUnit;
3944
import java.util.logging.Handler;
@@ -90,20 +95,21 @@ public class ProtocolLib extends JavaPlugin {
9095
// these fields are only existing once, we can make them static
9196
private static Logger logger;
9297
private static ProtocolConfig config;
93-
9498
private static InternalManager protocolManager;
9599
private static ErrorReporter reporter = new BasicErrorReporter();
96100

97101
private Statistics statistics;
98102

99-
private int packetTask = -1;
103+
private Task packetTask = null;
100104
private int tickCounter = 0;
101105
private int configExpectedMod = -1;
102106

103107
// updater
104108
private Updater updater;
105109
private Handler redirectHandler;
106110

111+
private ProtocolScheduler scheduler;
112+
107113
// commands
108114
private CommandProtocol commandProtocol;
109115
private CommandPacket commandPacket;
@@ -155,6 +161,10 @@ public void onLoad() {
155161
}
156162

157163
try {
164+
this.scheduler = Util.isUsingFolia()
165+
? new FoliaScheduler(this)
166+
: new DefaultScheduler(this);
167+
158168
// Check for other versions
159169
this.checkConflictingVersions();
160170

@@ -171,7 +181,7 @@ public void onLoad() {
171181
.minecraftVersion(version)
172182
.reporter(reporter)
173183
.build();
174-
ProtocolLibrary.init(this, config, protocolManager, reporter);
184+
ProtocolLibrary.init(this, config, protocolManager, scheduler, reporter);
175185

176186
// Setup error reporter
177187
detailedReporter.addGlobalParameter("manager", protocolManager);
@@ -483,12 +493,12 @@ private void disablePlugin() {
483493

484494
private void createPacketTask(Server server) {
485495
try {
486-
if (this.packetTask >= 0) {
496+
if (this.packetTask != null) {
487497
throw new IllegalStateException("Packet task has already been created");
488498
}
489499

490500
// Attempt to create task
491-
this.packetTask = SchedulerUtil.scheduleSyncRepeatingTask(this, () -> {
501+
this.packetTask = scheduler.scheduleSyncRepeatingTask(() -> {
492502
AsyncFilterManager manager = (AsyncFilterManager) protocolManager.getAsynchronousManager();
493503
// We KNOW we're on the main thread at the moment
494504
manager.sendProcessedPackets(ProtocolLib.this.tickCounter++, true);
@@ -504,7 +514,7 @@ private void createPacketTask(Server server) {
504514
} catch (OutOfMemoryError e) {
505515
throw e;
506516
} catch (Throwable e) {
507-
if (this.packetTask == -1) {
517+
if (this.packetTask == null) {
508518
reporter.reportDetailed(this, Report.newBuilder(REPORT_CANNOT_CREATE_TIMEOUT_TASK).error(e));
509519
}
510520
}
@@ -563,9 +573,9 @@ public void onDisable() {
563573
}
564574

565575
// Clean up
566-
if (this.packetTask >= 0) {
567-
SchedulerUtil.cancelTask(this, packetTask);
568-
this.packetTask = -1;
576+
if (this.packetTask != null) {
577+
packetTask.cancel();
578+
this.packetTask = null;
569579
}
570580

571581
// And redirect handler too
@@ -601,6 +611,10 @@ public ProtocolConfig getProtocolConfig() {
601611
return config;
602612
}
603613

614+
public ProtocolScheduler getScheduler() {
615+
return scheduler;
616+
}
617+
604618
// Different commands
605619
private enum ProtocolCommand {
606620
FILTER,

src/main/java/com/comphenix/protocol/ProtocolLibrary.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import com.comphenix.protocol.error.BasicErrorReporter;
1818
import com.comphenix.protocol.error.ErrorReporter;
19+
import com.comphenix.protocol.scheduler.ProtocolScheduler;
1920
import com.comphenix.protocol.utility.MinecraftVersion;
2021
import java.util.List;
2122
import com.google.common.collect.ImmutableList;
@@ -51,17 +52,20 @@ public class ProtocolLibrary {
5152
private static Plugin plugin;
5253
private static ProtocolConfig config;
5354
private static ProtocolManager manager;
55+
private static ProtocolScheduler scheduler;
5456
private static ErrorReporter reporter = new BasicErrorReporter();
5557

5658
private static boolean updatesDisabled;
5759
private static boolean initialized;
5860

59-
protected static void init(Plugin plugin, ProtocolConfig config, ProtocolManager manager, ErrorReporter reporter) {
61+
protected static void init(Plugin plugin, ProtocolConfig config, ProtocolManager manager,
62+
ProtocolScheduler scheduler, ErrorReporter reporter) {
6063
Validate.isTrue(!initialized, "ProtocolLib has already been initialized.");
6164
ProtocolLibrary.plugin = plugin;
6265
ProtocolLibrary.config = config;
6366
ProtocolLibrary.manager = manager;
6467
ProtocolLibrary.reporter = reporter;
68+
ProtocolLibrary.scheduler = scheduler;
6569
initialized = true;
6670
}
6771

@@ -89,6 +93,10 @@ public static ProtocolManager getProtocolManager() {
8993
return manager;
9094
}
9195

96+
public static ProtocolScheduler getScheduler() {
97+
return scheduler;
98+
}
99+
92100
/**
93101
* Retrieve the current error reporter.
94102
* @return Current error reporter.

src/main/java/com/comphenix/protocol/async/AsyncFilterManager.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@
3333
import com.comphenix.protocol.events.PacketListener;
3434
import com.comphenix.protocol.injector.PrioritizedListener;
3535
import com.comphenix.protocol.injector.SortedPacketListenerList;
36+
import com.comphenix.protocol.scheduler.ProtocolScheduler;
3637
import com.google.common.base.Objects;
3738
import com.google.common.collect.ImmutableSet;
3839
import com.google.common.collect.Iterables;
3940

4041
import org.bukkit.entity.Player;
4142
import org.bukkit.plugin.Plugin;
42-
import org.bukkit.scheduler.BukkitScheduler;
4343

4444
/**
4545
* Represents a filter manager for asynchronous packets.
@@ -67,7 +67,7 @@ public class AsyncFilterManager implements AsynchronousManager {
6767
private final Thread mainThread;
6868

6969
// Default scheduler
70-
private final BukkitScheduler scheduler;
70+
private final ProtocolScheduler scheduler;
7171

7272
// Current packet index
7373
private final AtomicInteger currentSendingIndex = new AtomicInteger();
@@ -82,7 +82,7 @@ public class AsyncFilterManager implements AsynchronousManager {
8282
* @param reporter - desired error reporter.
8383
* @param scheduler - task scheduler.
8484
*/
85-
public AsyncFilterManager(ErrorReporter reporter, BukkitScheduler scheduler) {
85+
public AsyncFilterManager(ErrorReporter reporter, ProtocolScheduler scheduler) {
8686
// Initialize timeout listeners
8787
this.serverTimeoutListeners = new SortedPacketListenerList();
8888
this.clientTimeoutListeners = new SortedPacketListenerList();
@@ -340,7 +340,7 @@ public Set<PacketType> getSendingTypes() {
340340
* Retrieve the current task scheduler.
341341
* @return Current task scheduler.
342342
*/
343-
public BukkitScheduler getScheduler() {
343+
public ProtocolScheduler getScheduler() {
344344
return scheduler;
345345
}
346346

src/main/java/com/comphenix/protocol/async/AsyncListenerHandler.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import com.comphenix.protocol.events.PacketAdapter;
3232
import com.comphenix.protocol.events.PacketEvent;
3333
import com.comphenix.protocol.events.PacketListener;
34+
import com.comphenix.protocol.scheduler.Task;
3435
import com.comphenix.protocol.timing.TimedListenerManager;
3536
import com.comphenix.protocol.timing.TimedListenerManager.ListenerType;
3637
import com.comphenix.protocol.timing.TimedTracker;
@@ -91,13 +92,13 @@ public class AsyncListenerHandler {
9192
private final Object stopLock = new Object();
9293

9394
// Processing task on the main thread
94-
private int syncTask = -1;
95+
private Task syncTask = null;
9596

9697
// Minecraft main thread
9798
private Thread mainThread;
9899

99100
// Warn plugins that the async listener handler must be started
100-
private int warningTask;
101+
private Task warningTask;
101102

102103
// Timing manager
103104
private TimedListenerManager timedManager = TimedListenerManager.getInstance();
@@ -121,19 +122,16 @@ public class AsyncListenerHandler {
121122
}
122123

123124
private void startWarningTask() {
124-
warningTask = filterManager.getScheduler().scheduleSyncDelayedTask(getPlugin(), () -> ProtocolLibrary.getErrorReporter().reportWarning(AsyncListenerHandler.this, Report.
125+
warningTask = filterManager.getScheduler().scheduleSyncDelayedTask(() -> ProtocolLibrary.getErrorReporter().reportWarning(AsyncListenerHandler.this, Report.
125126
newBuilder(REPORT_HANDLER_NOT_STARTED).
126127
messageParam(listener.getPlugin(), AsyncListenerHandler.this).
127128
build()), 2 * TICKS_PER_SECOND);
128129
}
129130

130131
private void stopWarningTask() {
131-
int taskId = warningTask;
132-
133-
// Ensure we have a task to cancel
134-
if (warningTask >= 0) {
135-
filterManager.getScheduler().cancelTask(taskId);
136-
warningTask = -1;
132+
if (warningTask != null) {
133+
warningTask.cancel();
134+
warningTask = null;
137135
}
138136
}
139137

@@ -406,10 +404,10 @@ public synchronized boolean syncStart(final long time, final TimeUnit unit) {
406404
final long tickDelay = 1;
407405
final int workerID = nextID.incrementAndGet();
408406

409-
if (syncTask < 0) {
407+
if (syncTask == null) {
410408
stopWarningTask();
411409

412-
syncTask = filterManager.getScheduler().scheduleSyncRepeatingTask(getPlugin(), () -> {
410+
syncTask = filterManager.getScheduler().scheduleSyncRepeatingTask(() -> {
413411
long stopTime = System.nanoTime() + unit.convert(time, TimeUnit.NANOSECONDS);
414412

415413
while (!cancelled) {
@@ -435,7 +433,7 @@ public synchronized boolean syncStart(final long time, final TimeUnit unit) {
435433
}, tickDelay, tickDelay);
436434

437435
// This is very bad - force the caller to handle it
438-
if (syncTask < 0)
436+
if (syncTask == null)
439437
throw new IllegalStateException("Cannot start synchronous task.");
440438
else
441439
return true;
@@ -449,10 +447,9 @@ public synchronized boolean syncStart(final long time, final TimeUnit unit) {
449447
* @return TRUE if we stopped any processing tasks, FALSE if it has already been stopped.
450448
*/
451449
public synchronized boolean syncStop() {
452-
if (syncTask > 0) {
453-
filterManager.getScheduler().cancelTask(syncTask);
454-
455-
syncTask = -1;
450+
if (syncTask != null) {
451+
syncTask.cancel();
452+
syncTask = null;
456453
return true;
457454
} else {
458455
return false;

src/main/java/com/comphenix/protocol/injector/PacketFilterBuilder.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.comphenix.protocol.injector;
22

3+
import com.comphenix.protocol.ProtocolLib;
34
import com.comphenix.protocol.async.AsyncFilterManager;
45
import com.comphenix.protocol.error.ErrorReporter;
56
import com.comphenix.protocol.utility.MinecraftVersion;
@@ -10,7 +11,7 @@
1011
public class PacketFilterBuilder {
1112

1213
private Server server;
13-
private Plugin library;
14+
private ProtocolLib library;
1415
private MinecraftVersion mcVersion;
1516
private ErrorReporter reporter;
1617
private AsyncFilterManager asyncManager;
@@ -32,7 +33,7 @@ public PacketFilterBuilder server(@Nonnull Server server) {
3233
* @param library - plugin instance.
3334
* @return This builder, for chaining.
3435
*/
35-
public PacketFilterBuilder library(@Nonnull Plugin library) {
36+
public PacketFilterBuilder library(@Nonnull ProtocolLib library) {
3637
this.library = library;
3738
return this;
3839
}
@@ -116,7 +117,7 @@ public InternalManager build() {
116117
throw new IllegalArgumentException("reporter cannot be NULL.");
117118
}
118119

119-
this.asyncManager = new AsyncFilterManager(this.reporter, this.server.getScheduler());
120+
this.asyncManager = new AsyncFilterManager(this.reporter, this.library.getScheduler());
120121
return new PacketFilterManager(this);
121122
}
122123
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.comphenix.protocol.scheduler;
2+
3+
import org.bukkit.plugin.Plugin;
4+
import org.bukkit.scheduler.BukkitScheduler;
5+
6+
public class DefaultScheduler implements ProtocolScheduler {
7+
private final Plugin plugin;
8+
private final BukkitScheduler scheduler;
9+
10+
public DefaultScheduler(Plugin plugin) {
11+
this.plugin = plugin;
12+
this.scheduler = plugin.getServer().getScheduler();
13+
}
14+
15+
@Override
16+
public Task scheduleSyncRepeatingTask(Runnable task, long delay, long period) {
17+
int taskId = scheduler.scheduleSyncRepeatingTask(plugin, task, delay, period);
18+
return taskId >= 0 ? new DefaultTask(scheduler, taskId) : null;
19+
}
20+
21+
@Override
22+
public Task runTask(Runnable task) {
23+
int taskId = scheduler.runTask(plugin, task).getTaskId();
24+
return taskId >= 0 ? new DefaultTask(scheduler, taskId) : null;
25+
}
26+
27+
@Override
28+
public Task scheduleSyncDelayedTask(Runnable task, long delay) {
29+
int taskId = scheduler.scheduleSyncDelayedTask(plugin, task, delay);
30+
return taskId >= 0 ? new DefaultTask(scheduler, taskId) : null;
31+
}
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.comphenix.protocol.scheduler;
2+
3+
import org.bukkit.scheduler.BukkitScheduler;
4+
import org.bukkit.scheduler.BukkitTask;
5+
6+
public class DefaultTask implements Task {
7+
private final int taskId;
8+
private final BukkitScheduler scheduler;
9+
10+
public DefaultTask(BukkitScheduler scheduler, int taskId) {
11+
this.taskId = taskId;
12+
this.scheduler = scheduler;
13+
}
14+
15+
@Override
16+
public void cancel() {
17+
scheduler.cancelTask(taskId);
18+
}
19+
}

0 commit comments

Comments
 (0)