fix issue w/FormattedCommandAliasProvider on 1.8 forks

This commit is contained in:
JRoy 2024-12-18 21:05:03 -05:00
parent c8d6f64deb
commit 5b146f8383
No known key found for this signature in database
GPG Key ID: FAD510B503869E7D

View File

@ -15,14 +15,12 @@ import java.util.List;
@ProviderData(description = "Reflection Formatted Command Alias Provider") @ProviderData(description = "Reflection Formatted Command Alias Provider")
public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasProvider { public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasProvider {
private final boolean senderArg;
private final boolean paper;
private final Field formatStringsField; private final Field formatStringsField;
private final MethodHandle buildCommandMethodHandle; private final MethodHandle buildCommandMethodHandle;
public ReflFormattedCommandAliasProvider(final boolean paper) { public ReflFormattedCommandAliasProvider() {
this.paper = paper; boolean senderArg = true;
final Class<? extends FormattedCommandAlias> formattedCommandAliasClass; final Class<? extends FormattedCommandAlias> formattedCommandAliasClass;
Field formatStringsField = null; Field formatStringsField = null;
MethodHandle buildCommandMethodHandle = null; MethodHandle buildCommandMethodHandle = null;
@ -30,14 +28,16 @@ public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasP
formattedCommandAliasClass = FormattedCommandAlias.class; formattedCommandAliasClass = FormattedCommandAlias.class;
formatStringsField = ReflUtil.getFieldCached(formattedCommandAliasClass, "formatStrings"); formatStringsField = ReflUtil.getFieldCached(formattedCommandAliasClass, "formatStrings");
final Class<?>[] parameterTypes; Method buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", CommandSender.class, String.class, String[].class);
if (paper) { if (buildCommandMethod == null) {
parameterTypes = new Class[] {CommandSender.class, String.class, String[].class}; senderArg = false;
} else { buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", String.class, String[].class);
parameterTypes = new Class[] {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); buildCommandMethod.setAccessible(true);
buildCommandMethodHandle = MethodHandles.lookup().unreflect(buildCommandMethod); buildCommandMethodHandle = MethodHandles.lookup().unreflect(buildCommandMethod);
} catch (final Exception ex) { } catch (final Exception ex) {
@ -45,6 +45,7 @@ public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasP
} finally { } finally {
this.formatStringsField = formatStringsField; this.formatStringsField = formatStringsField;
this.buildCommandMethodHandle = buildCommandMethodHandle; this.buildCommandMethodHandle = buildCommandMethodHandle;
this.senderArg = senderArg;
} }
} }
@ -77,7 +78,7 @@ public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasP
@Override @Override
public String buildCommand(FormattedCommandAlias command, CommandSender sender, String formatString, String[] args) { public String buildCommand(FormattedCommandAlias command, CommandSender sender, String formatString, String[] args) {
try { try {
if (paper) { if (senderArg) {
return (String) buildCommandMethodHandle.invoke(command, sender, formatString, args); return (String) buildCommandMethodHandle.invoke(command, sender, formatString, args);
} else { } else {
return (String) buildCommandMethodHandle.invoke(command, formatString, args); return (String) buildCommandMethodHandle.invoke(command, formatString, args);