currently refactoring a lot.
This commit is contained in:
parent
fe0b04c7d1
commit
9aa138058c
32 changed files with 484 additions and 254 deletions
|
|
@ -1,27 +1,50 @@
|
|||
package de.kentoj.kencommandapi;
|
||||
|
||||
import de.kentoj.kencommandapi.api.CommandNode;
|
||||
import de.kentoj.kencommandapi.internal.parser.CommandParser;
|
||||
import de.kentoj.kencommandapi.api.CommandHandler;
|
||||
import de.kentoj.kencommandapi.api.node.RootCommandNode;
|
||||
import de.kentoj.kencommandapi.api.parser.CommandParser;
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.bukkit.command.SimpleCommandMap;
|
||||
import org.bukkit.permissions.Permissible;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class CommandAPI {
|
||||
|
||||
private final CommandParser<CommandSender> commandParser = new CommandParser<>(Permissible::hasPermission);
|
||||
private final CommandHandler<CommandSender> commandHandler;
|
||||
private final List<RootCommandNode<CommandSender>> registeredNodes = new ArrayList<>();
|
||||
private final SimpleCommandMap commandMap;
|
||||
|
||||
public CommandAPI(Plugin plugin) {
|
||||
this.commandHandler = new CommandHandler<>(new CommandParser<>(Permissible::hasPermission),
|
||||
CommandSender::sendMessage, Permissible::hasPermission);
|
||||
Bukkit.getPluginManager().registerEvents(new AsyncTabCompleter(this, commandHandler), plugin);
|
||||
|
||||
public void register(CommandNode<CommandSender> rootNode) {
|
||||
SimpleCommandMap commandMap;
|
||||
try {
|
||||
Field field = Bukkit.getPluginManager().getClass().getDeclaredField("commandMap");
|
||||
field.setAccessible(true);
|
||||
commandMap = (SimpleCommandMap) field.get(Bukkit.getPluginManager());
|
||||
} catch (NoSuchFieldException | IllegalAccessException e) {
|
||||
throw new IllegalStateException("failed registering command ", e);
|
||||
throw new IllegalStateException("failed getting command map", e);
|
||||
}
|
||||
commandMap.register(rootNode.getNames()[0], new BukkitKenCommand(commandParser, rootNode));
|
||||
}
|
||||
|
||||
@Nullable RootCommandNode<CommandSender> getRegistered(String name) {
|
||||
for (var node : registeredNodes) {
|
||||
for (String nodeName : node.getNames()) {
|
||||
if (nodeName.equals(name)) return node;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void register(RootCommandNode<CommandSender> rootNode) {
|
||||
registeredNodes.add(rootNode);
|
||||
commandMap.register(rootNode.getNames()[0], new BukkitKenCommand(commandHandler, rootNode));
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue