From 7218390f6513f582facf0755a82e098faebb7b3d Mon Sep 17 00:00:00 2001 From: William Date: Fri, 12 Apr 2024 17:48:41 +0100 Subject: [PATCH] feat: Add support for Folia (#280) * feat: Add support for Folia * feat: fix folia advancement stuff * feat: fix double negation (whoops) --- .../william278/husksync/BukkitHuskSync.java | 11 +++++--- .../william278/husksync/data/BukkitData.java | 13 +++++----- .../william278/husksync/user/BukkitUser.java | 2 +- .../william278/husksync/util/BukkitTask.java | 26 ++++++++++++++++--- bukkit/src/main/resources/plugin.yml | 1 + .../husksync/data/UserDataHolder.java | 4 +-- .../net/william278/husksync/util/Task.java | 15 ++++++++--- paper/src/main/resources/paper-plugin.yml | 1 + 8 files changed, 52 insertions(+), 21 deletions(-) diff --git a/bukkit/src/main/java/net/william278/husksync/BukkitHuskSync.java b/bukkit/src/main/java/net/william278/husksync/BukkitHuskSync.java index c06c4a59..1c0d3e2b 100644 --- a/bukkit/src/main/java/net/william278/husksync/BukkitHuskSync.java +++ b/bukkit/src/main/java/net/william278/husksync/BukkitHuskSync.java @@ -65,9 +65,7 @@ import org.bukkit.plugin.java.JavaPlugin; import org.jetbrains.annotations.NotNull; import space.arim.morepaperlib.MorePaperLib; import space.arim.morepaperlib.commands.CommandRegistration; -import space.arim.morepaperlib.scheduling.AsynchronousScheduler; -import space.arim.morepaperlib.scheduling.GracefulScheduling; -import space.arim.morepaperlib.scheduling.RegionalScheduler; +import space.arim.morepaperlib.scheduling.*; import java.nio.file.Path; import java.util.*; @@ -331,11 +329,16 @@ public class BukkitHuskSync extends JavaPlugin implements HuskSync, BukkitTask.S } @NotNull - public RegionalScheduler getRegionalScheduler() { + public RegionalScheduler getSyncScheduler() { return regionalScheduler == null ? regionalScheduler = getScheduler().globalRegionalScheduler() : regionalScheduler; } + @NotNull + public AttachedScheduler getUserSyncScheduler(@NotNull UserDataHolder user) { + return getScheduler().entitySpecificScheduler(((BukkitUser) user).getPlayer()); + } + @NotNull public CommandRegistration getCommandRegistrar() { return paperLib.commandRegistration(); diff --git a/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java b/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java index 48b6496f..3edd97c6 100644 --- a/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java +++ b/bukkit/src/main/java/net/william278/husksync/data/BukkitData.java @@ -325,28 +325,29 @@ public abstract class BukkitData implements Data { .filter(r -> r.getKey().equals(advancement.getKey().toString())) .findFirst(); if (record.isEmpty()) { - this.setAdvancement(plugin, advancement, player, List.of(), progress.getAwardedCriteria()); + this.setAdvancement(plugin, advancement, player, user, List.of(), progress.getAwardedCriteria()); return; } final Map criteria = record.get().getCompletedCriteria(); this.setAdvancement( - plugin, advancement, player, + plugin, advancement, player, user, criteria.keySet().stream().filter(key -> !progress.getAwardedCriteria().contains(key)).toList(), progress.getAwardedCriteria().stream().filter(key -> !criteria.containsKey(key)).toList() ); })); } - private void setAdvancement(@NotNull HuskSync plugin, - @NotNull org.bukkit.advancement.Advancement advancement, @NotNull Player player, + private void setAdvancement(@NotNull HuskSync plugin, @NotNull org.bukkit.advancement.Advancement advancement, + @NotNull Player player, @NotNull BukkitUser user, @NotNull Collection toAward, @NotNull Collection toRevoke) { + final boolean folia = ((BukkitHuskSync) plugin).getScheduler().isUsingFolia(); plugin.runSync(() -> { // Track player exp level & progress final int expLevel = player.getLevel(); final float expProgress = player.getExp(); boolean gameRuleUpdated = false; - if (Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS))) { + if (!folia && Boolean.TRUE.equals(player.getWorld().getGameRuleValue(GameRule.ANNOUNCE_ADVANCEMENTS))) { player.getWorld().setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, false); gameRuleUpdated = true; } @@ -364,7 +365,7 @@ public abstract class BukkitData implements Data { if (gameRuleUpdated) { player.getWorld().setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, true); } - }); + }, user); } // Performs a consuming function for every advancement registered on the server diff --git a/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java b/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java index ff492f7a..b2218ca8 100644 --- a/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java +++ b/bukkit/src/main/java/net/william278/husksync/user/BukkitUser.java @@ -109,7 +109,7 @@ public class BukkitUser extends OnlineUser implements BukkitUserDataHolder { gui.setCloseGuiAction((close) -> onClose.accept(BukkitData.Items.ItemArray.adapt( Arrays.stream(close.getInventory().getContents()).limit(size).toArray(ItemStack[]::new) ))); - plugin.runSync(() -> gui.open(player)); + plugin.runSync(() -> gui.open(player), this); } @Override diff --git a/bukkit/src/main/java/net/william278/husksync/util/BukkitTask.java b/bukkit/src/main/java/net/william278/husksync/util/BukkitTask.java index a9381425..3aa9951e 100644 --- a/bukkit/src/main/java/net/william278/husksync/util/BukkitTask.java +++ b/bukkit/src/main/java/net/william278/husksync/util/BukkitTask.java @@ -21,8 +21,11 @@ package net.william278.husksync.util; import net.william278.husksync.BukkitHuskSync; import net.william278.husksync.HuskSync; +import net.william278.husksync.data.UserDataHolder; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import space.arim.morepaperlib.scheduling.AsynchronousScheduler; +import space.arim.morepaperlib.scheduling.AttachedScheduler; import space.arim.morepaperlib.scheduling.RegionalScheduler; import space.arim.morepaperlib.scheduling.ScheduledTask; @@ -34,9 +37,12 @@ public interface BukkitTask extends Task { class Sync extends Task.Sync implements BukkitTask { private ScheduledTask task; + private final @Nullable UserDataHolder user; - protected Sync(@NotNull HuskSync plugin, @NotNull Runnable runnable, long delayTicks) { + protected Sync(@NotNull HuskSync plugin, @NotNull Runnable runnable, + @Nullable UserDataHolder user, long delayTicks) { super(plugin, runnable, delayTicks); + this.user = user; } @Override @@ -57,7 +63,19 @@ public interface BukkitTask extends Task { return; } - final RegionalScheduler scheduler = ((BukkitHuskSync) getPlugin()).getRegionalScheduler(); + // Use entity-specific scheduler if user is not null + if (user != null) { + final AttachedScheduler scheduler = ((BukkitHuskSync) getPlugin()).getUserSyncScheduler(user); + if (delayTicks > 0) { + this.task = scheduler.runDelayed(runnable, null, delayTicks); + } else { + this.task = scheduler.run(runnable, null); + } + return; + } + + // Or default to the global scheduler + final RegionalScheduler scheduler = ((BukkitHuskSync) getPlugin()).getSyncScheduler(); if (delayTicks > 0) { this.task = scheduler.runDelayed(runnable, delayTicks); } else { @@ -146,8 +164,8 @@ public interface BukkitTask extends Task { @NotNull @Override - default Task.Sync getSyncTask(@NotNull Runnable runnable, long delayTicks) { - return new Sync(getPlugin(), runnable, delayTicks); + default Task.Sync getSyncTask(@NotNull Runnable runnable, @Nullable UserDataHolder user, long delayTicks) { + return new Sync(getPlugin(), runnable, user, delayTicks); } @NotNull diff --git a/bukkit/src/main/resources/plugin.yml b/bukkit/src/main/resources/plugin.yml index 3c119330..44d45516 100644 --- a/bukkit/src/main/resources/plugin.yml +++ b/bukkit/src/main/resources/plugin.yml @@ -5,6 +5,7 @@ api-version: 1.17 author: 'William278' description: '${description}' website: 'https://william278.net' +folia-supported: true softdepend: - 'ProtocolLib' - 'MysqlPlayerDataBridge' diff --git a/common/src/main/java/net/william278/husksync/data/UserDataHolder.java b/common/src/main/java/net/william278/husksync/data/UserDataHolder.java index 91e0d701..7321e6aa 100644 --- a/common/src/main/java/net/william278/husksync/data/UserDataHolder.java +++ b/common/src/main/java/net/william278/husksync/data/UserDataHolder.java @@ -60,7 +60,7 @@ public interface UserDataHolder extends DataHolder { */ @Override default void setData(@NotNull Identifier identifier, @NotNull Data data) { - getPlugin().runSync(() -> data.apply(this, getPlugin())); + getPlugin().runSync(() -> data.apply(this, getPlugin()), this); } /** @@ -119,7 +119,7 @@ public interface UserDataHolder extends DataHolder { return; } plugin.runAsync(() -> runAfter.accept(true)); - }); + }, this); } @Override diff --git a/common/src/main/java/net/william278/husksync/util/Task.java b/common/src/main/java/net/william278/husksync/util/Task.java index 6daf6905..0006b4bf 100644 --- a/common/src/main/java/net/william278/husksync/util/Task.java +++ b/common/src/main/java/net/william278/husksync/util/Task.java @@ -20,7 +20,9 @@ package net.william278.husksync.util; import net.william278.husksync.HuskSync; +import net.william278.husksync.data.UserDataHolder; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.concurrent.CompletableFuture; @@ -86,7 +88,7 @@ public interface Task extends Runnable { interface Supplier { @NotNull - Task.Sync getSyncTask(@NotNull Runnable runnable, long delayTicks); + Task.Sync getSyncTask(@NotNull Runnable runnable, @Nullable UserDataHolder user, long delayTicks); @NotNull Task.Async getAsyncTask(@NotNull Runnable runnable, long delayTicks); @@ -95,8 +97,8 @@ public interface Task extends Runnable { Task.Repeating getRepeatingTask(@NotNull Runnable runnable, long repeatingTicks); @NotNull - default Task.Sync runSyncDelayed(@NotNull Runnable runnable, long delayTicks) { - final Task.Sync task = getSyncTask(runnable, delayTicks); + default Task.Sync runSyncDelayed(@NotNull Runnable runnable, @Nullable UserDataHolder user, long delayTicks) { + final Task.Sync task = getSyncTask(runnable, user, delayTicks); task.run(); return task; } @@ -109,7 +111,12 @@ public interface Task extends Runnable { @NotNull default Task.Sync runSync(@NotNull Runnable runnable) { - return runSyncDelayed(runnable, 0); + return runSyncDelayed(runnable, null, 0); + } + + @NotNull + default Task.Sync runSync(@NotNull Runnable runnable, @NotNull UserDataHolder user) { + return runSyncDelayed(runnable, user, 0); } @NotNull diff --git a/paper/src/main/resources/paper-plugin.yml b/paper/src/main/resources/paper-plugin.yml index 37226d66..bcc6cd96 100644 --- a/paper/src/main/resources/paper-plugin.yml +++ b/paper/src/main/resources/paper-plugin.yml @@ -6,6 +6,7 @@ main: 'net.william278.husksync.PaperHuskSync' loader: 'net.william278.husksync.PaperHuskSyncLoader' version: '${version}' api-version: '1.19' +folia-supported: true dependencies: server: ProtocolLib: