Skip to content

Commit

Permalink
Folia support!
Browse files Browse the repository at this point in the history
  • Loading branch information
ajgeiss0702 committed Aug 28, 2023
1 parent d48613f commit 01d139f
Show file tree
Hide file tree
Showing 22 changed files with 1,022 additions and 93 deletions.
10 changes: 7 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ repositories {
maven { url = uri("https://repo.citizensnpcs.co/") }
maven { url = uri("https://oss.sonatype.org/content/groups/public/") }
maven { url = uri("https://s01.oss.sonatype.org/content/repositories/snapshots/") }
maven { url = uri("https://papermc.io/repo/repository/maven-public/") }

mavenLocal()
}

dependencies {
Expand All @@ -32,18 +35,18 @@ dependencies {
compileOnly("org.xerial:sqlite-jdbc:3.32.3.2")
compileOnly("org.spongepowered:configurate-yaml:4.0.0")

implementation("org.bstats:bstats-bukkit:1.7")

implementation("net.kyori:adventure-api:4.12.0")
implementation("net.kyori:adventure-text-minimessage:4.12.0")
implementation("net.kyori:adventure-platform-bukkit:4.3.0")

implementation("us.ajg0702:ajUtils:1.2.12")
implementation("us.ajg0702:ajUtils:1.2.23")
implementation("us.ajg0702.commands.platforms.bukkit:bukkit:1.0.0")
implementation("us.ajg0702.commands.api:api:1.0.0")

compileOnly("net.luckperms:api:5.4")

implementation("io.papermc:paperlib:1.0.7")

implementation(project(":nms:nms-legacy"))
implementation(project(":nms:nms-19"))

Expand Down Expand Up @@ -83,6 +86,7 @@ tasks.shadowJar {
relocate("org.spongepowered", "us.ajg0702.leaderboards.libs")
relocate("org.yaml", "us.ajg0702.leaderboards.libs")
relocate("io.leangen", "us.ajg0702.leaderboards.libs")
relocate("io.papermc.lib", "us.ajg0702.leaderboards.libs.paperlib")

archiveBaseName.set("ajLeaderboards")
archiveClassifier.set("")
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/us/ajg0702/leaderboards/Debug.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ public static void info(String message) {
logger.info("[DEBUG] "+message);
}

public static boolean isDebug() {
return debug;
}

private static boolean particles = false;

public static void setParticles(boolean particles) {
Expand Down
76 changes: 48 additions & 28 deletions src/main/java/us/ajg0702/leaderboards/LeaderboardPlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import net.kyori.adventure.text.Component;
import net.kyori.adventure.text.minimessage.MiniMessage;
import net.milkbowl.vault.chat.Chat;
import org.bstats.bukkit.Metrics;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.entity.Player;
import org.bukkit.plugin.RegisteredServiceProvider;
import org.bukkit.plugin.java.JavaPlugin;
Expand All @@ -36,12 +37,11 @@
import us.ajg0702.leaderboards.loaders.MessageLoader;
import us.ajg0702.leaderboards.nms.legacy.HeadUtils;
import us.ajg0702.leaderboards.placeholders.PlaceholderExpansion;
import us.ajg0702.leaderboards.utils.Exporter;
import us.ajg0702.leaderboards.utils.OfflineUpdater;
import us.ajg0702.leaderboards.utils.ResetSaver;
import us.ajg0702.leaderboards.utils.SlimJarLogger;
import us.ajg0702.leaderboards.utils.*;
import us.ajg0702.utils.common.Config;
import us.ajg0702.utils.common.Messages;
import us.ajg0702.utils.foliacompat.CompatScheduler;
import us.ajg0702.utils.foliacompat.Task;

import java.io.File;
import java.io.IOException;
Expand Down Expand Up @@ -82,6 +82,8 @@ public class LeaderboardPlugin extends JavaPlugin {

private boolean shuttingDown = false;

private final CompatScheduler compatScheduler = new CompatScheduler(this);

@Override
public void onLoad() {
try {
Expand Down Expand Up @@ -150,7 +152,7 @@ public void onEnable() {

TimeUtils.setStrings(messages);

Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
getScheduler().runTaskAsynchronously(() -> {
if(Bukkit.getPluginManager().isPluginEnabled("Vault")) {
RegisteredServiceProvider<Chat> rsp = getServer().getServicesManager().getRegistration(Chat.class);
if(rsp == null) {
Expand Down Expand Up @@ -183,9 +185,8 @@ public void onEnable() {

reloadInterval();

Bukkit.getScheduler().runTaskTimerAsynchronously(this, this::scheduleResets, 0, 15 * 60 * 20);
Bukkit.getScheduler().runTaskTimerAsynchronously(
this,
getScheduler().runTaskTimerAsynchronously(this::scheduleResets, 0, 15 * 60 * 20);
getScheduler().runTaskTimerAsynchronously(
() -> offlineUpdaters.forEach((b, u) -> u.progressLog()),
5 * 20,
30 * 20
Expand Down Expand Up @@ -220,7 +221,7 @@ private Iterable<String> getSignPath(String end) {
public void onDisable() {
shuttingDown = true;
if(getContextLoader() != null) getContextLoader().checkReload(false);
Bukkit.getScheduler().cancelTasks(this);
getScheduler().cancelTasks();
if(getTopManager() != null) getTopManager().shutdown();

if(getCache() != null) {
Expand All @@ -247,7 +248,7 @@ public void onDisable() {

getLogger().info("ajLeaderboards v"+getDescription().getVersion()+" disabled.");

Bukkit.getScheduler().getActiveWorkers().forEach(bukkitWorker -> {
getScheduler().getActiveWorkers().forEach(bukkitWorker -> {
Debug.info("Active worker: "+bukkitWorker.getOwner().getDescription().getName()+" "+bukkitWorker.getTaskId());
for (StackTraceElement stackTraceElement : bukkitWorker.getThread().getStackTrace()) {
Debug.info(" - "+stackTraceElement);
Expand All @@ -256,7 +257,7 @@ public void onDisable() {
}

private void killWorkers(int waitForDeath) {
List<BukkitWorker> workers = new ArrayList<>(Bukkit.getScheduler().getActiveWorkers());
List<BukkitWorker> workers = new ArrayList<>(getScheduler().getActiveWorkers());
List<Integer> killedWorkers = new ArrayList<>();
workers.forEach(bukkitWorker -> {
if(!bukkitWorker.getOwner().equals(this)) return;
Expand Down Expand Up @@ -341,15 +342,23 @@ public ResetSaver getResetSaver() {
return resetSaver;
}

int updateTaskId = -1;
public CompatScheduler getCompatScheduler() {
return compatScheduler;
}

public CompatScheduler getScheduler() {
return getCompatScheduler();
}

Task updateTask;
public void reloadInterval() {
if(updateTaskId != -1) {
if(updateTask != null) {
try {
Bukkit.getScheduler().cancelTask(updateTaskId);
updateTask.cancel();
} catch(IllegalArgumentException ignored) {}
updateTaskId = -1;
updateTask = null;
}
updateTaskId = Bukkit.getScheduler().runTaskTimerAsynchronously(this, () -> {
updateTask = getScheduler().runTaskTimerAsynchronously(() -> {
if(!config.getBoolean("update-stats")) return;
if(getTopManager().getFetchingAverage() > 100) {
getLogger().warning("Database is overloaded! Skipping update of players.");
Expand All @@ -359,21 +368,20 @@ public void reloadInterval() {
if(isShuttingDown()) return;
getTopManager().submit(() -> getCache().updatePlayerStats(p));
}
}, 10*20, config.getInt("stat-refresh")).getTaskId();
Debug.info("Update task id is "+updateTaskId);
}, 10*20, config.getInt("stat-refresh"));
}

final HashMap<TimedType, Integer> resetIds = new HashMap<>();
final HashMap<TimedType, Task> resetTasks = new HashMap<>();
public void scheduleResets() {
resetIds.values().forEach(Bukkit.getScheduler()::cancelTask);
resetIds.clear();
resetTasks.values().forEach(Task::cancel);
resetTasks.clear();

for(TimedType type : TimedType.values()) {
try {
scheduleReset(type);
} catch (ExecutionException | InterruptedException e) {
if(isShuttingDown()) return;
getLogger().log(Level.WARNING, "Scheduling reset interupted:", e);
getLogger().log(Level.WARNING, "Scheduling reset interrupted:", e);
}
}
}
Expand All @@ -400,7 +408,7 @@ public void scheduleReset(TimedType type) throws ExecutionException, Interrupted
}

if(resetNow.size() > 0) {
Bukkit.getScheduler().runTaskAsynchronously(this, () -> {
getScheduler().runTaskAsynchronously(() -> {
try {
for (String board : resetNow) {
cache.reset(board, type);
Expand All @@ -424,8 +432,7 @@ public void scheduleReset(TimedType type) throws ExecutionException, Interrupted
Debug.info(TimeUtils.formatTimeSeconds(secsTilNextReset)+" until the reset for "+type.lowerName()+" (next formatted: "+type.getNextReset().atOffset(ZoneOffset.UTC).format(DateTimeFormatter.RFC_1123_DATE_TIME)+" next: "+nextReset+")");

if(isShuttingDown()) return;
int taskId = Bukkit.getScheduler().runTaskLaterAsynchronously(
this,
Task task = getScheduler().runTaskLaterAsynchronously(
() -> {
try {
for (String board : getTopManager().getBoards()) {
Expand All @@ -437,8 +444,8 @@ public void scheduleReset(TimedType type) throws ExecutionException, Interrupted
}
},
secsTilNextReset*20L
).getTaskId();
resetIds.put(type, taskId);
);
resetTasks.put(type, task);
}

public boolean validatePlaceholder(String placeholder, CommandSender sayOutput) {
Expand Down Expand Up @@ -525,4 +532,17 @@ public void timePlaceholderUsed() {
}
doublePrevention = false;
}

public Future<Material> safeGetBlockType(Location location) {
CompletableFuture<Material> future = new CompletableFuture<>();
Runnable runnable = () -> {
future.complete(location.getBlock().getType());
};
if(CompatScheduler.isFolia()) {
getScheduler().runSync(location, runnable);
} else {
runnable.run();
}
return future;
}
}
4 changes: 2 additions & 2 deletions src/main/java/us/ajg0702/leaderboards/Listeners.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public Listeners(LeaderboardPlugin plugin) {
@EventHandler
public void onJoin(PlayerJoinEvent e) {
if(plugin.getCache().getMethod().getName().equals("sqlite") && e.getPlayer().hasPermission("ajleaderboards.use")) {
Bukkit.getScheduler().runTaskLaterAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskLaterAsynchronously(() -> {
plugin.getAdventure().player(e.getPlayer())
.sendMessage(message(
"\n&6[ajLeaderboards] &cSQLite is not recommended and will be removed! &7Please switch to h2 for a faster (and more stable) cache storage.\n" +
Expand All @@ -36,7 +36,7 @@ public void onJoin(PlayerJoinEvent e) {
}
if(!plugin.getAConfig().getBoolean("update-stats")) return;
if(!plugin.getAConfig().getBoolean("update-on-join")) return;
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> plugin.getCache().updatePlayerStats(e.getPlayer()));
plugin.getScheduler().runTaskAsynchronously(() -> plugin.getCache().updatePlayerStats(e.getPlayer()));
}

@EventHandler
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TopManager(LeaderboardPlugin pl, List<String> initialBoards) {
ThreadFactoryProxy.getDefaultThreadFactory("AJLBFETCH")
);
fetchService.allowCoreThreadTimeOut(true);
Bukkit.getScheduler().runTaskTimerAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskTimerAsynchronously(() -> {
rolling.add(getQueuedTasks()+getActiveFetchers());
if(rolling.size() > 50) {
rolling.remove(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public List<String> autoComplete(CommandSender commandSender, String[] args) {

@Override
public void execute(CommandSender sender, String[] args, String label) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
if(args.length < 1) {
sender.sendMessage(message("&cPlease provide a placeholder to track.\n&7Usage: /"+label+" add <placeholder>"));
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void execute(CommandSender sender, String[] args, String label) {
sender.sendMessage(message("&cPlease provide a file name.\n&7Usage: /"+label+" export <file>"));
return;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
String fileName = addJsonEnding(args[0]);
File file = new File(plugin.getDataFolder(), fileName);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public void execute(CommandSender sender, String[] args, String label) {
sender.sendMessage(message("&cPlease provide a file name.\n&7Usage: /"+label+" import <file>"));
return;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
File file = new File(plugin.getDataFolder(), args[0]);
if(!file.exists()) {
sender.sendMessage(plugin.getMessages().getComponent("commands.import.nofile", "FILE:"+file.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public java.util.List<String> autoComplete(CommandSender commandSender, String[]

@Override
public void execute(CommandSender sender, String[] args, String label) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
if(args.length < 1) {
StringBuilder list = new StringBuilder("&6Boards");
for(String boardn : plugin.getTopManager().getBoards()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public void execute(CommandSender sender, String[] args, String label) {
"</b></green></click>"));
confirmDeletes.put(sender.getHandle(), board);
Debug.info("Added confirmDelete: "+confirmDeletes.keySet().size());
Bukkit.getScheduler().runTaskLater(plugin, () -> {
plugin.getScheduler().runTaskLaterAsynchronously(() -> {
Debug.info("Removing confirmDelete");
if(confirmDeletes.containsKey(sender.getHandle()) && confirmDeletes.get(sender.getHandle()).equals(board)) {
confirmDeletes.remove(sender.getHandle());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void execute(CommandSender sender, String[] args, String label) {
boards = plugin.getCache().getBoards();
}
List<String> finalBoards = boards;
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
for(String b : finalBoards) {
if(plugin.getCache().removePlayer(b, playername)) {
sender.sendMessage(message("&aRemoved "+playername+" from "+b+"!"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void execute(CommandSender sender, String[] args, String label) {
"</b></green></click>"));
confirmResets.put(sender.getHandle(), board);
Debug.info("Added confirmDelete: "+ confirmResets.keySet().size());
Bukkit.getScheduler().runTaskLater(plugin, () -> {
plugin.getScheduler().runTaskLaterAsynchronously(() -> {
if(confirmResets.containsKey(sender.getHandle()) && confirmResets.get(sender.getHandle()).equals(board)) {
confirmResets.remove(sender.getHandle());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public void execute(CommandSender sender, String[] args, String label) {
sender.sendMessage(message("&cPlease provide a board and player to update\n&7Usage: /"+label+" update <board> <player>"));
return;
}
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
String board = args[0];
if(!plugin.getCache().boardExists(board) && !board.equals("*")) {
sender.sendMessage(message("&cThe board '"+board+"' does not exist."));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public List<String> autoComplete(CommandSender sender, String[] args) {

@Override
public void execute(CommandSender sender, String[] args, String label) {
Bukkit.getScheduler().runTaskAsynchronously(plugin, () -> {
plugin.getScheduler().runTaskAsynchronously(() -> {
JsonObject obj = plugin.getExporter().export(sender);
if(obj == null) {
sender.sendMessage(plugin.getMessages().getComponent("commands.export.fail"));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package us.ajg0702.leaderboards.commands.main.subcommands.signs;

import io.papermc.lib.PaperLib;
import org.bukkit.Location;
import org.bukkit.entity.Player;
import us.ajg0702.commands.CommandSender;
Expand Down Expand Up @@ -65,6 +66,6 @@ public void execute(CommandSender sender, String[] args, String label) {
loc.setX(loc.getBlockX()+0.5);
loc.setZ(loc.getBlockZ()+0.5);

player.teleport(loc);
PaperLib.teleportAsync(player, loc);
}
}
Loading

0 comments on commit 01d139f

Please sign in to comment.