mirror of
https://github.com/xHyroM/lighteco.git
synced 2024-11-22 15:01:05 +01:00
refactor: cleanup, rename bukkit to paper
This commit is contained in:
parent
ce2743bd36
commit
a71a2b6454
1 changed files with 56 additions and 0 deletions
|
@ -0,0 +1,56 @@
|
|||
package dev.xhyrom.lighteco.paper.util;
|
||||
|
||||
import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin;
|
||||
import org.bukkit.command.*;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.java.JavaPlugin;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class PaperCommandMapUtil {
|
||||
private static final Constructor<PluginCommand> COMMAND_CONSTRUCTOR;
|
||||
|
||||
static {
|
||||
try {
|
||||
COMMAND_CONSTRUCTOR = PluginCommand.class.getDeclaredConstructor(String.class, Plugin.class);
|
||||
COMMAND_CONSTRUCTOR.setAccessible(true);
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new ExceptionInInitializerError(e);
|
||||
}
|
||||
}
|
||||
|
||||
private final LightEcoPlugin plugin;
|
||||
public PaperCommandMapUtil(LightEcoPlugin plugin) {
|
||||
this.plugin = plugin;
|
||||
}
|
||||
|
||||
public void register(CommandExecutor executor, TabCompleter completer, List<String> aliases) {
|
||||
Plugin bukkitPlugin = (JavaPlugin) this.plugin.getBootstrap().getLoader();
|
||||
CommandMap commandMap = bukkitPlugin.getServer().getCommandMap();
|
||||
Map<String, Command> knownCommands = commandMap.getKnownCommands();
|
||||
|
||||
for (String name : aliases) {
|
||||
if (!name.toLowerCase().equals(name)) {
|
||||
throw new IllegalArgumentException("Command aliases must be lowercase! (name: " + name + ")");
|
||||
}
|
||||
|
||||
try {
|
||||
PluginCommand command = COMMAND_CONSTRUCTOR.newInstance(name, bukkitPlugin);
|
||||
|
||||
commandMap.register(bukkitPlugin.getName().toLowerCase(), command);
|
||||
knownCommands.put(bukkitPlugin.getName().toLowerCase() + ":" + name.toLowerCase(), command);
|
||||
knownCommands.put(name, command);
|
||||
|
||||
command.setLabel(name);
|
||||
command.setExecutor(executor);
|
||||
command.setTabCompleter(completer);
|
||||
|
||||
commandMap.register(bukkitPlugin.getName(), command);
|
||||
} catch (ReflectiveOperationException e) {
|
||||
this.plugin.getBootstrap().getLogger().error("Failed to register command: %s", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue