From 5b146f8383aa078c5f2b2b22bf8df520c381fd01 Mon Sep 17 00:00:00 2001 From: JRoy <10731363+JRoy@users.noreply.github.com> Date: Wed, 18 Dec 2024 21:05:03 -0500 Subject: [PATCH] fix issue w/FormattedCommandAliasProvider on 1.8 forks --- .../ReflFormattedCommandAliasProvider.java | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflFormattedCommandAliasProvider.java b/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflFormattedCommandAliasProvider.java index 1e3d9f615..ff20f66b3 100644 --- a/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflFormattedCommandAliasProvider.java +++ b/providers/NMSReflectionProvider/src/main/java/net/ess3/nms/refl/providers/ReflFormattedCommandAliasProvider.java @@ -15,14 +15,12 @@ import java.util.List; @ProviderData(description = "Reflection Formatted Command Alias Provider") public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasProvider { - - private final boolean paper; + private final boolean senderArg; private final Field formatStringsField; private final MethodHandle buildCommandMethodHandle; - public ReflFormattedCommandAliasProvider(final boolean paper) { - this.paper = paper; - + public ReflFormattedCommandAliasProvider() { + boolean senderArg = true; final Class formattedCommandAliasClass; Field formatStringsField = null; MethodHandle buildCommandMethodHandle = null; @@ -30,14 +28,16 @@ public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasP formattedCommandAliasClass = FormattedCommandAlias.class; formatStringsField = ReflUtil.getFieldCached(formattedCommandAliasClass, "formatStrings"); - final Class[] parameterTypes; - if (paper) { - parameterTypes = new Class[] {CommandSender.class, String.class, String[].class}; - } else { - parameterTypes = new Class[] {String.class, String[].class}; + Method buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", CommandSender.class, String.class, String[].class); + if (buildCommandMethod == null) { + senderArg = false; + buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", String.class, String[].class); + } + + if (buildCommandMethod == null) { + throw new NoSuchMethodException("Could not find buildCommand method in FormattedCommandAlias"); } - final Method buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", parameterTypes); buildCommandMethod.setAccessible(true); buildCommandMethodHandle = MethodHandles.lookup().unreflect(buildCommandMethod); } catch (final Exception ex) { @@ -45,6 +45,7 @@ public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasP } finally { this.formatStringsField = formatStringsField; this.buildCommandMethodHandle = buildCommandMethodHandle; + this.senderArg = senderArg; } } @@ -77,7 +78,7 @@ public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasP @Override public String buildCommand(FormattedCommandAlias command, CommandSender sender, String formatString, String[] args) { try { - if (paper) { + if (senderArg) { return (String) buildCommandMethodHandle.invoke(command, sender, formatString, args); } else { return (String) buildCommandMethodHandle.invoke(command, formatString, args);