Reflectively access folia

This commit is contained in:
Dan Mulloy 2023-05-12 11:07:05 -04:00
parent f80ac0b81e
commit ea7deb7bbf
No known key found for this signature in database
GPG Key ID: F379C293F178751F
5 changed files with 63 additions and 26 deletions

View File

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

View File

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

View File

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

View File

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

View File

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