mirror of
https://github.com/dmulloy2/ProtocolLib.git
synced 2025-02-21 15:01:41 +01:00
Refactor
This commit is contained in:
parent
3fbc40ae3a
commit
e3cd32d5b4
@ -27,10 +27,7 @@ import com.comphenix.protocol.injector.PacketFilterManager;
|
||||
import com.comphenix.protocol.metrics.Statistics;
|
||||
import com.comphenix.protocol.updater.Updater;
|
||||
import com.comphenix.protocol.updater.Updater.UpdateType;
|
||||
import com.comphenix.protocol.utility.ByteBuddyFactory;
|
||||
import com.comphenix.protocol.utility.ChatExtensions;
|
||||
import com.comphenix.protocol.utility.MinecraftVersion;
|
||||
import com.comphenix.protocol.utility.Util;
|
||||
import com.comphenix.protocol.utility.*;
|
||||
import com.google.common.base.Splitter;
|
||||
import com.google.common.collect.Iterables;
|
||||
|
||||
@ -59,9 +56,6 @@ import org.bukkit.plugin.java.JavaPlugin;
|
||||
* @author Kristian
|
||||
*/
|
||||
public class ProtocolLib extends JavaPlugin {
|
||||
|
||||
public static boolean isFolia = false;
|
||||
|
||||
// Every possible error or warning report type
|
||||
public static final ReportType REPORT_CANNOT_DELETE_CONFIG = new ReportType(
|
||||
"Cannot delete old ProtocolLib configuration.");
|
||||
@ -121,12 +115,6 @@ public class ProtocolLib extends JavaPlugin {
|
||||
|
||||
@Override
|
||||
public void onLoad() {
|
||||
// Folia support
|
||||
try {
|
||||
Class.forName("io.papermc.paper.threadedregions.RegionizedServer");
|
||||
isFolia = true;
|
||||
} catch (ClassNotFoundException ignored) {}
|
||||
|
||||
// Logging
|
||||
logger = this.getLogger();
|
||||
ProtocolLogger.init(this);
|
||||
@ -500,13 +488,19 @@ public class ProtocolLib extends JavaPlugin {
|
||||
}
|
||||
|
||||
// Attempt to create task
|
||||
this.packetTask = SchedulerUtil.scheduleSyncRepeatingTask(this, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY, () -> {
|
||||
AsyncFilterManager manager = (AsyncFilterManager) protocolManager.getAsynchronousManager();
|
||||
// We KNOW we're on the main thread at the moment
|
||||
manager.sendProcessedPackets(ProtocolLib.this.tickCounter++, true);
|
||||
|
||||
if (isFolia) {
|
||||
this.packetTask = 1;
|
||||
server.getGlobalRegionScheduler().runAtFixedRate(this, task -> packetTaskRegistrator(), ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY);
|
||||
} else {
|
||||
this.packetTask = server.getScheduler().scheduleSyncRepeatingTask(this, this::packetTaskRegistrator, ASYNC_MANAGER_DELAY, ASYNC_MANAGER_DELAY);
|
||||
}
|
||||
// House keeping
|
||||
ProtocolLib.this.updateConfiguration();
|
||||
|
||||
// Check for updates too
|
||||
if (!ProtocolLibrary.updatesDisabled() && (ProtocolLib.this.tickCounter % 20) == 0) {
|
||||
ProtocolLib.this.checkUpdates();
|
||||
}
|
||||
});
|
||||
} catch (OutOfMemoryError e) {
|
||||
throw e;
|
||||
} catch (Throwable e) {
|
||||
@ -516,20 +510,6 @@ public class ProtocolLib extends JavaPlugin {
|
||||
}
|
||||
}
|
||||
|
||||
private void packetTaskRegistrator() {
|
||||
AsyncFilterManager manager = (AsyncFilterManager) protocolManager.getAsynchronousManager();
|
||||
// We KNOW we're on the main thread at the moment
|
||||
manager.sendProcessedPackets(ProtocolLib.this.tickCounter++, true);
|
||||
|
||||
// House keeping
|
||||
ProtocolLib.this.updateConfiguration();
|
||||
|
||||
// Check for updates too
|
||||
if (!ProtocolLibrary.updatesDisabled() && (ProtocolLib.this.tickCounter % 20) == 0) {
|
||||
ProtocolLib.this.checkUpdates();
|
||||
}
|
||||
}
|
||||
|
||||
private void updateConfiguration() {
|
||||
if (config != null && config.getModificationCount() != this.configExpectedMod) {
|
||||
this.configExpectedMod = config.getModificationCount();
|
||||
@ -584,11 +564,7 @@ public class ProtocolLib extends JavaPlugin {
|
||||
|
||||
// Clean up
|
||||
if (this.packetTask >= 0) {
|
||||
if (isFolia) {
|
||||
this.getServer().getGlobalRegionScheduler().cancelTasks(this);
|
||||
} else {
|
||||
this.getServer().getScheduler().cancelTask(this.packetTask);
|
||||
}
|
||||
SchedulerUtil.cancelTask(this, packetTask);
|
||||
this.packetTask = -1;
|
||||
}
|
||||
|
||||
|
@ -17,6 +17,30 @@
|
||||
|
||||
package com.comphenix.protocol.events;
|
||||
|
||||
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;
|
||||
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
|
||||
import net.bytebuddy.implementation.FieldAccessor;
|
||||
import net.bytebuddy.implementation.InvocationHandlerAdapter;
|
||||
import net.bytebuddy.implementation.MethodCall;
|
||||
import net.bytebuddy.implementation.MethodDelegation;
|
||||
import net.bytebuddy.implementation.bind.annotation.AllArguments;
|
||||
import net.bytebuddy.implementation.bind.annotation.FieldValue;
|
||||
import net.bytebuddy.implementation.bind.annotation.Origin;
|
||||
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
@ -28,32 +52,6 @@ import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLib;
|
||||
import com.comphenix.protocol.reflect.accessors.Accessors;
|
||||
import com.comphenix.protocol.reflect.accessors.MethodAccessor;
|
||||
import com.comphenix.protocol.utility.ByteBuddyFactory;
|
||||
|
||||
import com.destroystokyo.paper.profile.PlayerProfile;
|
||||
import org.bukkit.*;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
import net.bytebuddy.description.ByteCodeElement;
|
||||
import net.bytebuddy.description.modifier.Visibility;
|
||||
import net.bytebuddy.dynamic.loading.ClassLoadingStrategy;
|
||||
import net.bytebuddy.dynamic.scaffold.subclass.ConstructorStrategy;
|
||||
import net.bytebuddy.implementation.FieldAccessor;
|
||||
import net.bytebuddy.implementation.InvocationHandlerAdapter;
|
||||
import net.bytebuddy.implementation.MethodCall;
|
||||
import net.bytebuddy.implementation.MethodDelegation;
|
||||
import net.bytebuddy.implementation.bind.annotation.AllArguments;
|
||||
import net.bytebuddy.implementation.bind.annotation.Origin;
|
||||
import net.bytebuddy.implementation.bind.annotation.FieldValue;
|
||||
import net.bytebuddy.implementation.bind.annotation.RuntimeType;
|
||||
import net.bytebuddy.matcher.ElementMatcher;
|
||||
import net.bytebuddy.matcher.ElementMatchers;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
/**
|
||||
* Represents a player object that can be serialized by Java.
|
||||
*
|
||||
@ -105,7 +103,7 @@ class SerializedOfflinePlayer implements OfflinePlayer, Serializable {
|
||||
this.playedBefore = offline.hasPlayedBefore();
|
||||
this.online = offline.isOnline();
|
||||
this.whitelisted = offline.isWhitelisted();
|
||||
if (ProtocolLib.isFolia) {
|
||||
if (Util.isUsingFolia()) {
|
||||
this.lastSeen = offline.getLastSeen();
|
||||
this.lastLogin = offline.getLastLogin();
|
||||
}
|
||||
|
@ -16,10 +16,10 @@
|
||||
*/
|
||||
package com.comphenix.protocol.updater;
|
||||
|
||||
import com.comphenix.protocol.ProtocolLib;
|
||||
import com.comphenix.protocol.ProtocolLibrary;
|
||||
import com.comphenix.protocol.error.Report;
|
||||
import com.comphenix.protocol.utility.Closer;
|
||||
import com.comphenix.protocol.utility.SchedulerUtil;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
@ -81,11 +81,7 @@ public final class SpigotUpdater extends Updater {
|
||||
} finally {
|
||||
// Invoke the listeners on the main thread
|
||||
for (Runnable listener : listeners) {
|
||||
if (ProtocolLib.isFolia) {
|
||||
plugin.getServer().getGlobalRegionScheduler().execute(plugin, listener);
|
||||
} else {
|
||||
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, listener);
|
||||
}
|
||||
SchedulerUtil.execute(listener, plugin);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,32 @@
|
||||
package com.comphenix.protocol.utility;
|
||||
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public class SchedulerUtil {
|
||||
|
||||
public static int scheduleSyncRepeatingTask(Plugin plugin, long delay, long period, Runnable runnable) {
|
||||
if (Util.isUsingFolia()) {
|
||||
plugin.getServer().getGlobalRegionScheduler().runAtFixedRate(plugin, 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) {
|
||||
if (Util.isUsingFolia()) {
|
||||
plugin.getServer().getGlobalRegionScheduler().cancelTasks(plugin);
|
||||
} else {
|
||||
plugin.getServer().getScheduler().cancelTask(id);
|
||||
}
|
||||
}
|
||||
|
||||
public static void execute(Runnable runnable, Plugin plugin) {
|
||||
if (Util.isUsingFolia()) {
|
||||
plugin.getServer().getGlobalRegionScheduler().execute(plugin, runnable);
|
||||
} else {
|
||||
plugin.getServer().getScheduler().scheduleSyncDelayedTask(plugin, runnable);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -21,43 +21,49 @@ package com.comphenix.protocol.utility;
|
||||
*/
|
||||
public final class Util {
|
||||
|
||||
private static final boolean SPIGOT = classExists("org.spigotmc.SpigotConfig");
|
||||
private static Class<?> cachedBundleClass;
|
||||
private static final boolean SPIGOT = classExists("org.spigotmc.SpigotConfig");
|
||||
private static final boolean FOLIA = classExists("io.papermc.paper.threadedregions.RegionizedServer");
|
||||
private static Class<?> cachedBundleClass;
|
||||
|
||||
public static boolean classExists(String className) {
|
||||
try {
|
||||
Class.forName(className);
|
||||
return true;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
public static boolean classExists(String className) {
|
||||
try {
|
||||
Class.forName(className);
|
||||
return true;
|
||||
} catch (ClassNotFoundException ex) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Whether this server is running Spigot or a Spigot fork. This works by checking if the SpigotConfig exists, which
|
||||
* should be true of all forks.
|
||||
*
|
||||
* @return True if it is, false if not.
|
||||
*/
|
||||
public static boolean isUsingSpigot() {
|
||||
return SPIGOT;
|
||||
}
|
||||
/**
|
||||
* Whether this server is running Spigot or a Spigot fork. This works by checking if the SpigotConfig exists, which
|
||||
* should be true of all forks.
|
||||
*
|
||||
* @return True if it is, false if not.
|
||||
*/
|
||||
public static boolean isUsingSpigot() {
|
||||
return SPIGOT;
|
||||
}
|
||||
|
||||
public static boolean isUsingFolia() {
|
||||
return FOLIA;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the server is getting reloaded by walking down the current thread stack trace.
|
||||
*
|
||||
* @return true if the server is getting reloaded, false otherwise.
|
||||
*/
|
||||
public static boolean isCurrentlyReloading() {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
for (StackTraceElement element : stackTrace) {
|
||||
String clazz = element.getClassName();
|
||||
if (clazz.startsWith("org.bukkit.craftbukkit.")
|
||||
&& clazz.endsWith(".CraftServer")
|
||||
&& element.getMethodName().equals("reload")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the server is getting reloaded by walking down the current thread stack trace.
|
||||
*
|
||||
* @return true if the server is getting reloaded, false otherwise.
|
||||
*/
|
||||
public static boolean isCurrentlyReloading() {
|
||||
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
|
||||
for (StackTraceElement element : stackTrace) {
|
||||
String clazz = element.getClassName();
|
||||
if (clazz.startsWith("org.bukkit.craftbukkit.")
|
||||
&& clazz.endsWith(".CraftServer")
|
||||
&& element.getMethodName().equals("reload")) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user