Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,6 +3,9 @@
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;

Expand All @@ -12,13 +15,35 @@
public class BukkitSchedulerImpl implements Scheduler {

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 boolean isGlobal() {
return this.server.isPrimaryThread();
}

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

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

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

@Override
public Task sync(Runnable task) {
return new BukkitTaskImpl(this.rootScheduler.runTask(this.plugin, task));
Expand All @@ -29,6 +54,16 @@ public Task async(Runnable task) {
return new BukkitTaskImpl(this.rootScheduler.runTaskAsynchronously(this.plugin, task));
}

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

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

@Override
public Task laterSync(Runnable task, Duration delay) {
return new BukkitTaskImpl(this.rootScheduler.runTaskLater(this.plugin, task, this.toTick(delay)));
Expand All @@ -39,14 +74,34 @@ public Task laterAsync(Runnable task, Duration delay) {
return new BukkitTaskImpl(this.rootScheduler.runTaskLaterAsynchronously(this.plugin, task, this.toTick(delay)));
}

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

@Override
public Task laterAsync(Entity entity, 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)));
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 Task timerAsync(Location location, Runnable task, Duration delay, Duration period) {
return new BukkitTaskImpl(this.rootScheduler.runTaskTimerAsynchronously(this.plugin, task, this.toTick(delay), this.toTick(period)), true);
}

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

@Override
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,22 @@ public boolean isAsync() {
return !this.rootTask.isSync();
}

@Override
public Plugin getPlugin() {
return this.rootTask.getOwner();
}

@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;
}
}
19 changes: 19 additions & 0 deletions eternalcode-commons-folia/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
plugins {
`commons-java-17`
`commons-publish`
`commons-repositories`
`commons-java-unit-test`
}


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

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

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

tasks.test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
package com.eternalcode.commons.folia.scheduler;

import com.eternalcode.commons.scheduler.Scheduler;
import com.eternalcode.commons.scheduler.Task;
import io.papermc.paper.threadedregions.scheduler.AsyncScheduler;
import io.papermc.paper.threadedregions.scheduler.GlobalRegionScheduler;
import io.papermc.paper.threadedregions.scheduler.RegionScheduler;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Supplier;
import org.bukkit.Location;
import org.bukkit.Server;
import org.bukkit.entity.Entity;
import org.bukkit.plugin.Plugin;

public class FoliaSchedulerImpl implements Scheduler {

public final Plugin plugin;
private final Server server;

private final GlobalRegionScheduler globalRegionScheduler;
private final AsyncScheduler asyncScheduler;
private final RegionScheduler regionScheduler;

public FoliaSchedulerImpl(Plugin plugin) {
this.plugin = plugin;
this.server = this.plugin.getServer();

this.globalRegionScheduler = this.server.getGlobalRegionScheduler();
this.asyncScheduler = this.server.getAsyncScheduler();
this.regionScheduler = this.server.getRegionScheduler();
}

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

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

@Override
public boolean isEntity(Entity entity) {
return this.server.isOwnedByCurrentRegion(entity);
}

@Override
public boolean isRegion(Location location) {
return this.server.isOwnedByCurrentRegion(location);
}

@Override
public Task sync(Runnable task) {
return new FoliaTaskImpl(this.globalRegionScheduler.run(plugin, runnable -> task.run()));
}

@Override
public Task async(Runnable task) {
return new FoliaTaskImpl(this.asyncScheduler.runNow(plugin, runnable -> task.run()));
}

@Override
public Task async(Location location, Runnable task) {
return new FoliaTaskImpl(this.regionScheduler.run(plugin, location, runnable -> task.run()));
}

@Override
public Task async(Entity entity, Runnable task) {
return new FoliaTaskImpl(entity.getScheduler().run(plugin, runnable -> task.run(), null));
}

@Override
public Task laterSync(Runnable task, Duration delay) {
if (delay.isZero()) {
delay = Duration.ofMillis(1);
}

return new FoliaTaskImpl(this.globalRegionScheduler.runDelayed(
plugin,
runnable -> task.run(),
delay.toMillis())
);
}

@Override
public Task laterAsync(Runnable task, Duration delay) {
return new FoliaTaskImpl(this.globalRegionScheduler.runDelayed(
plugin,
runnable -> task.run(),
delay.toMillis())
);
}

@Override
public Task laterAsync(Location location, Runnable task, Duration delay) {
return new FoliaTaskImpl(this.regionScheduler.runDelayed(
plugin,
location,
runnable -> task.run(),
delay.toMillis())
);
}

@Override
public Task laterAsync(Entity entity, Runnable task, Duration delay) {
return new FoliaTaskImpl(entity.getScheduler().runDelayed(
plugin,
runnable -> task.run(),
null,
delay.toMillis())
);
}

@Override
public Task timerSync(Runnable task, Duration delay, Duration period) {
if (delay.isZero()) {
delay = Duration.ofMillis(1);
}

if (period.isZero()) {
period = Duration.ofMillis(1);
}

return new FoliaTaskImpl(this.globalRegionScheduler.runAtFixedRate(
plugin,
runnable -> task.run(),
delay.toMillis(),
period.toMillis())
);
}

@Override
public Task timerAsync(Runnable task, Duration delay, Duration period) {
return new FoliaTaskImpl(this.asyncScheduler.runAtFixedRate(
plugin,
runnable -> task.run(),
delay.toMillis(),
period.toMillis(),
TimeUnit.MILLISECONDS)
);
}

@Override
public Task timerAsync(Location location, Runnable task, Duration delay, Duration period) {
return new FoliaTaskImpl(this.regionScheduler.runAtFixedRate(
plugin,
location,
runnable -> task.run(),
delay.toMillis(),
period.toMillis())
);
}

@Override
public Task timerAsync(Entity entity, Runnable task, Duration delay, Duration period) {
return new FoliaTaskImpl(entity.getScheduler().runAtFixedRate(
plugin,
runnable -> task.run(),
null,
delay.toMillis(),
period.toMillis())
);
}

@Override
public <T> CompletableFuture<T> completeSync(Supplier<T> task) {
CompletableFuture<T> completable = new CompletableFuture<>();
this.globalRegionScheduler.run(plugin, scheduledTask -> completable.complete(task.get()));
return completable;
}

@Override
public <T> CompletableFuture<T> completeAsync(Supplier<T> task) {
CompletableFuture<T> completable = new CompletableFuture<>();
this.asyncScheduler.runNow(plugin, scheduledTask -> completable.complete(task.get()));
return completable;
}
}
Loading