This commit is contained in:
YuTian 2024-07-23 16:22:37 +08:00
parent 22e35ef80a
commit 1148cdbb2e
5 changed files with 365 additions and 10 deletions

View File

@ -2,10 +2,6 @@ package com.io.yutian.aulib.command;
public interface IAlias { public interface IAlias {
default boolean inMainCommand() {
return true;
}
String[] getAlias(); String[] getAlias();
} }

View File

@ -0,0 +1,295 @@
package com.io.yutian.aulib.command;
import com.io.yutian.aulib.command.argument.Argument;
import com.io.yutian.aulib.command.argument.ArgumentValue;
import com.io.yutian.aulib.command.handler.CommandHandler;
import com.io.yutian.aulib.lang.Lang;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import java.util.*;
import java.util.stream.Stream;
public class SimpleCommandHandler extends Command {
private ICommand iCommand;
public SimpleCommandHandler(String name, ICommand iCommand) {
super(name);
this.iCommand = iCommand;
}
@Override
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
executes(sender, commandLabel, args);
return true;
}
public void executes(CommandSender sender, String commandLabel, String[] args) {
if (!iCommand.hasPermission(sender)) {
sender.sendMessage(Lang.get("command-no-permission"));
return;
}
List<CommandNode> commandNodes = iCommand.getCommandNodes();
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < args.length; i++) {
stringBuilder.append(args[i]);
if (i < args.length - 1) {
stringBuilder.append(" ");
}
}
String commandString = stringBuilder.toString();
if (commandNodes.size() == 0) {
Map<String, ArgumentValue> map = new HashMap<>();
if (iCommand.getArguments().size() > 0) {
int argSize = args.length;
List<Argument> arguments = iCommand.getArguments();
int k = 0;
if (arguments.get(arguments.size()-1).isOptional()) {
k++;
}
if (argSize < arguments.size()-k) {
sender.sendMessage(Lang.get("command-short-arg"));
return;
}
for (int l = 0; l < arguments.size(); l++) {
Argument a = arguments.get(l);
int index = l;
if (index >= args.length) {
break;
}
String arg = args[index];
if (!a.getArgumentsType().test(arg)) {
sender.sendMessage(Lang.get("command-unknown-arg", index+1, arg));
return;
}
}
map = parseArgumentValue(sender, arguments, args, -1);
}
iCommand.executes(new CommandContext(commandString, commandLabel, sender, map));
return;
}
int nodeSize = args.length;
if (commandNodes.size() > 0 && nodeSize == 0) {
if (!iCommand.emptyExecutes(sender)) {
sender.sendMessage(Lang.get("command-short-arg"));
}
return;
}
String mainNode = args[0];
Stream<CommandNode> nodeStream = commandNodes.stream().filter((n) -> {
return n.getName().equalsIgnoreCase(mainNode) || n.getAlias().contains(mainNode);
});
Optional<CommandNode> nodeOptional = nodeStream.findFirst();
if (!nodeOptional.isPresent()) {
sender.sendMessage(Lang.get("command-unknown-arg", 1, mainNode));
return;
}
CommandNode node = nodeOptional.get();
if (node.getChildrens().size() > 0) {
checkClidren(commandString, commandLabel, sender, 0, args, node);
} else {
if (node.getCommand() != null) {
Map<String, ArgumentValue> map = new HashMap<>();
if (node.getArguments().size() > 0) {
int argSize = args.length - 1;
List<Argument> arguments = node.getArguments();
int k = 0;
if (arguments.get(arguments.size()-1).isOptional()) {
k++;
}
if (argSize < arguments.size()-k) {
sender.sendMessage(Lang.get("command-short-arg"));
return;
}
for (int l = 0; l < arguments.size(); l++) {
Argument a = arguments.get(l);
int index = l + 1;
if (index >= args.length) {
break;
}
String arg = args[index];
if (!a.getArgumentsType().test(arg)) {
sender.sendMessage(Lang.get("command-error-arg", index+1, arg));
return;
}
}
map = parseArgumentValue(sender, node.getArguments(), args, 0);
}
node.getCommand().run(new CommandContext(commandString, commandLabel, sender, map));
} else {
sender.sendMessage(Lang.get("command-unknown-arg", 2, mainNode));
return;
}
}
}
private void checkClidren(String commandString, String commandLabel, CommandSender sender, int i, String[] args, CommandNode node) {
i++;
if (i >= args.length) {
if (node.getCommand() == null) {
sender.sendMessage(Lang.get("command-short-arg"));
} else {
node.getCommand().run(new CommandContext(commandString, commandLabel, sender, new HashMap<>()));
}
return;
}
String s = args[i];
Stream<CommandNode> nodeStream = node.getChildrens().stream().filter((n) -> {
return n.getName().equalsIgnoreCase(s) || n.getAlias().contains(s);
});
Optional<CommandNode> nodeOptional = nodeStream.findFirst();
if (!nodeOptional.isPresent()) {
sender.sendMessage(Lang.get("command-unknown-arg", i+1, s));
return;
}
CommandNode node1 = nodeOptional.get();
if (node1.getChildrens().size() > 0) {
checkClidren(commandString, commandLabel, sender, i, args, node1);
} else {
if (node1.getCommand() != null) {
Map<String, ArgumentValue> map = new HashMap<>();
if (node1.getArguments().size() > 0) {
int argSize = args.length - i - 1;
List<Argument> arguments = node1.getArguments();
int k = 0;
if (arguments.get(arguments.size()-1).isOptional()) {
k++;
}
if (argSize < arguments.size()-k) {
sender.sendMessage(Lang.get("command-short-arg"));
return;
}
for (int l = 0; l < arguments.size(); l++) {
Argument a = arguments.get(l);
int index = i + l + 1;
if (index >= args.length) {
break;
}
String arg = args[index];
if (!a.getArgumentsType().test(arg)) {
sender.sendMessage(Lang.get("command-unknown-arg", index+1, arg));
return;
}
}
map = parseArgumentValue(sender, node1.getArguments(), args, i);
}
node1.getCommand().run(new CommandContext(commandString, commandLabel, sender, map));
} else {
sender.sendMessage(Lang.get("command-unknown-arg", i+1, s));
return;
}
}
}
private Map<String, ArgumentValue> parseArgumentValue(CommandSender commandSender, List<Argument> argumentList, String[] args, int i) {
Map<String, ArgumentValue> map = new HashMap<>();
List<Argument> arguments = argumentList;
for (int l = 0; l < arguments.size(); l++) {
Argument a = arguments.get(l);
if (i+1+l >= args.length) {
if (a.isOptional()) {
map.put(a.getName(), new ArgumentValue(a.getDefaultValue()));
}
return map;
}
String arg = args[i+1+l];
if (!a.getArgumentsType().test(arg)) {
continue;
}
ArgumentValue argumentValue = new ArgumentValue(a.getArgumentsType().get(arg));
map.put(a.getName(), argumentValue);
}
return map;
}
@Override
public List<String> tabComplete(CommandSender sender, String alias, String[] args) throws IllegalArgumentException {
return onTabComplete(sender, alias, args);
}
public List<String> onTabComplete(CommandSender sender, String alias, String[] args) {
List<String> list = new ArrayList<>();
int index = args.length;
String arg = args[index-1];
if (!iCommand.hasPermission(sender)) {
return list;
}
if (iCommand instanceof ITabCompleter) {
ITabCompleter tabCompleter = (ITabCompleter) iCommand;
return tabCompleter.onTabComplete(sender, args, index-1, arg);
} else {
Map<Integer, List<String>> map = new HashMap<>();
if (iCommand.getCommandNodes().size() > 0) {
List<String> list1 = new ArrayList<>();
for (CommandNode node : iCommand.getCommandNodes()) {
list1.add(node.getName());
list1.addAll(node.getAlias());
if (index >= 2) {
if (!node.getName().equalsIgnoreCase(args[0])) {
continue;
}
}
if (node.getChildrens().size() > 0) {
getTabComplete(node, 1, map);
} else if (node.getArguments().size() > 0) {
List<Argument> arguments = node.getArguments();
for (int l = 0; l < arguments.size(); l++) {
Argument argument = arguments.get(l);
if (argument.getSuggest() != null) {
map.put(2+l, argument.getSuggest().getSuggest());
continue;
}
map.put(2+l, Arrays.asList("<"+argument.getName()+">"));
}
}
}
map.put(1, list1);
return CommandHandler.preseSuggest(map.getOrDefault(index, list), arg);
} else if (iCommand.getArguments().size() > 0) {
List<Argument> arguments = iCommand.getArguments();
for (int l = 0; l < arguments.size(); l++) {
Argument argument = arguments.get(l);
if (argument.getSuggest() != null) {
map.put(0+l+1, argument.getSuggest().getSuggest());
continue;
}
map.put(0+l+1, Arrays.asList("<"+argument.getName()+">"));
}
return CommandHandler.preseSuggest(map.getOrDefault(index, list), arg);
}
}
return CommandHandler.preseSuggest(list, arg);
}
private void getTabComplete(CommandNode node, int i, Map<Integer, List<String>> map) {
i++;
List<String> list = map.getOrDefault(i, new ArrayList<>());
for (CommandNode c : node.getChildrens()) {
list.add(c.getName());
if (c.getChildrens().size() > 0) {
getTabComplete(c, i, map);
} else if (c.getArguments().size() > 0) {
List<Argument> arguments = c.getArguments();
for (int l = 0; l < arguments.size(); l++) {
Argument argument = arguments.get(l);
if (argument.getSuggest() != null) {
map.put(i+l+1, argument.getSuggest().getSuggest());
continue;
}
map.put(i+l+1, Arrays.asList("<"+argument.getName()+">"));
}
}
}
map.put(i, list);
}
}

View File

@ -3,24 +3,35 @@ package com.io.yutian.aulib.command;
import com.io.yutian.aulib.command.handler.CommandHandler; import com.io.yutian.aulib.command.handler.CommandHandler;
import com.io.yutian.aulib.command.list.CommandHelp; import com.io.yutian.aulib.command.list.CommandHelp;
import org.bukkit.Bukkit; import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandMap;
import org.bukkit.plugin.Plugin; import org.bukkit.plugin.Plugin;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
public class SimpleCommandManager implements ICommandManager { public class SimpleCommandManager implements ICommandManager {
protected static Map<String, Command> bukkitCommandMap = new HashMap<>();
protected static CommandMap commandMap;
private final Plugin plugin;
private final String name; private final String name;
@NotNull @NotNull
private List<ICommand> commands; private List<ICommand> commands;
public SimpleCommandManager(String name) {
this(name, new ArrayList<>()); public SimpleCommandManager(Plugin plugin, String name) {
this(plugin, name, new ArrayList<>());
} }
public SimpleCommandManager(String name, @NotNull List<ICommand> commands) { public SimpleCommandManager(Plugin plugin, String name, @NotNull List<ICommand> commands) {
this.plugin = plugin;
this.name = name; this.name = name;
this.commands = commands; this.commands = commands;
register(new CommandHelp(this)); register(new CommandHelp(this));
@ -31,12 +42,52 @@ public class SimpleCommandManager implements ICommandManager {
return; return;
} }
commands.add(command); commands.add(command);
if (command instanceof IAlias alias) {
String[] array = alias.getAlias();
for (String s : array) {
registerPluginBukkitCommand(plugin, s, command);
}
}
} }
public void registerBukkitCommand(@NotNull Plugin plugin, String commandName) { public void unregisterAll() {
for (ICommand command : commands) {
if (command instanceof IAlias) {
unregister(command);
}
}
}
public static void unregister(ICommand command) {
if (!(command instanceof IAlias)) {
return;
}
try {
Map<String, Command> map = (Map<String, Command>) commandMap.getClass().getMethod("getKnownCommands").invoke(commandMap);
for (String name : ((IAlias) command).getAlias()) {
map.remove(name);
Command bukkitCommand = bukkitCommandMap.get(name);
bukkitCommand.unregister(commandMap);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void registerPluginCommand(@NotNull Plugin plugin, String commandName) {
Bukkit.getPluginCommand(commandName).setExecutor(new CommandHandler(plugin, this)); Bukkit.getPluginCommand(commandName).setExecutor(new CommandHandler(plugin, this));
} }
protected static void registerPluginBukkitCommand(Plugin plugin, String name, ICommand command) {
SimpleCommandHandler simpleCommandHandler = new SimpleCommandHandler(name, command);
bukkitCommandMap.put(name, simpleCommandHandler);
commandMap.register(plugin.getName(), simpleCommandHandler);
}
public Plugin getPlugin() {
return plugin;
}
@NotNull @NotNull
@Override @Override
public String getName() { public String getName() {
@ -49,4 +100,17 @@ public class SimpleCommandManager implements ICommandManager {
return commands; return commands;
} }
static {
try {
Class<?> c = Bukkit.getServer().getClass();
for (Method method : c.getDeclaredMethods()) {
if (method.getName().equals("getCommandMap")) {
commandMap = (CommandMap) method.invoke(Bukkit.getServer(), new Object[0]);
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
} }

View File

@ -33,7 +33,7 @@ public class RedisCacheSyncTimer {
PluginInfo pluginInfo = entry.getValue(); PluginInfo pluginInfo = entry.getValue();
String lockKey = LOCK_KEY + "_" + plugin.getName(); String lockKey = LOCK_KEY + "_" + plugin.getName();
IJedisGetter jedisGetter = pluginInfo.getJedisGetter(); IJedisGetter jedisGetter = pluginInfo.getJedisGetter();
try (Jedis jedis = jedisGetter.getRedis()) { try (Jedis jedis = jedisGetter.getRedis()) {·
long lockResult = jedis.setnx(lockKey, "locked"); long lockResult = jedis.setnx(lockKey, "locked");
if (lockResult != 1) { if (lockResult != 1) {
continue; continue;

View File

@ -1,5 +1,5 @@
name: AuLib name: AuLib
main: com.io.yutian.aulib.AuLib main: com.io.yutian.aulib.AuLib
version: 2.5.2 version: 2.6
api-version: 1.18 api-version: 1.18
author: SuperYuTian author: SuperYuTian