feat: Add support for Folia (#280)

* feat: Add support for Folia

* feat: fix folia advancement stuff

* feat: fix double negation (whoops)
feat/data-edit-commands
William 7 months ago committed by GitHub
parent bd312c48ea
commit 7218390f65
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

@ -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();

@ -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<String, Date> 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<String> toAward, @NotNull Collection<String> 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

@ -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

@ -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

@ -5,6 +5,7 @@ api-version: 1.17
author: 'William278'
description: '${description}'
website: 'https://william278.net'
folia-supported: true
softdepend:
- 'ProtocolLib'
- 'MysqlPlayerDataBridge'

@ -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

@ -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

@ -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:

Loading…
Cancel
Save