From ea7deb7bbf1a4d30910d32205cfb4b1ee69c06af Mon Sep 17 00:00:00 2001 From: Dan Mulloy Date: Fri, 12 May 2023 11:07:05 -0400 Subject: [PATCH] Reflectively access folia --- build.gradle | 13 ++--- .../com/comphenix/protocol/ProtocolLib.java | 4 +- .../events/SerializedOfflinePlayer.java | 16 +++--- .../protocol/updater/SpigotUpdater.java | 2 +- .../protocol/utility/SchedulerUtil.java | 54 ++++++++++++++++--- 5 files changed, 63 insertions(+), 26 deletions(-) diff --git a/build.gradle b/build.gradle index 19e4e80b..91fe2b00 100644 --- a/build.gradle +++ b/build.gradle @@ -22,10 +22,6 @@ repositories { url 'https://hub.spigotmc.org/nexus/content/groups/public/' } - maven { - url 'https://repo.papermc.io/repository/maven-public/' - } - maven { url 'https://libraries.minecraft.net/' metadataSources { @@ -38,7 +34,6 @@ repositories { dependencies { implementation 'net.bytebuddy:byte-buddy:1.14.3' - compileOnly 'dev.folia:folia-api:1.19.4-R0.1-SNAPSHOT' compileOnly 'org.spigotmc:spigot-api:1.19.4-R0.1-SNAPSHOT' compileOnly 'org.spigotmc:spigot:1.19.4-R0.1-SNAPSHOT' compileOnly 'io.netty:netty-all:4.0.23.Final' @@ -57,11 +52,11 @@ dependencies { } java { - sourceCompatibility = JavaVersion.VERSION_1_8 - targetCompatibility = JavaVersion.VERSION_17 // Need for Folia + sourceCompatibility = JavaVersion.VERSION_1_8 + targetCompatibility = JavaVersion.VERSION_1_8 - withJavadocJar() - withSourcesJar() + withJavadocJar() + withSourcesJar() } shadowJar { diff --git a/src/main/java/com/comphenix/protocol/ProtocolLib.java b/src/main/java/com/comphenix/protocol/ProtocolLib.java index 7fcd456f..8864d60a 100644 --- a/src/main/java/com/comphenix/protocol/ProtocolLib.java +++ b/src/main/java/com/comphenix/protocol/ProtocolLib.java @@ -488,7 +488,7 @@ public class ProtocolLib extends JavaPlugin { } // Attempt to create task - this.packetTask = SchedulerUtil.scheduleSyncRepeatingTask(this, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY, () -> { + this.packetTask = SchedulerUtil.scheduleSyncRepeatingTask(this, () -> { AsyncFilterManager manager = (AsyncFilterManager) protocolManager.getAsynchronousManager(); // We KNOW we're on the main thread at the moment manager.sendProcessedPackets(ProtocolLib.this.tickCounter++, true); @@ -500,7 +500,7 @@ public class ProtocolLib extends JavaPlugin { if (!ProtocolLibrary.updatesDisabled() && (ProtocolLib.this.tickCounter % 20) == 0) { ProtocolLib.this.checkUpdates(); } - }); + }, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY); } catch (OutOfMemoryError e) { throw e; } catch (Throwable e) { diff --git a/src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java b/src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java index 8c91b6cc..fab48df2 100644 --- a/src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java +++ b/src/main/java/com/comphenix/protocol/events/SerializedOfflinePlayer.java @@ -21,7 +21,6 @@ import com.comphenix.protocol.reflect.accessors.Accessors; import com.comphenix.protocol.reflect.accessors.MethodAccessor; import com.comphenix.protocol.utility.ByteBuddyFactory; import com.comphenix.protocol.utility.Util; -import com.destroystokyo.paper.profile.PlayerProfile; import net.bytebuddy.description.ByteCodeElement; import net.bytebuddy.description.modifier.Visibility; import net.bytebuddy.dynamic.loading.ClassLoadingStrategy; @@ -39,6 +38,7 @@ import net.bytebuddy.matcher.ElementMatchers; import org.bukkit.*; import org.bukkit.entity.EntityType; import org.bukkit.entity.Player; +import org.bukkit.profile.PlayerProfile; import org.jetbrains.annotations.NotNull; import java.io.IOException; @@ -76,8 +76,8 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable { private boolean playedBefore; private boolean online; private boolean whitelisted; - long lastLogin; - long lastSeen; + private long lastLogin; + private long lastSeen; private static final Constructor proxyPlayerConstructor = setupProxyPlayerConstructor(); @@ -103,9 +103,11 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable { this.playedBefore = offline.hasPlayedBefore(); this.online = offline.isOnline(); this.whitelisted = offline.isWhitelisted(); + + // TODO needs to be reflectively obtained if (Util.isUsingFolia()) { - this.lastSeen = offline.getLastSeen(); - this.lastLogin = offline.getLastLogin(); + // this.lastSeen = offline.getLastSeen(); + // this.lastLogin = offline.getLastLogin(); } } @@ -129,12 +131,12 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable { return bedSpawnLocation; } - @Override + // @Override public long getLastLogin() { return lastLogin; } - @Override + // @Override public long getLastSeen() { return lastSeen; } diff --git a/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java b/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java index 44bdefdd..9cc40801 100644 --- a/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java +++ b/src/main/java/com/comphenix/protocol/updater/SpigotUpdater.java @@ -81,7 +81,7 @@ public final class SpigotUpdater extends Updater { } finally { // Invoke the listeners on the main thread for (Runnable listener : listeners) { - SchedulerUtil.execute(listener, plugin); + SchedulerUtil.execute(plugin, listener); } } } diff --git a/src/main/java/com/comphenix/protocol/utility/SchedulerUtil.java b/src/main/java/com/comphenix/protocol/utility/SchedulerUtil.java index f26da8bf..47040212 100644 --- a/src/main/java/com/comphenix/protocol/utility/SchedulerUtil.java +++ b/src/main/java/com/comphenix/protocol/utility/SchedulerUtil.java @@ -1,32 +1,72 @@ package com.comphenix.protocol.utility; +import com.comphenix.protocol.reflect.accessors.Accessors; +import com.comphenix.protocol.reflect.accessors.MethodAccessor; +import org.bukkit.Bukkit; import org.bukkit.plugin.Plugin; -public class SchedulerUtil { +import java.util.function.Consumer; - public static int scheduleSyncRepeatingTask(Plugin plugin, long delay, long period, Runnable runnable) { +public class SchedulerUtil { + private Object foliaScheduler; + private MethodAccessor runAtFixedRate; + private MethodAccessor cancelTasks; + private MethodAccessor execute; + + private static SchedulerUtil getInstance() { + return Holder.INSTANCE; + } + + private static class Holder { + private static final SchedulerUtil INSTANCE = new SchedulerUtil(); + } + + private SchedulerUtil() { if (Util.isUsingFolia()) { - plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, task -> runnable.run(), delay, period); + MethodAccessor getScheduler = Accessors.getMethodAccessor(Bukkit.getServer().getClass(), "getGlobalRegionScheduler"); + foliaScheduler = getScheduler.invoke(Bukkit.getServer()); + + runAtFixedRate = Accessors.getMethodAccessor(foliaScheduler.getClass(), "runAtFixedRate", Plugin.class, + Runnable.class, long.class, long.class); + cancelTasks = Accessors.getMethodAccessor(foliaScheduler.getClass(), "cancelTasks", Plugin.class); + execute = Accessors.getMethodAccessor(foliaScheduler.getClass(), "execute", Plugin.class, Runnable.class); + } + } + + public static int scheduleSyncRepeatingTask(Plugin plugin, Runnable runnable, long delay, long period) { + return getInstance().doScheduleSyncRepeatingTask(plugin, runnable, delay, period); + } + + private int doScheduleSyncRepeatingTask(Plugin plugin, Runnable runnable, long delay, long period) { + if (Util.isUsingFolia()) { + runAtFixedRate.invoke(foliaScheduler, plugin, (Consumer)(task -> runnable.run()), delay, period); return 1; } else { return plugin.getServer().getScheduler().scheduleSyncRepeatingTask(plugin, runnable, delay, period); } } - public static void cancelTask(Plugin plugin, int id) { + private void doCancelTask(Plugin plugin, int id) { if (Util.isUsingFolia()) { - plugin.getServer().getGlobalRegionScheduler().cancelTasks(plugin); + cancelTasks.invoke(foliaScheduler, plugin); } else { plugin.getServer().getScheduler().cancelTask(id); } } - public static void execute(Runnable runnable, Plugin plugin) { + public static void cancelTask(Plugin plugin, int id) { + getInstance().doCancelTask(plugin, id); + } + + private void doExecute(Plugin plugin, Runnable runnable) { if (Util.isUsingFolia()) { - plugin.getServer().getGlobalRegionScheduler().execute(plugin, runnable); + execute.invoke(foliaScheduler, plugin, runnable); } else { plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, runnable); } } + public static void execute(Plugin plugin, Runnable runnable) { + getInstance().doExecute(plugin, runnable); + } }