Skip to content

Commit

Permalink
GH-30 Add folia scheduler (#30)
Browse files Browse the repository at this point in the history
* [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]>
  • Loading branch information
vLuckyyy and Rollczi authored Sep 21, 2024
1 parent a52808d commit f2c6ed4
Show file tree
Hide file tree
Showing 12 changed files with 407 additions and 18 deletions.
4 changes: 2 additions & 2 deletions buildSrc/src/main/kotlin/commons-publish.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ plugins {
}

group = "com.eternalcode"
version = "1.1.3"
version = "1.1.4-SNAPSHOT"

java {
withSourcesJar()
withJavadocJar()
// withJavadocJar()
}

publishing {
Expand Down
2 changes: 1 addition & 1 deletion buildSrc/src/main/kotlin/commons-repositories.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ plugins {
repositories {
mavenCentral()

maven("https://papermc.io/repo/repository/maven-public/") // paper, adventure, velocity
maven("https://repo.papermc.io/repository/maven-public/") // paper, adventure, velocity
maven("https://hub.spigotmc.org/nexus/content/repositories/snapshots/") // spigot
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,54 +3,79 @@
import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.commons.scheduler.Task;
import java.util.concurrent.CompletableFuture;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;

import java.time.Duration;
import java.util.function.Supplier;

public class BukkitSchedulerImpl implements Scheduler {
public class BukkitSchedulerImpl implements MinecraftScheduler {

private final Plugin plugin;
private final Server server;
private final BukkitScheduler rootScheduler;

public BukkitSchedulerImpl(Plugin plugin) {
this.plugin = plugin;
this.server = plugin.getServer();
this.rootScheduler = plugin.getServer().getScheduler();
}

@Override
public Task sync(Runnable task) {
public boolean isGlobalTickThread() {
return this.server.isPrimaryThread();
}

@Override
public boolean isPrimaryThread() {
return this.server.isPrimaryThread();
}

@Override
public boolean isRegionThread(Entity entity) {
return this.server.isPrimaryThread();
}

@Override
public boolean isRegionThread(Location location) {
return this.server.isPrimaryThread();
}

@Override
public Task run(Runnable task) {
return new BukkitTaskImpl(this.rootScheduler.runTask(this.plugin, task));
}

@Override
public Task async(Runnable task) {
public Task runAsync(Runnable task) {
return new BukkitTaskImpl(this.rootScheduler.runTaskAsynchronously(this.plugin, task));
}

@Override
public Task laterSync(Runnable task, Duration delay) {
public Task runLater(Runnable task, Duration delay) {
return new BukkitTaskImpl(this.rootScheduler.runTaskLater(this.plugin, task, this.toTick(delay)));
}

@Override
public Task laterAsync(Runnable task, Duration delay) {
public Task runLaterAsync(Runnable task, Duration delay) {
return new BukkitTaskImpl(this.rootScheduler.runTaskLaterAsynchronously(this.plugin, task, this.toTick(delay)));
}

@Override
public Task timerSync(Runnable task, Duration delay, Duration period) {
return new BukkitTaskImpl(this.rootScheduler.runTaskTimer(this.plugin, task, this.toTick(delay), this.toTick(period)));
public Task timer(Runnable task, Duration delay, Duration period) {
return new BukkitTaskImpl(this.rootScheduler.runTaskTimer(this.plugin, task, this.toTick(delay), this.toTick(period)), true);
}

@Override
public Task timerAsync(Runnable task, Duration delay, Duration period) {
return new BukkitTaskImpl(this.rootScheduler.runTaskTimerAsynchronously(this.plugin, task, this.toTick(delay), this.toTick(period)));
return new BukkitTaskImpl(this.rootScheduler.runTaskTimerAsynchronously(this.plugin, task, this.toTick(delay), this.toTick(period)), true);
}

@Override
public <T> CompletableFuture<T> completeSync(Supplier<T> task) {
public <T> CompletableFuture<T> complete(Supplier<T> task) {
CompletableFuture<T> completable = new CompletableFuture<>();
this.rootScheduler.runTask(this.plugin, () -> completable.complete(task.get()));
return completable;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,24 @@
package com.eternalcode.commons.bukkit.scheduler;

import com.eternalcode.commons.scheduler.Task;
import org.bukkit.Bukkit;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitTask;

class BukkitTaskImpl implements Task {

private final BukkitTask rootTask;

private boolean isRepeating;

BukkitTaskImpl(BukkitTask rootTask) {
this.rootTask = rootTask;
this.isRepeating = false;
}

public BukkitTaskImpl(BukkitTask rootTask, boolean isRepeating) {
this.rootTask = rootTask;
this.isRepeating = isRepeating;
}

@Override
Expand All @@ -26,4 +36,17 @@ public boolean isAsync() {
return !this.rootTask.isSync();
}

@Override
public boolean isRunning() {
// There's no other way,
// there's no other way
// All that you can do is [...]]
// https://www.youtube.com/watch?v=LJzCYSdrHMI
return Bukkit.getServer().getScheduler().isCurrentlyRunning(this.rootTask.getTaskId());
}

@Override
public boolean isRepeating() {
return this.isRepeating;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package com.eternalcode.commons.bukkit.scheduler;

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.commons.scheduler.Task;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import org.bukkit.Location;
import org.bukkit.entity.Entity;

public interface MinecraftScheduler extends Scheduler {

boolean isGlobalTickThread();

boolean isPrimaryThread();

boolean isRegionThread(Entity entity);

boolean isRegionThread(Location location);

Task run(Runnable task);

Task runAsync(Runnable task);

default Task run(Location location, Runnable task) {
return run(task);
}

default Task run(Entity entity, Runnable task) {
return run(task);
}

Task runLater(Runnable task, Duration delay);

Task runLaterAsync(Runnable task, Duration delay);

default Task runLater(Location location, Runnable task, Duration delay) {
return runLater(task, delay);
}

default Task runLater(Entity entity, Runnable task, Duration delay) {
return runLater(task, delay);
}

Task timer(Runnable task, Duration delay, Duration period);

Task timerAsync(Runnable task, Duration delay, Duration period);

default Task timer(Location location, Runnable task, Duration delay, Duration period) {
return timer(task, delay, period);
}

default Task timer(Entity entity, Runnable task, Duration delay, Duration period) {
return timer(task, delay, period);
}

<T> CompletableFuture<T> complete(Supplier<T> task);

<T> CompletableFuture<T> completeAsync(Supplier<T> task);

default <T> CompletableFuture<T> complete(Location location, Supplier<T> task) {
return complete(task);
}

default <T> CompletableFuture<T> complete(Entity entity, Supplier<T> task) {
return complete(task);
}

}
20 changes: 20 additions & 0 deletions eternalcode-commons-folia/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
plugins {
`commons-java-17`
`commons-publish`
`commons-repositories`
`commons-java-unit-test`
}


dependencies {
api(project(":eternalcode-commons-shared"))
api(project(":eternalcode-commons-bukkit"))

compileOnlyApi("dev.folia:folia-api:1.20.1-R0.1-SNAPSHOT")

api("org.jetbrains:annotations:24.1.0")
}

tasks.test {
useJUnitPlatform()
}
Loading

0 comments on commit f2c6ed4

Please sign in to comment.