Skip to content

Commit

Permalink
feat: Add support for Folia (#280)
Browse files Browse the repository at this point in the history
* feat: Add support for Folia

* feat: fix folia advancement stuff

* feat: fix double negation (whoops)
  • Loading branch information
WiIIiam278 authored Apr 12, 2024
1 parent bd312c4 commit 7218390
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 21 deletions.
11 changes: 7 additions & 4 deletions bukkit/src/main/java/net/william278/husksync/BukkitHuskSync.java
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@
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.*;
Expand Down Expand Up @@ -331,11 +329,16 @@ public AsynchronousScheduler getAsyncScheduler() {
}

@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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -325,28 +325,29 @@ public void apply(@NotNull BukkitUser user, @NotNull BukkitHuskSync plugin) thro
.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;
}
Expand All @@ -364,7 +365,7 @@ private void setAdvancement(@NotNull HuskSync plugin,
if (gameRuleUpdated) {
player.getWorld().setGameRule(GameRule.ANNOUNCE_ADVANCEMENTS, true);
}
});
}, user);
}

// Performs a consuming function for every advancement registered on the server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public void showGui(@NotNull Data.Items items, @NotNull MineDown title, boolean
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
Expand Down
26 changes: 22 additions & 4 deletions bukkit/src/main/java/net/william278/husksync/util/BukkitTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@

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;

Expand All @@ -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
Expand All @@ -57,7 +63,19 @@ public void run() {
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 {
Expand Down Expand Up @@ -146,8 +164,8 @@ interface Supplier extends Task.Supplier {

@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
Expand Down
1 change: 1 addition & 0 deletions bukkit/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ api-version: 1.17
author: 'William278'
description: '${description}'
website: 'https://william278.net'
folia-supported: true
softdepend:
- 'ProtocolLib'
- 'MysqlPlayerDataBridge'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ default Map<Identifier, Data> getData() {
*/
@Override
default void setData(@NotNull Identifier identifier, @NotNull Data data) {
getPlugin().runSync(() -> data.apply(this, getPlugin()));
getPlugin().runSync(() -> data.apply(this, getPlugin()), this);
}

/**
Expand Down Expand Up @@ -119,7 +119,7 @@ default void applySnapshot(@NotNull DataSnapshot.Packed snapshot, @NotNull Throw
return;
}
plugin.runAsync(() -> runAfter.accept(true));
});
}, this);
}

@Override
Expand Down
15 changes: 11 additions & 4 deletions common/src/main/java/net/william278/husksync/util/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -86,7 +88,7 @@ protected Repeating(@NotNull HuskSync plugin, @NotNull Runnable runnable, long r
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);
Expand All @@ -95,8 +97,8 @@ interface Supplier {
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;
}
Expand All @@ -109,7 +111,12 @@ default Task.Async runAsyncDelayed(@NotNull Runnable runnable, long delayTicks)

@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
Expand Down
1 change: 1 addition & 0 deletions paper/src/main/resources/paper-plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 7218390

Please sign in to comment.