Skip to content

Commit

Permalink
Folia for Tebex
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodrichDev committed Jun 27, 2024
1 parent cd3b30d commit 3a05993
Show file tree
Hide file tree
Showing 8 changed files with 437 additions and 25 deletions.
12 changes: 8 additions & 4 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ plugins {
defaultTasks("shadowJar")

group = "io.tebex"
version = "2.0.5"
version = "2.0.5-folia"

subprojects {
plugins.apply("java")
plugins.apply("com.github.johnrengelman.shadow")

java {
toolchain.languageVersion.set(JavaLanguageVersion.of(8))
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
toolchain.languageVersion.set(JavaLanguageVersion.of(17))
sourceCompatibility = JavaVersion.VERSION_17
targetCompatibility = JavaVersion.VERSION_17
}

tasks.named("shadowJar", ShadowJar::class.java) {
Expand Down Expand Up @@ -48,6 +48,10 @@ subprojects {
maven("https://maven.nucleoid.xyz/") {
name = "nucleoid"
}
maven {
name = "paper"
url = uri("https://papermc.io/repo/repository/maven-public/")
}
}

tasks.named("processResources", Copy::class.java) {
Expand Down
3 changes: 2 additions & 1 deletion bukkit/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ dependencies {
implementation("com.github.cryptomorin:XSeries:9.3.1") { isTransitive = false }
implementation("dev.triumphteam:triumph-gui:3.1.2")

compileOnly("org.spigotmc:spigot-api:1.8.8-R0.1-SNAPSHOT")
//compileOnly("io.papermc.paper:paper-api:1.20.6-R0.1-SNAPSHOT")
compileOnly("dev.folia:folia-api:1.20.4-R0.1-SNAPSHOT")
compileOnly("dev.dejvokep:boosted-yaml:1.3")
compileOnly("me.clip:placeholderapi:2.11.3")
}
Expand Down
86 changes: 66 additions & 20 deletions bukkit/src/main/java/io/tebex/plugin/TebexPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import io.tebex.plugin.gui.BuyGUI;
import io.tebex.plugin.manager.CommandManager;
import io.tebex.plugin.placeholder.BukkitNamePlaceholder;
import io.tebex.plugin.scheduling.FoliaTaskScheduler;
import io.tebex.sdk.SDK;
import io.tebex.sdk.Tebex;
import io.tebex.sdk.obj.Category;
Expand Down Expand Up @@ -51,6 +52,8 @@ public final class TebexPlugin extends JavaPlugin implements Platform {
private Map<Object, Integer> queuedPlayers;
private YamlDocument configYaml;

private FoliaTaskScheduler scheduler;

private ServerInformation storeInformation;
private List<Category> storeCategories;
private List<ServerEvent> serverEvents;
Expand Down Expand Up @@ -97,23 +100,9 @@ public void onEnable() {

registerEvents(new JoinListener(this));

getServer().getScheduler().runTaskTimerAsynchronously(this, this::refreshListings, 0, 20 * 60 * 5);

getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
List<ServerEvent> runEvents = Lists.newArrayList(serverEvents.subList(0, Math.min(serverEvents.size(), 750)));
if (runEvents.isEmpty()) return;
if (!this.isSetup()) return;
scheduler = new FoliaTaskScheduler(this);

sdk.sendEvents(runEvents)
.thenAccept(aVoid -> {
serverEvents.removeAll(runEvents);
debug("Successfully sent analytics.");
})
.exceptionally(throwable -> {
debug("Failed to send analytics: " + throwable.getMessage());
return null;
});
}, 0, 20 * 60);
refreshTaskTimers();

// Register the custom /buy command
try {
Expand Down Expand Up @@ -210,6 +199,47 @@ public void migrateConfig() {
}
}

private void refreshTaskTimers() {
// Using Folia scheduler for asynchronous task timer
if (scheduler != null) {
scheduler.runAsyncRepeating(task -> refreshListings(), 0L, 20L * 60L * 5L, TimeUnit.MILLISECONDS);

scheduler.runAsyncRepeating(task -> {
List<ServerEvent> runEvents = Lists.newArrayList(serverEvents.subList(0, Math.min(serverEvents.size(), 750)));
if (runEvents.isEmpty()) return;
if (!this.isSetup()) return;

sdk.sendEvents(runEvents)
.thenAccept(aVoid -> {
serverEvents.removeAll(runEvents);
debug("Successfully sent analytics.");
})
.exceptionally(throwable -> {
debug("Failed to send analytics: " + throwable.getMessage());
return null;
});
}, 0L, 20L * 60L, TimeUnit.MILLISECONDS);
} else {
getServer().getScheduler().runTaskTimerAsynchronously(this, this::refreshListings, 0L, 20L * 60L * 5L);

getServer().getScheduler().runTaskTimerAsynchronously(this, () -> {
List<ServerEvent> runEvents = Lists.newArrayList(serverEvents.subList(0, Math.min(serverEvents.size(), 750)));
if (runEvents.isEmpty()) return;
if (!this.isSetup()) return;

sdk.sendEvents(runEvents)
.thenAccept(aVoid -> {
serverEvents.removeAll(runEvents);
debug("Successfully sent analytics.");
})
.exceptionally(throwable -> {
debug("Failed to send analytics: " + throwable.getMessage());
return null;
});
}, 0L, 20L * 60L);
}
}

@Override
public int getFreeSlots(Object playerId) {
Player player = getPlayer(playerId);
Expand Down Expand Up @@ -304,28 +334,44 @@ public void dispatchCommand(String command) {
public void executeAsync(Runnable runnable) {
if (!isEnabled()) return;

getServer().getScheduler().runTaskAsynchronously(this, runnable);
if (scheduler != null) {
scheduler.runAsync(runnable);
} else {
getServer().getScheduler().runTaskAsynchronously(this, runnable);
}
}

@Override
public void executeAsyncLater(Runnable runnable, long time, TimeUnit unit) {
if (!isEnabled()) return;

getServer().getScheduler().runTaskLaterAsynchronously(this, runnable, unit.toMillis(time) / 50);
if (scheduler != null) {
scheduler.runAsyncLater(runnable, unit.toMillis(time));
} else {
getServer().getScheduler().runTaskLaterAsynchronously(this, runnable, unit.toMillis(time) / 50);
}
}

@Override
public void executeBlocking(Runnable runnable) {
if (!isEnabled()) return;

getServer().getScheduler().runTask(this, runnable);
if (scheduler != null) {
scheduler.run(runnable);
} else {
getServer().getScheduler().runTask(this, runnable);
}
}

@Override
public void executeBlockingLater(Runnable runnable, long time, TimeUnit unit) {
if (!isEnabled()) return;

getServer().getScheduler().runTaskLater(this, runnable, unit.toMillis(time) / 50);
if (scheduler != null) {
scheduler.runLater(runnable, unit.toMillis(time));
} else {
getServer().getScheduler().runTaskLater(this, runnable, unit.toMillis(time) / 50);
}
}

public Player getPlayer(Object player) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package io.tebex.plugin.scheduling;

import io.papermc.paper.threadedregions.scheduler.ScheduledTask;
import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull;

public class FoliaScheduledTask implements io.tebex.plugin.scheduling.ScheduledTask {
private final ScheduledTask task;

public FoliaScheduledTask(final ScheduledTask task) {
this.task = task;
}

@Override
public void cancel() {
this.task.cancel();
}

@Override
public boolean isCancelled() {
return this.task.isCancelled();
}

@Override
public @NotNull Plugin getOwningPlugin() {
return this.task.getOwningPlugin();
}

@Override
public boolean isCurrentlyRunning() {
final ScheduledTask.ExecutionState state = this.task.getExecutionState();
return state == ScheduledTask.ExecutionState.RUNNING || state == ScheduledTask.ExecutionState.CANCELLED_RUNNING;
}

@Override
public boolean isRepeatingTask() {
return this.task.isRepeatingTask();
}
}
Loading

0 comments on commit 3a05993

Please sign in to comment.