Use reflection to set name if setName doesnt exists, Fixes #16

This commit is contained in:
Aikar
2017-04-21 21:00:17 -04:00
parent 9793c7e8ae
commit ea14d121d5
@@ -38,6 +38,7 @@ import org.bukkit.command.CommandSender;
import org.bukkit.configuration.InvalidConfigurationException;
import org.bukkit.util.StringUtil;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
@@ -108,7 +109,18 @@ public abstract class BaseCommand extends Command {
cmd = CommandPatterns.PIPE.split(rootCmdAlias.value())[0];
}
cmd = cmd.toLowerCase();
setName(cmd);
try {
setName(cmd);
} catch (NoSuchMethodError ignored) {
try {
// To support pre 1.8 where setName was not added.
Field field = Command.class.getDeclaredField("name");
field.setAccessible(true);
field.set(this, cmd);
} catch (NoSuchFieldException | IllegalAccessException e) {
CommandLog.exception("Error setting name for command", e);
}
}
setLabel(cmd);
}