Skip to content

Commit a2fc9ad

Browse files
committed
generify PlatformScheduler
1 parent e4e3b6f commit a2fc9ad

File tree

11 files changed

+404
-490
lines changed

11 files changed

+404
-490
lines changed

ext/platform-scheduler/src/main/java/eu/cloudnetservice/ext/scheduler/BukkitPlatformScheduler.java

Lines changed: 14 additions & 135 deletions
Original file line numberDiff line numberDiff line change
@@ -16,153 +16,32 @@
1616

1717
package eu.cloudnetservice.ext.scheduler;
1818

19-
import java.util.concurrent.TimeUnit;
2019
import lombok.NonNull;
21-
import org.bukkit.Bukkit;
2220
import org.bukkit.World;
2321
import org.bukkit.entity.Entity;
2422
import org.bukkit.plugin.Plugin;
25-
import org.bukkit.scheduler.BukkitScheduler;
26-
import org.jetbrains.annotations.Nullable;
2723

2824
/**
29-
* Bukkit implementation of a platform scheduler.
25+
* Base type for all scheduler implementations that target a platform that implements the bukkit api.
3026
*
3127
* @since 4.0
3228
*/
33-
final class BukkitPlatformScheduler implements PlatformScheduler {
34-
35-
private static final BukkitScheduler SCHEDULER = Bukkit.getScheduler();
36-
37-
/**
38-
* {@inheritDoc}
39-
*/
40-
@Override
41-
public @NonNull ScheduledPlatformTask globalRunDelayed(
42-
@NonNull Plugin plugin,
43-
@NonNull Runnable task,
44-
long delayTicks
45-
) {
46-
var bukkitTask = SCHEDULER.runTaskLater(plugin, task, delayTicks);
47-
return new BukkitScheduledPlatformTask(bukkitTask);
48-
}
49-
50-
/**
51-
* {@inheritDoc}
52-
*/
53-
@Override
54-
public @NonNull ScheduledPlatformTask globalRunAtFixedRate(
55-
@NonNull Plugin plugin,
56-
@NonNull Runnable task,
57-
long initialDelayTicks,
58-
long periodTicks
59-
) {
60-
var bukkitTask = SCHEDULER.runTaskTimer(plugin, task, initialDelayTicks, periodTicks);
61-
return new BukkitScheduledPlatformTask(bukkitTask);
62-
}
63-
64-
/**
65-
* {@inheritDoc}
66-
*/
67-
@Override
68-
public @NonNull ScheduledPlatformTask regionRunDelayed(
69-
@NonNull Plugin plugin,
70-
@NonNull World world,
71-
int chunkX,
72-
int chunkZ,
73-
@NonNull Runnable task,
74-
long delayTicks
75-
) {
76-
return this.globalRunDelayed(plugin, task, delayTicks);
77-
}
78-
79-
/**
80-
* {@inheritDoc}
81-
*/
82-
@Override
83-
public @NonNull ScheduledPlatformTask regionRunAtFixedRate(
84-
@NonNull Plugin plugin,
85-
@NonNull World world,
86-
int chunkX,
87-
int chunkZ,
88-
@NonNull Runnable task,
89-
long initialDelayTicks,
90-
long periodTicks
91-
) {
92-
return this.globalRunAtFixedRate(plugin, task, initialDelayTicks, periodTicks);
93-
}
94-
95-
/**
96-
* {@inheritDoc}
97-
*/
98-
@Override
99-
public @NonNull ScheduledPlatformTask asyncRunNow(@NonNull Plugin plugin, @NonNull Runnable task) {
100-
var bukkitTask = SCHEDULER.runTaskAsynchronously(plugin, task);
101-
return new BukkitScheduledPlatformTask(bukkitTask);
102-
}
103-
104-
/**
105-
* {@inheritDoc}
106-
*/
107-
@Override
108-
public @NonNull ScheduledPlatformTask asyncRunDelayed(
109-
@NonNull Plugin plugin,
110-
@NonNull Runnable task,
111-
long delay,
112-
@NonNull TimeUnit unit
113-
) {
114-
var bukkitTask = SCHEDULER.runTaskLaterAsynchronously(
115-
plugin,
116-
task,
117-
unit.toMillis(delay) / 50);
118-
return new BukkitScheduledPlatformTask(bukkitTask);
119-
}
120-
121-
/**
122-
* {@inheritDoc}
123-
*/
124-
@Override
125-
public @NonNull ScheduledPlatformTask asyncRunAtFixedRate(
126-
@NonNull Plugin plugin,
127-
@NonNull Runnable task,
128-
long initialDelay,
129-
long period,
130-
@NonNull TimeUnit unit
131-
) {
132-
var bukkitTask = SCHEDULER.runTaskTimerAsynchronously(
133-
plugin,
134-
task,
135-
unit.toMillis(initialDelay) / 50,
136-
unit.toMillis(period) / 50);
137-
return new BukkitScheduledPlatformTask(bukkitTask);
138-
}
29+
public sealed interface BukkitPlatformScheduler
30+
extends PlatformScheduler<Plugin, World, Entity>
31+
permits RawBukkitPlatformScheduler, FoliaPlatformScheduler {
13932

14033
/**
141-
* {@inheritDoc}
34+
* Get the selected bukkit platform scheduler instance.
35+
*
36+
* @return the selected bukkit platform scheduler instance.
37+
* @throws UnsupportedOperationException if the current platform is not a bukkit platform.
14238
*/
143-
@Override
144-
public @Nullable ScheduledPlatformTask entityRunDelayed(
145-
@NonNull Entity entity,
146-
@NonNull Plugin plugin,
147-
@NonNull Runnable task,
148-
@Nullable Runnable retired,
149-
long delayTicks
150-
) {
151-
return this.globalRunDelayed(plugin, task, delayTicks);
152-
}
39+
static @NonNull BukkitPlatformScheduler bukkitScheduler() {
40+
var scheduler = PlatformScheduler.scheduler();
41+
if (!(scheduler instanceof BukkitPlatformScheduler bukkitPlatformScheduler)) {
42+
throw new UnsupportedOperationException("Selected platform scheduler impl is not a bukkit platform scheduler");
43+
}
15344

154-
/**
155-
* {@inheritDoc}
156-
*/
157-
@Override
158-
public @Nullable ScheduledPlatformTask entityRunAtFixedRate(
159-
@NonNull Entity entity,
160-
@NonNull Plugin plugin,
161-
@NonNull Runnable task,
162-
@Nullable Runnable retired,
163-
long initialDelayTicks,
164-
long periodTicks
165-
) {
166-
return this.globalRunAtFixedRate(plugin, task, initialDelayTicks, periodTicks);
45+
return bukkitPlatformScheduler;
16746
}
16847
}

ext/platform-scheduler/src/main/java/eu/cloudnetservice/ext/scheduler/BukkitScheduledPlatformTask.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

0 commit comments

Comments
 (0)