Skip to content

Commit f2c6ed4

Browse files
vLuckyyyRollczi
andauthored
GH-30 Add folia scheduler (#30)
* [WIP] Add folia scheduler basics * Fix folia api. * Add entity, region, location. scheduelrs. * Implement bukkit scheduler. * Add MinecraftScheduler. * Fix imports * Remove getPlugin method --------- Co-authored-by: Rollczi <[email protected]>
1 parent a52808d commit f2c6ed4

File tree

12 files changed

+407
-18
lines changed

12 files changed

+407
-18
lines changed

buildSrc/src/main/kotlin/commons-publish.gradle.kts

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ plugins {
44
}
55

66
group = "com.eternalcode"
7-
version = "1.1.3"
7+
version = "1.1.4-SNAPSHOT"
88

99
java {
1010
withSourcesJar()
11-
withJavadocJar()
11+
// withJavadocJar()
1212
}
1313

1414
publishing {

buildSrc/src/main/kotlin/commons-repositories.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,6 @@ plugins {
55
repositories {
66
mavenCentral()
77

8-
maven("https://papermc.io/repo/repository/maven-public/") // paper, adventure, velocity
8+
maven("https://repo.papermc.io/repository/maven-public/") // paper, adventure, velocity
99
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") // spigot
1010
}

eternalcode-commons-bukkit/src/main/java/com/eternalcode/commons/bukkit/scheduler/BukkitSchedulerImpl.java

+34-9
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,79 @@
33
import com.eternalcode.commons.scheduler.Scheduler;
44
import com.eternalcode.commons.scheduler.Task;
55
import java.util.concurrent.CompletableFuture;
6+
import org.bukkit.Location;
7+
import org.bukkit.Server;
8+
import org.bukkit.entity.Entity;
69
import org.bukkit.plugin.Plugin;
710
import org.bukkit.scheduler.BukkitScheduler;
811

912
import java.time.Duration;
1013
import java.util.function.Supplier;
1114

12-
public class BukkitSchedulerImpl implements Scheduler {
15+
public class BukkitSchedulerImpl implements MinecraftScheduler {
1316

1417
private final Plugin plugin;
18+
private final Server server;
1519
private final BukkitScheduler rootScheduler;
1620

1721
public BukkitSchedulerImpl(Plugin plugin) {
1822
this.plugin = plugin;
23+
this.server = plugin.getServer();
1924
this.rootScheduler = plugin.getServer().getScheduler();
2025
}
2126

2227
@Override
23-
public Task sync(Runnable task) {
28+
public boolean isGlobalTickThread() {
29+
return this.server.isPrimaryThread();
30+
}
31+
32+
@Override
33+
public boolean isPrimaryThread() {
34+
return this.server.isPrimaryThread();
35+
}
36+
37+
@Override
38+
public boolean isRegionThread(Entity entity) {
39+
return this.server.isPrimaryThread();
40+
}
41+
42+
@Override
43+
public boolean isRegionThread(Location location) {
44+
return this.server.isPrimaryThread();
45+
}
46+
47+
@Override
48+
public Task run(Runnable task) {
2449
return new BukkitTaskImpl(this.rootScheduler.runTask(this.plugin, task));
2550
}
2651

2752
@Override
28-
public Task async(Runnable task) {
53+
public Task runAsync(Runnable task) {
2954
return new BukkitTaskImpl(this.rootScheduler.runTaskAsynchronously(this.plugin, task));
3055
}
3156

3257
@Override
33-
public Task laterSync(Runnable task, Duration delay) {
58+
public Task runLater(Runnable task, Duration delay) {
3459
return new BukkitTaskImpl(this.rootScheduler.runTaskLater(this.plugin, task, this.toTick(delay)));
3560
}
3661

3762
@Override
38-
public Task laterAsync(Runnable task, Duration delay) {
63+
public Task runLaterAsync(Runnable task, Duration delay) {
3964
return new BukkitTaskImpl(this.rootScheduler.runTaskLaterAsynchronously(this.plugin, task, this.toTick(delay)));
4065
}
4166

4267
@Override
43-
public Task timerSync(Runnable task, Duration delay, Duration period) {
44-
return new BukkitTaskImpl(this.rootScheduler.runTaskTimer(this.plugin, task, this.toTick(delay), this.toTick(period)));
68+
public Task timer(Runnable task, Duration delay, Duration period) {
69+
return new BukkitTaskImpl(this.rootScheduler.runTaskTimer(this.plugin, task, this.toTick(delay), this.toTick(period)), true);
4570
}
4671

4772
@Override
4873
public Task timerAsync(Runnable task, Duration delay, Duration period) {
49-
return new BukkitTaskImpl(this.rootScheduler.runTaskTimerAsynchronously(this.plugin, task, this.toTick(delay), this.toTick(period)));
74+
return new BukkitTaskImpl(this.rootScheduler.runTaskTimerAsynchronously(this.plugin, task, this.toTick(delay), this.toTick(period)), true);
5075
}
5176

5277
@Override
53-
public <T> CompletableFuture<T> completeSync(Supplier<T> task) {
78+
public <T> CompletableFuture<T> complete(Supplier<T> task) {
5479
CompletableFuture<T> completable = new CompletableFuture<>();
5580
this.rootScheduler.runTask(this.plugin, () -> completable.complete(task.get()));
5681
return completable;

eternalcode-commons-bukkit/src/main/java/com/eternalcode/commons/bukkit/scheduler/BukkitTaskImpl.java

+23
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,24 @@
11
package com.eternalcode.commons.bukkit.scheduler;
22

33
import com.eternalcode.commons.scheduler.Task;
4+
import org.bukkit.Bukkit;
5+
import org.bukkit.plugin.Plugin;
46
import org.bukkit.scheduler.BukkitTask;
57

68
class BukkitTaskImpl implements Task {
79

810
private final BukkitTask rootTask;
911

12+
private boolean isRepeating;
13+
1014
BukkitTaskImpl(BukkitTask rootTask) {
1115
this.rootTask = rootTask;
16+
this.isRepeating = false;
17+
}
18+
19+
public BukkitTaskImpl(BukkitTask rootTask, boolean isRepeating) {
20+
this.rootTask = rootTask;
21+
this.isRepeating = isRepeating;
1222
}
1323

1424
@Override
@@ -26,4 +36,17 @@ public boolean isAsync() {
2636
return !this.rootTask.isSync();
2737
}
2838

39+
@Override
40+
public boolean isRunning() {
41+
// There's no other way,
42+
// there's no other way
43+
// All that you can do is [...]]
44+
// https://www.youtube.com/watch?v=LJzCYSdrHMI
45+
return Bukkit.getServer().getScheduler().isCurrentlyRunning(this.rootTask.getTaskId());
46+
}
47+
48+
@Override
49+
public boolean isRepeating() {
50+
return this.isRepeating;
51+
}
2952
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.eternalcode.commons.bukkit.scheduler;
2+
3+
import com.eternalcode.commons.scheduler.Scheduler;
4+
import com.eternalcode.commons.scheduler.Task;
5+
import java.time.Duration;
6+
import java.util.concurrent.CompletableFuture;
7+
import java.util.function.Supplier;
8+
import org.bukkit.Location;
9+
import org.bukkit.entity.Entity;
10+
11+
public interface MinecraftScheduler extends Scheduler {
12+
13+
boolean isGlobalTickThread();
14+
15+
boolean isPrimaryThread();
16+
17+
boolean isRegionThread(Entity entity);
18+
19+
boolean isRegionThread(Location location);
20+
21+
Task run(Runnable task);
22+
23+
Task runAsync(Runnable task);
24+
25+
default Task run(Location location, Runnable task) {
26+
return run(task);
27+
}
28+
29+
default Task run(Entity entity, Runnable task) {
30+
return run(task);
31+
}
32+
33+
Task runLater(Runnable task, Duration delay);
34+
35+
Task runLaterAsync(Runnable task, Duration delay);
36+
37+
default Task runLater(Location location, Runnable task, Duration delay) {
38+
return runLater(task, delay);
39+
}
40+
41+
default Task runLater(Entity entity, Runnable task, Duration delay) {
42+
return runLater(task, delay);
43+
}
44+
45+
Task timer(Runnable task, Duration delay, Duration period);
46+
47+
Task timerAsync(Runnable task, Duration delay, Duration period);
48+
49+
default Task timer(Location location, Runnable task, Duration delay, Duration period) {
50+
return timer(task, delay, period);
51+
}
52+
53+
default Task timer(Entity entity, Runnable task, Duration delay, Duration period) {
54+
return timer(task, delay, period);
55+
}
56+
57+
<T> CompletableFuture<T> complete(Supplier<T> task);
58+
59+
<T> CompletableFuture<T> completeAsync(Supplier<T> task);
60+
61+
default <T> CompletableFuture<T> complete(Location location, Supplier<T> task) {
62+
return complete(task);
63+
}
64+
65+
default <T> CompletableFuture<T> complete(Entity entity, Supplier<T> task) {
66+
return complete(task);
67+
}
68+
69+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
plugins {
2+
`commons-java-17`
3+
`commons-publish`
4+
`commons-repositories`
5+
`commons-java-unit-test`
6+
}
7+
8+
9+
dependencies {
10+
api(project(":eternalcode-commons-shared"))
11+
api(project(":eternalcode-commons-bukkit"))
12+
13+
compileOnlyApi("dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT")
14+
15+
api("org.jetbrains:annotations:24.1.0")
16+
}
17+
18+
tasks.test {
19+
useJUnitPlatform()
20+
}

0 commit comments

Comments
 (0)