|
| 1 | +package net.ess3.nms.refl.providers; |
| 2 | + |
| 3 | +import net.ess3.nms.refl.ReflUtil; |
| 4 | +import net.ess3.provider.FormattedCommandAliasProvider; |
| 5 | +import org.bukkit.command.CommandSender; |
| 6 | +import org.bukkit.command.FormattedCommandAlias; |
| 7 | + |
| 8 | +import java.lang.reflect.Field; |
| 9 | +import java.lang.reflect.Method; |
| 10 | +import java.util.ArrayList; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +public class ReflFormattedCommandAliasProvider implements FormattedCommandAliasProvider { |
| 14 | + |
| 15 | + private final boolean paper; |
| 16 | + private final Field formatStringsField; |
| 17 | + private final Method buildCommandMethod; |
| 18 | + |
| 19 | + public ReflFormattedCommandAliasProvider(boolean paper) { |
| 20 | + this.paper = paper; |
| 21 | + Field formatStringsField = null; |
| 22 | + Method buildCommandMethod = null; |
| 23 | + try { |
| 24 | + @SuppressWarnings("unchecked") |
| 25 | + final Class<? extends FormattedCommandAlias> formattedCommandAliasClass = (Class<? extends FormattedCommandAlias>) ReflUtil.getOBClass("command.FormattedCommandAlias"); |
| 26 | + if (formattedCommandAliasClass != null) { |
| 27 | + formatStringsField = ReflUtil.getFieldCached(formattedCommandAliasClass, "formatStrings"); |
| 28 | + if (paper) { |
| 29 | + buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", CommandSender.class, String.class, String[].class); |
| 30 | + } else { |
| 31 | + buildCommandMethod = ReflUtil.getMethodCached(formattedCommandAliasClass, "buildCommand", String.class, String[].class); |
| 32 | + } |
| 33 | + } |
| 34 | + } catch (final Exception ex) { |
| 35 | + ex.printStackTrace(); |
| 36 | + } finally { |
| 37 | + this.formatStringsField = formatStringsField; |
| 38 | + this.buildCommandMethod = buildCommandMethod; |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public List<String> createCommands(FormattedCommandAlias command, CommandSender sender, String[] args) { |
| 44 | + final List<String> commands = new ArrayList<>(); |
| 45 | + if (buildCommandMethod == null || formatStringsField == null) return commands; |
| 46 | + |
| 47 | + final String[] formatStrings; |
| 48 | + try { |
| 49 | + formatStrings = (String[]) formatStringsField.get(command); |
| 50 | + } catch (ReflectiveOperationException ex) { |
| 51 | + ex.printStackTrace(); |
| 52 | + return commands; |
| 53 | + } |
| 54 | + |
| 55 | + for (String formatString : formatStrings) { |
| 56 | + final String cmd; |
| 57 | + try { |
| 58 | + cmd = buildCommand(command, sender, formatString, args); |
| 59 | + } catch (Throwable th) { |
| 60 | + continue; // Ignore, let server handle this. |
| 61 | + } |
| 62 | + |
| 63 | + if (cmd == null) continue; |
| 64 | + commands.add(cmd.trim()); |
| 65 | + } |
| 66 | + return commands; |
| 67 | + } |
| 68 | + |
| 69 | + private String buildCommand(FormattedCommandAlias command, CommandSender sender, String formatString, String[] args) { |
| 70 | + try { |
| 71 | + if (paper) { |
| 72 | + return (String) buildCommandMethod.invoke(command, sender, formatString, args); |
| 73 | + } else { |
| 74 | + return (String) buildCommandMethod.invoke(command, formatString, args); |
| 75 | + } |
| 76 | + } catch (ReflectiveOperationException ex) { |
| 77 | + ex.printStackTrace(); |
| 78 | + } |
| 79 | + return null; |
| 80 | + } |
| 81 | +} |
0 commit comments