From 14c2ab5ddd44bb3946c9c24718120f17d23bd505 Mon Sep 17 00:00:00 2001 From: Josh Roy <10731363+JRoy@users.noreply.github.com> Date: Tue, 31 Aug 2021 08:34:10 -0700 Subject: [PATCH] Add online mode data for /ess dump (#4497) EssentialsX/Website#60 --- .../com/earth2me/essentials/Essentials.java | 9 +++ .../com/earth2me/essentials/IEssentials.java | 3 + .../commands/Commandessentials.java | 1 + .../providers/ReflOnlineModeProvider.java | 59 +++++++++++++++++++ 4 files changed, 72 insertions(+) create mode 100644 providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflOnlineModeProvider.java diff --git a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java index dd9c353e0..fa4a8a3ce 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/Essentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/Essentials.java @@ -49,6 +49,7 @@ import net.ess3.api.IJails; import net.ess3.api.ISettings; import net.ess3.nms.refl.providers.ReflFormattedCommandAliasProvider; import net.ess3.nms.refl.providers.ReflKnownCommandsProvider; +import net.ess3.nms.refl.providers.ReflOnlineModeProvider; import net.ess3.nms.refl.providers.ReflPersistentDataProvider; import net.ess3.nms.refl.providers.ReflServerStateProvider; import net.ess3.nms.refl.providers.ReflSpawnEggProvider; @@ -165,6 +166,7 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { private transient MaterialTagProvider materialTagProvider; private transient SyncCommandsProvider syncCommandsProvider; private transient PersistentDataProvider persistentDataProvider; + private transient ReflOnlineModeProvider onlineModeProvider; private transient Kits kits; private transient RandomTeleport randomTeleport; private transient UpdateChecker updateChecker; @@ -406,6 +408,8 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { persistentDataProvider = new ReflPersistentDataProvider(this); } + onlineModeProvider = new ReflOnlineModeProvider(); + execTimer.mark("Init(Providers)"); reload(); @@ -1260,6 +1264,11 @@ public class Essentials extends JavaPlugin implements net.ess3.api.IEssentials { return persistentDataProvider; } + @Override + public ReflOnlineModeProvider getOnlineModeProvider() { + return onlineModeProvider; + } + @Override public PluginCommand getPluginCommand(final String cmd) { return this.getCommand(cmd); diff --git a/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java b/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java index 6f47fbbed..73e09b8c7 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/IEssentials.java @@ -7,6 +7,7 @@ import com.earth2me.essentials.commands.IEssentialsCommand; import com.earth2me.essentials.commands.PlayerNotFoundException; import com.earth2me.essentials.perm.PermissionsHandler; import com.earth2me.essentials.updatecheck.UpdateChecker; +import net.ess3.nms.refl.providers.ReflOnlineModeProvider; import net.ess3.provider.ContainerProvider; import net.ess3.provider.FormattedCommandAliasProvider; import net.ess3.provider.KnownCommandsProvider; @@ -156,5 +157,7 @@ public interface IEssentials extends Plugin { PersistentDataProvider getPersistentDataProvider(); + ReflOnlineModeProvider getOnlineModeProvider(); + PluginCommand getPluginCommand(String cmd); } diff --git a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java index 876304fe6..984f9cb4c 100644 --- a/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java +++ b/Essentials/src/main/java/com/earth2me/essentials/commands/Commandessentials.java @@ -190,6 +190,7 @@ public class Commandessentials extends EssentialsCommand { serverData.addProperty("bukkit-version", Bukkit.getBukkitVersion()); serverData.addProperty("server-version", Bukkit.getVersion()); serverData.addProperty("server-brand", Bukkit.getName()); + serverData.addProperty("online-mode", ess.getOnlineModeProvider().getOnlineModeString()); final JsonObject supportStatus = new JsonObject(); final VersionUtil.SupportStatus status = VersionUtil.getServerSupportStatus(); supportStatus.addProperty("status", status.name()); diff --git a/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflOnlineModeProvider.java b/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflOnlineModeProvider.java new file mode 100644 index 000000000..b5c43c2a6 --- /dev/null +++ b/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflOnlineModeProvider.java @@ -0,0 +1,59 @@ +package net.ess3.nms.refl.providers; + +import org.bukkit.Bukkit; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; + +public class ReflOnlineModeProvider { + private final MethodHandle spigotBungeeGetter; + private final MethodHandle paperBungeeGetter; + private final boolean fancyPaperCheck; + + public ReflOnlineModeProvider() { + MethodHandle spigotBungeeGetter = null; + MethodHandle paperBungeeGetter = null; + boolean fancyCheck = false; + try { + final MethodHandles.Lookup lookup = MethodHandles.lookup(); + spigotBungeeGetter = lookup.findStaticGetter(Class.forName("org.spigotmc.SpigotConfig"), "bungee", boolean.class); + final Class paperConfig = Class.forName("com.destroystokyo.paper.PaperConfig"); + paperBungeeGetter = lookup.findStaticGetter(paperConfig, "bungeeOnlineMode", boolean.class); + paperBungeeGetter = lookup.findStatic(paperConfig, "isProxyOnlineMode", MethodType.methodType(boolean.class)); + fancyCheck = true; + } catch (Throwable ignored) { + } + this.spigotBungeeGetter = spigotBungeeGetter; + this.paperBungeeGetter = paperBungeeGetter; + this.fancyPaperCheck = fancyCheck; + } + + public String getOnlineModeString() { + if (spigotBungeeGetter == null) { + return Bukkit.getOnlineMode() ? "Online Mode" : "Offline Mode"; + } + + try { + if (Bukkit.getOnlineMode()) { + return "Online Mode"; + } + + if (fancyPaperCheck) { + if ((boolean) paperBungeeGetter.invoke()) { + // Could be Velocity or Bungee, so do not specify. + return "Proxy Mode"; + } + return "Offline Mode"; + } + + if ((boolean) spigotBungeeGetter.invoke() && (paperBungeeGetter == null || (boolean) paperBungeeGetter.invoke())) { + return "Bungee Mode"; + } + + return "Offline Mode"; + } catch (Throwable ignored) { + return "Unknown"; + } + } +}