From ac45e0937e00fc77d36e3274060f9c7ecfde70ff Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Tue, 12 Nov 2024 23:40:41 -0300 Subject: [PATCH 01/47] reworked the command system --- .../bungee/command/BungeeCommandExecutor.java | 21 +- .../bungee/command/BungeeCommandManager.java | 2 +- ...{GenericSender.java => CommandSender.java} | 15 +- .../command/sender/ProxiedPlayerSender.java | 42 --- .../wlib/command/CommandManager.java | 97 ++---- .../wlib/command/CommandSender.java | 12 - .../wlib/command/RegisteredCommand.java | 275 ------------------ .../command/{ => annotation}/Command.java | 4 +- .../command/data/AnnotationCommandData.java | 96 ++++++ .../wlib/command/data/CommandData.java | 66 +++++ .../command/exception/CommandException.java | 21 ++ .../command/executor/CommandExecutor.java | 12 + .../command/extractor/CommandExtractor.java | 13 + .../extractor/DirectCommandExtractor.java | 16 + .../extractor/MethodCommandExtractor.java | 44 +++ .../wlib/command/holder/CommandExecutor.java | 3 +- .../command/registered/RegisteredCommand.java | 131 +++++++++ .../registered/RegisteredMethodCommand.java | 122 ++++++++ .../wlib/command/BukkitCommandExecutor.java | 37 +-- .../wlib/command/sender/AbstractSender.java | 39 --- .../command/sender/BlockCommandSender.java | 8 - .../wlib/command/sender/CommandSender.java | 52 ++++ .../wlib/command/sender/ConsoleSender.java | 10 - .../wlib/command/sender/GenericSender.java | 21 -- .../wlib/command/sender/PlayerSender.java | 10 - 25 files changed, 629 insertions(+), 540 deletions(-) rename bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/{GenericSender.java => CommandSender.java} (64%) delete mode 100644 bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/ProxiedPlayerSender.java delete mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/RegisteredCommand.java rename commands/src/main/java/com/wizardlybump17/wlib/command/{ => annotation}/Command.java (96%) create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/exception/CommandException.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/extractor/CommandExtractor.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/extractor/DirectCommandExtractor.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java delete mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/sender/AbstractSender.java delete mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/sender/BlockCommandSender.java create mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java delete mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/sender/ConsoleSender.java delete mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/sender/GenericSender.java delete mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/sender/PlayerSender.java diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java index 57ed1875..743da1c2 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java @@ -1,13 +1,13 @@ package com.wizardlybump17.wlib.bungee.command; -import com.wizardlybump17.wlib.bungee.command.sender.GenericSender; -import com.wizardlybump17.wlib.bungee.command.sender.ProxiedPlayerSender; +import com.wizardlybump17.wlib.bungee.command.sender.CommandSender; import com.wizardlybump17.wlib.command.CommandManager; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.exception.CommandException; import com.wizardlybump17.wlib.command.holder.CommandExecutor; -import net.md_5.bungee.api.connection.ProxiedPlayer; import net.md_5.bungee.api.plugin.Command; +import java.util.logging.Level; + public class BungeeCommandExecutor extends Command implements CommandExecutor { private final CommandManager manager; @@ -18,17 +18,16 @@ public BungeeCommandExecutor(CommandManager manager, String name) { } @Override - public void execute(CommandSender sender, String commandName, String[] args) { + public void execute(com.wizardlybump17.wlib.command.CommandSender sender, String commandName, String[] args) throws CommandException { manager.execute(sender, commandName + " " + String.join(" ", args)); } @Override public void execute(net.md_5.bungee.api.CommandSender sender, String[] args) { - CommandSender realSender; - if (sender instanceof ProxiedPlayer) - realSender = new ProxiedPlayerSender((ProxiedPlayer) sender); - else - realSender = new GenericSender(sender); - execute(realSender, getName(), args); + try { + execute(new CommandSender(sender), getName(), args); + } catch (CommandException e) { + manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command", e); + } } } diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java index 48c0b047..416de0ef 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java @@ -1,7 +1,7 @@ package com.wizardlybump17.wlib.bungee.command; import com.wizardlybump17.wlib.command.CommandManager; -import com.wizardlybump17.wlib.command.RegisteredCommand; +import com.wizardlybump17.wlib.command.registered.RegisteredCommand; import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Plugin; diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/GenericSender.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java similarity index 64% rename from bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/GenericSender.java rename to bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java index 86698b06..ef0ddda4 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/GenericSender.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java @@ -1,11 +1,13 @@ package com.wizardlybump17.wlib.bungee.command.sender; -import com.wizardlybump17.wlib.command.CommandSender; import lombok.RequiredArgsConstructor; import net.md_5.bungee.api.chat.TextComponent; +import net.md_5.bungee.api.connection.ConnectedPlayer; +import net.md_5.bungee.api.connection.ProxiedPlayer; +import org.jetbrains.annotations.NotNull; @RequiredArgsConstructor -public class GenericSender implements CommandSender { +public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender { private final net.md_5.bungee.api.CommandSender handle; @@ -34,12 +36,11 @@ public boolean hasPermission(String permission) { return handle.hasPermission(permission); } - @Override - public GenericSender toGeneric() { - return this; + public @NotNull ProxiedPlayer asProxiedPlayer() { + return (ProxiedPlayer) handle; } - public static boolean isGeneric() { - return true; + public @NotNull ConnectedPlayer asConnectedPlayer() { + return (ConnectedPlayer) handle; } } diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/ProxiedPlayerSender.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/ProxiedPlayerSender.java deleted file mode 100644 index d2fca3f7..00000000 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/ProxiedPlayerSender.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.wizardlybump17.wlib.bungee.command.sender; - -import com.wizardlybump17.wlib.command.CommandSender; -import lombok.RequiredArgsConstructor; -import net.md_5.bungee.api.chat.TextComponent; -import net.md_5.bungee.api.connection.ProxiedPlayer; - -@RequiredArgsConstructor -public class ProxiedPlayerSender implements CommandSender { - - private final ProxiedPlayer handle; - - @Override - public ProxiedPlayer getHandle() { - return handle; - } - - @Override - public void sendMessage(String message) { - handle.sendMessage(TextComponent.fromLegacyText(message)); - } - - @Override - public void sendMessage(String... messages) { - handle.sendMessage(TextComponent.fromLegacyText(String.join("\n", messages))); - } - - @Override - public String getName() { - return handle.getName(); - } - - @Override - public boolean hasPermission(String permission) { - return handle.hasPermission(permission); - } - - @Override - public GenericSender toGeneric() { - return new GenericSender(handle); - } -} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 371354f6..957ee572 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -1,22 +1,17 @@ package com.wizardlybump17.wlib.command; +import com.wizardlybump17.wlib.command.data.CommandData; +import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.extractor.CommandExtractor; import com.wizardlybump17.wlib.command.holder.CommandHolder; -import com.wizardlybump17.wlib.util.ReflectionUtil; +import com.wizardlybump17.wlib.command.registered.RegisteredCommand; import lombok.Getter; import lombok.NonNull; import lombok.RequiredArgsConstructor; -import org.jetbrains.annotations.Nullable; +import org.jetbrains.annotations.NotNull; -import java.lang.invoke.MethodHandle; -import java.lang.invoke.MethodHandles; -import java.lang.invoke.MethodType; import java.lang.reflect.Field; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.logging.Level; +import java.util.*; @Getter @RequiredArgsConstructor @@ -25,35 +20,12 @@ public class CommandManager { private final List commands = new ArrayList<>(); protected final CommandHolder holder; private final @NonNull Map, Map> fieldCache = new HashMap<>(); + private final @NotNull Set commandExtractors = new HashSet<>(); public void registerCommands(Object... objects) { - for (Object object : objects) { - for (Method method : object.getClass().getDeclaredMethods()) { - if (!method.isAnnotationPresent(Command.class) || method.getParameterCount() == 0 || !CommandSender.class.isAssignableFrom(method.getParameterTypes()[0])) - continue; - - MethodHandle handle; - try { - handle = MethodHandles.publicLookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); - } catch (NoSuchMethodException | IllegalAccessException e) { - holder.getLogger().log(Level.SEVERE, "Error while trying to get the MethodHandle for " + method.getName() + " at " + object.getClass().getName(), e); - continue; - } - - RegisteredCommand command = new RegisteredCommand( - method.getAnnotation(Command.class), - object, - method, - handle - ); - - commands.add(command); - com.wizardlybump17.wlib.command.holder.Command holderCommand = holder.getCommand(command.getName()); - if (holderCommand != null) - holderCommand.setExecutor(holderCommand.getDefaultExecutor(this, command.getName())); - } - } - + for (Object object : objects) + for (CommandExtractor extractor : commandExtractors) + commands.addAll(extractor.extract(this, holder, object)); commands.sort(null); } @@ -61,22 +33,26 @@ public void unregisterCommands() { commands.clear(); } - public void execute(CommandSender sender, String string) { + public void execute(CommandSender sender, String string) throws CommandException { if (commands.isEmpty()) return; for (RegisteredCommand registeredCommand : commands) { - Command command = registeredCommand.getCommand(); + CommandData command = registeredCommand.getCommand(); CommandResult result = registeredCommand.execute(sender, string); switch (result) { case PERMISSION_FAIL -> { - handleMessage(registeredCommand, sender, command.permissionMessage(), command.permissionMessageIsField()); + String message = command.getPermissionMessage(); + if (message != null) + sender.sendMessage(message); return; } case INVALID_SENDER -> { - handleMessage(registeredCommand, sender, command.invalidSenderMessage(), command.invalidSenderMessageIsField()); + String message = command.getInvalidSenderMessage(); + if (message != null) + sender.sendMessage(message); return; } @@ -87,40 +63,9 @@ public void execute(CommandSender sender, String string) { } } - protected void handleMessage(@NonNull RegisteredCommand registeredCommand, @NonNull CommandSender sender, @NonNull String message, boolean isField) { - if (!isField) { - if (!message.isEmpty()) - sender.sendMessage(message); - return; - } - - String fieldMessage = getFieldMessage(registeredCommand, message); - if (fieldMessage != null) - sender.sendMessage(fieldMessage); - } - - protected @Nullable String getFieldMessage(@NonNull RegisteredCommand registeredCommand, @NonNull String fieldName) { - Map fields = fieldCache.computeIfAbsent(registeredCommand.getObject().getClass(), clazz -> { - Map map = new HashMap<>(); - for (Field field : clazz.getDeclaredFields()) - map.put(field.getName(), field); - return map; - }); - - Field field = fields.get(fieldName); - if (field == null) - return null; - - Object fieldValue = ReflectionUtil.getFieldValue(field, registeredCommand.getObject()); - return fieldValue == null ? null : fieldValue.toString(); - } - @NonNull public List<@NonNull String> autoComplete(@NonNull CommandSender sender, @NonNull String string) { - List result = new ArrayList<>(); - for (RegisteredCommand command : commands) - result.addAll(command.autoComplete(sender, string)); - return result; + return List.of(); } public List getCommand(String name) { @@ -131,10 +76,10 @@ public List getCommand(String name) { return commands; } - public List getCommands(Object object) { + public List getCommands(@NotNull Object object) { List commands = new ArrayList<>(this.commands.size()); for (RegisteredCommand command : this.commands) - if (command.getObject() == object) + if (command.isOwnedBy(object)) commands.add(command); return commands; } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java index c4b89aa3..127a3037 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java @@ -1,7 +1,5 @@ package com.wizardlybump17.wlib.command; -import org.jetbrains.annotations.Nullable; - /** * Represents a command sender, someone that can trigger commands. * @@ -21,14 +19,4 @@ public interface CommandSender { String getName(); boolean hasPermission(String permission); - - /** - * Used to convert this CommandSender to a generic sender, a command sender that can be anything - * - * @return the generic sender - */ - @Nullable - default CommandSender toGeneric() { - return null; - } } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/RegisteredCommand.java deleted file mode 100644 index 5ae2885e..00000000 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/RegisteredCommand.java +++ /dev/null @@ -1,275 +0,0 @@ -package com.wizardlybump17.wlib.command; - -import com.wizardlybump17.wlib.command.args.ArgsNode; -import com.wizardlybump17.wlib.command.args.ArgsReaderRegistry; -import com.wizardlybump17.wlib.command.args.ArgsReaderType; -import com.wizardlybump17.wlib.command.args.reader.ArgsReader; -import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; -import com.wizardlybump17.wlib.object.Pair; -import lombok.Getter; -import lombok.NonNull; -import org.jetbrains.annotations.NotNull; - -import java.lang.invoke.MethodHandle; -import java.lang.reflect.Method; -import java.lang.reflect.Parameter; -import java.util.*; - -@Getter -public class RegisteredCommand implements Comparable { - - private final Command command; - private final Object object; - private final Method method; - private final List nodes = new ArrayList<>(); - private final @NonNull MethodHandle methodHandle; - - public RegisteredCommand(Command command, Object object, Method method, @NonNull MethodHandle methodHandle) { - this.command = command; - this.object = object; - this.method = method; - this.methodHandle = methodHandle; - prepareNodes(); - } - - private void prepareNodes() { - String[] commandArgs = command.execution().split(" "); - - Class[] types = method.getParameterTypes(); - Parameter[] parameters = method.getParameters(); - int index = 1; //skipping the first type because of the CommandSender - for (String commandArg : commandArgs) { - if (!requiredArgs(commandArg)) { - nodes.add(new ArgsNode( - commandArg, - false, - null, - null, - false - )); - continue; - } - - Description description = parameters[index].getAnnotation(Description.class); - - ArgsReaderType argsReaderType = parameters[index].getAnnotation(ArgsReaderType.class); - if (argsReaderType == null && Argument.class.isAssignableFrom(types[index])) - throw new IllegalArgumentException("the \"" + commandArg + "\" argument requires the " + ArgsReaderType.class.getName() + " annotation"); - - ArgsReader reader; - if (argsReaderType == null) - reader = ArgsReaderRegistry.INSTANCE.getReader(types[index]); - else - reader = ArgsReaderRegistry.INSTANCE.get(argsReaderType.value()); - if (reader == null) - throw new IllegalArgumentException("no reader found for " + types[index].getName()); - - nodes.add(new ArgsNode( - trim(commandArg), - true, - reader, - description == null ? null : description.value(), - Argument.class.isAssignableFrom(types[index]) - )); - - index++; - } - } - - public String getName() { - return command.execution().split(" ")[0]; - } - - @Override - public int compareTo(@NotNull RegisteredCommand o) { - if (o.command.priority() == -1 && command.priority() == -1) { - int args = compareArgs(o); - - if (args == 0) - return compareSize(o); - - return args; - } - - return Integer.compare(o.command.priority(), command.priority()); - } - - private int compareArgs(RegisteredCommand other) { - return Integer.compare(other.command.execution().split(" ").length, command.execution().split(" ").length); - } - - private int compareSize(RegisteredCommand other) { - return Integer.compare(other.command.execution().length(), command.execution().length()); - } - - public Optional>> parse(String input, boolean autoComplete) throws ArgsReaderException { - List toParse = new ArrayList<>(); - checkArrays(input, toParse, autoComplete); - - List> result = new ArrayList<>(nodes.size()); - - if (parseRequiredOnly(result, toParse, autoComplete)) - return Optional.of(result); - - return Optional.empty(); - } - - private boolean parseRequiredOnly(List> target, List strings, boolean autoComplete) throws ArgsReaderException { - if (autoComplete && nodes.size() > strings.size()) - return false; - - for (int i = 0; i < nodes.size() && i < strings.size(); i++) { - ArgsNode node = nodes.get(i); - if (!node.isUserInput()) { - if (!node.getName().equalsIgnoreCase(strings.get(i))) - return false; - - continue; - } - - Object parse = node.parse(strings.get(i)); - if (parse == ArgsNode.EMPTY) - continue; - - target.add(new Pair<>(node, parse)); - } - - return true; - } - - private void checkArrays(String input, List target, boolean autoComplete) throws ArgsReaderException { - StringBuilder builder = new StringBuilder(); - boolean inArray = false; - for (String s : input.split(" ")) { - if (s.startsWith("\"") && s.endsWith("\"") && !s.endsWith("\\\"") && s.length() != 1) { //"string" | single word - target.add(s.substring(1, s.length() - 1)); - continue; - } - - if (s.startsWith("\"")) { //"string | it is starting the array - builder.append(s.substring(1).replace("\\\"", "\"")).append(" "); - inArray = true; - continue; - } - - if (s.endsWith("\"") && !s.endsWith("\\\"")) { //string" | it is ending the array - builder.append(s.replace("\\\"", "\""), 0, s.length() - 1); - target.add(builder.toString()); - builder = new StringBuilder(); - inArray = false; - continue; - } - - if (!builder.isEmpty()) { //string | it is in the array - builder.append(s.replace("\\\"", "\"")).append(" "); - continue; - } - - target.add(s.replace("\\\"", "\"")); //string | it is not in the array - } - - if (inArray && !autoComplete) - throw new ArgsReaderException("Invalid array"); - } - - public CommandResult execute(CommandSender sender, String string) { - try { - Optional>> parse = parse(string, true); - if (parse.isEmpty()) - return CommandResult.ARGS_FAIL; - - if (!getSenderType().isInstance(sender) && !isSenderGeneric()) - return CommandResult.INVALID_SENDER; - - Object[] objects = parse.get().stream().map(Pair::getSecond).toArray(); - return executeInternal(sender, objects); - } catch (ArgsReaderException e) { - return CommandResult.EXCEPTION; - } - } - - private CommandResult executeInternal(CommandSender sender, Object[] objects) { - try { - if (!command.permission().isEmpty() && !sender.hasPermission(command.permission())) - return CommandResult.PERMISSION_FAIL; - - List list = new ArrayList<>(Arrays.asList(objects)); - list.add(0, isSenderGeneric() ? sender.toGeneric() : sender); - - if (list.size() != method.getParameterCount()) - return CommandResult.ARGS_FAIL; - - list.add(0, object); - methodHandle.invokeWithArguments(list); - return CommandResult.SUCCESS; - } catch (Throwable e) { - e.printStackTrace(); - return CommandResult.METHOD_FAIL; - } - } - - @NonNull - public List<@NonNull String> autoComplete(@NonNull CommandSender sender, @NonNull String string) { - if (nodes.size() == 1) - return Collections.emptyList(); - - if (!getSenderType().isInstance(sender) && !isSenderGeneric()) - return Collections.emptyList(); - - try { - Optional>> parse = parse(string, false); - if (parse.isEmpty()) - return Collections.emptyList(); - - String[] split = string.split(" "); - String lastString = split[split.length - 1]; - - List> pairs = parse.get(); - - if (pairs.isEmpty()) { - ArgsNode secondNode = nodes.get(1); - ArgsReader secondNodeReader = secondNode.getReader(); - return secondNodeReader == null ? List.of(secondNode.getName()) : secondNodeReader.autoComplete(sender, lastString); - } - - Pair lastPair = pairs.get(pairs.size() - 1); - - ArgsNode node = lastPair.getFirst(); - ArgsReader reader = node.getReader(); - - return reader == null ? List.of(node.getName()) : reader.autoComplete(sender, lastString); - } catch (ArgsReaderException e) { - e.printStackTrace(); - return Collections.emptyList(); - } - } - - @Override - public String toString() { - return "RegisteredCommand{" + command.execution() + "}"; - } - - @SuppressWarnings("unchecked") - public Class> getSenderType() { - return (Class>) method.getParameterTypes()[0]; - } - - public boolean isSenderGeneric() { - boolean result = false; - try { - result = (Boolean) getSenderType().getDeclaredMethod("isGeneric").invoke(null); - } catch (NoSuchMethodException ignored) { - } catch (Exception e) { - e.printStackTrace(); - } - return result; - } - - private static String trim(String string) { - return string.substring(1, string.length() - 1); - } - - private static boolean requiredArgs(String string) { - return string.startsWith("<") && string.endsWith(">"); - } -} \ No newline at end of file diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/Command.java b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java similarity index 96% rename from commands/src/main/java/com/wizardlybump17/wlib/command/Command.java rename to commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java index ca53cf22..1009d78b 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/Command.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java @@ -1,4 +1,6 @@ -package com.wizardlybump17.wlib.command; +package com.wizardlybump17.wlib.command.annotation; + +import com.wizardlybump17.wlib.command.CommandSender; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java new file mode 100644 index 00000000..b659ea2e --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java @@ -0,0 +1,96 @@ +package com.wizardlybump17.wlib.command.data; + +import com.wizardlybump17.wlib.command.annotation.Command; +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.lang.reflect.Field; +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +public class AnnotationCommandData implements CommandData { + + private final @NotNull Command annotation; + private final @NotNull Object object; + private final @NotNull Map fieldCache = new HashMap<>(); + + public AnnotationCommandData(@NotNull Command annotation, @NotNull Object object) { + this.annotation = annotation; + this.object = object; + } + + @Override + public @NotNull String getExecution() { + return annotation.execution(); + } + + @Override + public @Nullable String getPermission() { + String permission = annotation.permission(); + return permission.isBlank() ? CommandData.super.getPermission() : permission; + } + + @Override + public @Nullable String getPermissionMessage() { + return getMessage(annotation.permissionMessage(), annotation.permissionMessageIsField(), CommandData.super.getPermissionMessage()); + } + + @Override + public int getPriority() { + int priority = annotation.priority(); + return priority == -1 ? CommandData.super.getPriority() : priority; + } + + @Override + public @Nullable String getDescription() { + String description = annotation.description(); + return description.isBlank() ? CommandData.super.getDescription() : description; + } + + @Override + public @Nullable String getInvalidSenderMessage() { + return getMessage(annotation.invalidSenderMessage(), annotation.invalidSenderMessageIsField(), CommandData.super.getInvalidSenderMessage()); + } + + protected @Nullable String getMessage(@NotNull String message, boolean isField, @Nullable String defaultMessage) { + if (isField) { + return ReflectionUtil.getFieldValue( + fieldCache.computeIfAbsent( + message, + $ -> ReflectionUtil.getField(message, object.getClass()) + ), + object + ); + } + return message.isBlank() ? defaultMessage : message; + } + + public @NotNull Command getAnnotation() { + return annotation; + } + + + @Override + public boolean equals(Object object1) { + if (object1 == null || getClass() != object1.getClass()) + return false; + AnnotationCommandData that = (AnnotationCommandData) object1; + return Objects.equals(annotation, that.annotation) && Objects.equals(object, that.object) && Objects.equals(fieldCache, that.fieldCache); + } + + @Override + public int hashCode() { + return Objects.hash(annotation, object, fieldCache); + } + + @Override + public String toString() { + return "AnnotationCommandData{" + + "annotation=" + annotation + + ", object=" + object + + ", fieldCache=" + fieldCache + + '}'; + } +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java new file mode 100644 index 00000000..9c077a5d --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -0,0 +1,66 @@ +package com.wizardlybump17.wlib.command.data; + +import com.wizardlybump17.wlib.command.CommandSender; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +public interface CommandData { + + /** + *

How the sender must type the command in order for it to be triggered. + * Example:

command hello world
+ * In the example above, the sender must type /command hello world, so it can be triggered. + * To add parameters that depends on the sender input, just put the parameter between <>. + *
+ * Example:
command <hello> world

+ *

The type of each parameter is defined in the method that have this annotation. + * An example for the command above: + *

+     *  @Command(execution = "command <hello> world")
+     *  public void commandHelloWorld(GenericSender sender, String hello) {
+     *      System.out.println(sender.getName() + " executed the hello world command with the argument " + hello);
+     *  }
+     * 

+ * + * @return how the command must be sent to be triggered + * @see com.wizardlybump17.wlib.command.args.reader.ArgsReader + */ + @NotNull String getExecution(); + + /** + * @return which permission the sender must have to trigger this command + */ + default @Nullable String getPermission() { + return null; + } + + /** + *

Used when the {@link CommandSender} does not have the required {@link #getPermission()}.

+ * @return the message to be sent when the {@link CommandSender} does not have the required {@link #getPermission()} + */ + default @Nullable String getPermissionMessage() { + return null; + } + + /** + * @return the priority of this command + */ + default int getPriority() { + return getExecution().split(" ").length; + } + + /** + * @return the description of this command + */ + default @Nullable String getDescription() { + return null; + } + + /** + *

Used when the {@link CommandSender} is not valid for this command.

+ * @return the message to be sent when the {@link CommandSender} is not valid for this command + */ + default @Nullable String getInvalidSenderMessage() { + return null; + } +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/exception/CommandException.java b/commands/src/main/java/com/wizardlybump17/wlib/command/exception/CommandException.java new file mode 100644 index 00000000..7c99b73f --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/exception/CommandException.java @@ -0,0 +1,21 @@ +package com.wizardlybump17.wlib.command.exception; + +import org.jetbrains.annotations.NotNull; + +public class CommandException extends Exception { + + public CommandException() { + } + + public CommandException(@NotNull String message) { + super(message); + } + + public CommandException(@NotNull String message, @NotNull Throwable cause) { + super(message, cause); + } + + public CommandException(@NotNull Throwable cause) { + super(cause); + } +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java new file mode 100644 index 00000000..298f73b7 --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java @@ -0,0 +1,12 @@ +package com.wizardlybump17.wlib.command.executor; + +import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.exception.CommandException; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public interface CommandExecutor { + + void execute(@NotNull CommandSender sender, @NotNull Map args) throws CommandException; +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/CommandExtractor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/CommandExtractor.java new file mode 100644 index 00000000..8f1d4373 --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/CommandExtractor.java @@ -0,0 +1,13 @@ +package com.wizardlybump17.wlib.command.extractor; + +import com.wizardlybump17.wlib.command.CommandManager; +import com.wizardlybump17.wlib.command.holder.CommandHolder; +import com.wizardlybump17.wlib.command.registered.RegisteredCommand; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public interface CommandExtractor { + + @NotNull List extract(@NotNull CommandManager manager, @NotNull CommandHolder holder, @NotNull Object object); +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/DirectCommandExtractor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/DirectCommandExtractor.java new file mode 100644 index 00000000..bb21f6c5 --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/DirectCommandExtractor.java @@ -0,0 +1,16 @@ +package com.wizardlybump17.wlib.command.extractor; + +import com.wizardlybump17.wlib.command.CommandManager; +import com.wizardlybump17.wlib.command.holder.CommandHolder; +import com.wizardlybump17.wlib.command.registered.RegisteredCommand; +import org.jetbrains.annotations.NotNull; + +import java.util.List; + +public class DirectCommandExtractor implements CommandExtractor { + + @Override + public @NotNull List extract(@NotNull CommandManager manager, @NotNull CommandHolder holder, @NotNull Object object) { + return object instanceof RegisteredCommand command ? List.of(command) : List.of(); + } +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java new file mode 100644 index 00000000..3e650e68 --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java @@ -0,0 +1,44 @@ +package com.wizardlybump17.wlib.command.extractor; + +import com.wizardlybump17.wlib.command.CommandManager; +import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.annotation.Command; +import com.wizardlybump17.wlib.command.holder.CommandHolder; +import com.wizardlybump17.wlib.command.registered.RegisteredCommand; +import com.wizardlybump17.wlib.command.registered.RegisteredMethodCommand; +import org.jetbrains.annotations.NotNull; + +import java.lang.reflect.Method; +import java.util.ArrayList; +import java.util.List; +import java.util.logging.Level; + +public class MethodCommandExtractor implements CommandExtractor { + + @Override + public @NotNull List extract(@NotNull CommandManager manager, @NotNull CommandHolder holder, @NotNull Object object) { + List commands = new ArrayList<>(); + for (Method method : object.getClass().getDeclaredMethods()) { + if (!method.isAnnotationPresent(Command.class) || method.getParameterCount() == 0 || !CommandSender.class.isAssignableFrom(method.getParameterTypes()[0])) + continue; + + RegisteredMethodCommand command; + try { + command = new RegisteredMethodCommand( + method.getAnnotation(Command.class), + object, + method + ); + } catch (Exception e) { + holder.getLogger().log(Level.SEVERE, "Error while creating a command", e); + continue; + } + + commands.add(command); + com.wizardlybump17.wlib.command.holder.Command holderCommand = holder.getCommand(command.getName()); + if (holderCommand != null) + holderCommand.setExecutor(holderCommand.getDefaultExecutor(manager, command.getName())); + } + return commands; + } +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java index de8e1475..24bf890b 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java @@ -2,11 +2,12 @@ import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; +import com.wizardlybump17.wlib.command.exception.CommandException; /** * Interface for intercepting commands when they are called */ public interface CommandExecutor { - void execute(CommandSender sender, String commandName, String[] args) throws ArgsReaderException; + void execute(CommandSender sender, String commandName, String[] args) throws ArgsReaderException, CommandException; } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java new file mode 100644 index 00000000..3106237f --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -0,0 +1,131 @@ +package com.wizardlybump17.wlib.command.registered; + +import com.wizardlybump17.wlib.command.CommandResult; +import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.args.ArgsNode; +import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; +import com.wizardlybump17.wlib.command.data.CommandData; +import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.executor.CommandExecutor; +import com.wizardlybump17.wlib.util.StringUtil; +import org.jetbrains.annotations.NotNull; + +import java.util.LinkedHashMap; +import java.util.List; +import java.util.Optional; + +public class RegisteredCommand implements Comparable { + + private final @NotNull CommandData command; + private final @NotNull List nodes; + private final @NotNull CommandExecutor executor; + + public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor) { + this.command = command; + this.nodes = nodes; + this.executor = executor; + } + + public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes) { + this.command = command; + this.nodes = nodes; + executor = createExecutor(); + } + + protected CommandExecutor createExecutor() { + throw new UnsupportedOperationException(); + } + + public String getName() { + return command.getExecution().split(" ")[0]; + } + + @Override + public int compareTo(@NotNull RegisteredCommand o) { + return Integer.compare(o.command.getPriority(), command.getPriority()); + } + + public Optional> parse(String input) throws ArgsReaderException { + List toParse = StringUtil.parseQuotedStrings(input); + + LinkedHashMap result = new LinkedHashMap<>(); + + if (checkNodes(result, toParse)) + return Optional.of(result); + + return Optional.empty(); + } + + protected boolean checkNodes(LinkedHashMap target, List strings) throws ArgsReaderException { + if (nodes.size() > strings.size()) + return false; + + for (int i = 0; i < nodes.size() && i < strings.size(); i++) { + ArgsNode node = nodes.get(i); + if (!node.isUserInput()) { + if (node.getName().equalsIgnoreCase(strings.get(i))) + continue; + return false; + } + + Object parse = node.parse(strings.get(i)); + if (parse != ArgsNode.EMPTY) + target.put(node.getName(), parse); + } + + return true; + } + + public @NotNull CommandResult execute(@NotNull CommandSender sender, @NotNull String string) throws CommandException { + if (!canExecute(sender)) + return CommandResult.INVALID_SENDER; + + String permission = command.getPermission(); + if (permission != null && !sender.hasPermission(permission)) + return CommandResult.PERMISSION_FAIL; + + try { + Optional> parse = parse(string); + if (parse.isEmpty()) + return CommandResult.ARGS_FAIL; + + LinkedHashMap args = parse.get(); + if (!isValidArgs(args)) + return CommandResult.ARGS_FAIL; + + executor.execute(sender, args); + return CommandResult.SUCCESS; + } catch (ArgsReaderException e) { + return CommandResult.EXCEPTION; + } + } + + protected boolean isValidArgs(@NotNull LinkedHashMap args) { + return true; + } + + public boolean canExecute(@NotNull CommandSender sender) { + return true; + } + + @Override + public String toString() { + return "RegisteredCommand{" + command.getExecution() + "}"; + } + + public @NotNull CommandData getCommand() { + return command; + } + + public @NotNull CommandExecutor getExecutor() { + return executor; + } + + public @NotNull List getNodes() { + return nodes; + } + + public boolean isOwnedBy(@NotNull Object object) { + return false; + } +} \ No newline at end of file diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java new file mode 100644 index 00000000..afa08270 --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java @@ -0,0 +1,122 @@ +package com.wizardlybump17.wlib.command.registered; + +import com.wizardlybump17.wlib.command.Argument; +import com.wizardlybump17.wlib.command.Description; +import com.wizardlybump17.wlib.command.annotation.Command; +import com.wizardlybump17.wlib.command.args.ArgsNode; +import com.wizardlybump17.wlib.command.args.ArgsReaderRegistry; +import com.wizardlybump17.wlib.command.args.ArgsReaderType; +import com.wizardlybump17.wlib.command.args.reader.ArgsReader; +import com.wizardlybump17.wlib.command.data.AnnotationCommandData; +import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.executor.CommandExecutor; +import lombok.Getter; +import org.jetbrains.annotations.NotNull; + +import java.lang.invoke.MethodHandle; +import java.lang.invoke.MethodHandles; +import java.lang.invoke.MethodType; +import java.lang.reflect.Method; +import java.lang.reflect.Parameter; +import java.util.ArrayList; +import java.util.LinkedHashMap; +import java.util.List; + +@Getter +public class RegisteredMethodCommand extends RegisteredCommand { + + private final @NotNull Command annotation; + private final @NotNull Object object; + private final @NotNull Method method; + private final @NotNull MethodHandle methodHandle; + + public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object object, @NotNull Method method) throws NoSuchMethodException, IllegalAccessException { + super( + new AnnotationCommandData( + annotation, + object + ), + new ArrayList<>() + ); + this.annotation = annotation; + this.object = object; + this.method = method; + methodHandle = MethodHandles.lookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); + prepareNodes(); + } + + @Override + protected CommandExecutor createExecutor() { + return (sender, args) -> { + List objects = new ArrayList<>(args.values()); + objects.add(0, sender); + try { + methodHandle.invokeWithArguments(objects); + } catch (Throwable e) { + throw new CommandException("Error while executing the command " + getCommand().getExecution() + " with the arguments " + args, e); + } + }; + } + + protected void prepareNodes() { + String[] commandArgs = getCommand().getExecution().split(" "); + + Class[] types = method.getParameterTypes(); + Parameter[] parameters = method.getParameters(); + int index = 1; //skipping the first type because of the CommandSender + for (String commandArg : commandArgs) { + if (!isRequiredArgs(commandArg)) { + getNodes().add(new ArgsNode( + commandArg, + false, + null, + null, + false + )); + continue; + } + + Description description = parameters[index].getAnnotation(Description.class); + + ArgsReaderType argsReaderType = parameters[index].getAnnotation(ArgsReaderType.class); + if (argsReaderType == null && Argument.class.isAssignableFrom(types[index])) + throw new IllegalArgumentException("the \"" + commandArg + "\" argument requires the " + ArgsReaderType.class.getName() + " annotation"); + + ArgsReader reader; + if (argsReaderType == null) + reader = ArgsReaderRegistry.INSTANCE.getReader(types[index]); + else + reader = ArgsReaderRegistry.INSTANCE.get(argsReaderType.value()); + if (reader == null) + throw new IllegalArgumentException("no reader found for " + types[index].getName()); + + getNodes().add(new ArgsNode( + trim(commandArg), + true, + reader, + description == null ? null : description.value(), + Argument.class.isAssignableFrom(types[index]) + )); + + index++; + } + } + + @Override + protected boolean isValidArgs(@NotNull LinkedHashMap args) { + return args.size() + 1 == method.getParameterCount(); + } + + @Override + public boolean isOwnedBy(@NotNull Object object) { + return this.object == object; + } + + private static String trim(String string) { + return string.substring(1, string.length() - 1); + } + + private static boolean isRequiredArgs(String string) { + return string.startsWith("<") && string.endsWith(">"); + } +} \ No newline at end of file diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java b/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java index c5577b03..c17fb0b0 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java @@ -1,54 +1,39 @@ package com.wizardlybump17.wlib.command; -import com.wizardlybump17.wlib.command.sender.ConsoleSender; -import com.wizardlybump17.wlib.command.sender.GenericSender; -import com.wizardlybump17.wlib.command.sender.PlayerSender; +import com.wizardlybump17.wlib.command.exception.CommandException; import lombok.RequiredArgsConstructor; -import lombok.SneakyThrows; import org.bukkit.command.Command; -import org.bukkit.command.CommandSender; -import org.bukkit.command.*; -import org.bukkit.entity.Player; +import org.bukkit.command.TabExecutor; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.List; +import java.util.logging.Level; @RequiredArgsConstructor public class BukkitCommandExecutor implements TabExecutor, com.wizardlybump17.wlib.command.holder.CommandExecutor { private final CommandManager manager; - @SneakyThrows @Override - public boolean onCommand(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { - execute(getSender(sender), command.getName(), args); + public boolean onCommand(@NotNull org.bukkit.command.CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { + try { + execute(new com.wizardlybump17.wlib.command.sender.CommandSender(sender), command.getName(), args); + } catch (CommandException e) { + manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command", e); + } return false; } @Override - public void execute(com.wizardlybump17.wlib.command.CommandSender sender, String commandName, String[] args) { + public void execute(com.wizardlybump17.wlib.command.CommandSender sender, String commandName, String[] args) throws CommandException { String commandExecution = commandName + " " + String.join(" ", args); manager.execute(sender, commandExecution); } @Nullable @Override - public List onTabComplete(@NotNull CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { -// String fullCommand = command.getName() + " " + String.join(" ", args); -// List suggestions = manager.autoComplete(getSender(sender), fullCommand); -// ArrayList strings = StringUtil.copyPartialMatches(args[args.length - 1], suggestions, new ArrayList<>(suggestions.size())); -// return strings; + public List onTabComplete(@NotNull org.bukkit.command.CommandSender sender, @NotNull Command command, @NotNull String label, @NotNull String[] args) { return null; } - - private com.wizardlybump17.wlib.command.CommandSender getSender(CommandSender original) { - if (original instanceof Player player) - return new PlayerSender(player); - if (original instanceof ConsoleCommandSender consoleSender) - return new ConsoleSender(consoleSender); - if (original instanceof BlockCommandSender blockSender) - return new com.wizardlybump17.wlib.command.sender.BlockCommandSender(blockSender); - return new GenericSender(original); - } } diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/AbstractSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/AbstractSender.java deleted file mode 100644 index 98af7103..00000000 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/AbstractSender.java +++ /dev/null @@ -1,39 +0,0 @@ -package com.wizardlybump17.wlib.command.sender; - -import com.wizardlybump17.wlib.command.CommandSender; -import lombok.Getter; -import lombok.NonNull; -import lombok.RequiredArgsConstructor; - -@RequiredArgsConstructor -@Getter -public abstract class AbstractSender implements CommandSender { - - private final S handle; - - @Override - public void sendMessage(String message) { - handle.sendMessage(message); - } - - @Override - public void sendMessage(String... message) { - handle.sendMessage(String.join("\n", message)); - } - - @Override - public String getName() { - return handle.getName(); - } - - @Override - public boolean hasPermission(String permission) { - return handle.hasPermission(permission); - } - - @Override - @NonNull - public GenericSender toGeneric() { - return new GenericSender(handle); - } -} diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/BlockCommandSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BlockCommandSender.java deleted file mode 100644 index 2f3b8455..00000000 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/BlockCommandSender.java +++ /dev/null @@ -1,8 +0,0 @@ -package com.wizardlybump17.wlib.command.sender; - -public class BlockCommandSender extends AbstractSender { - - public BlockCommandSender(org.bukkit.command.BlockCommandSender handle) { - super(handle); - } -} diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java new file mode 100644 index 00000000..db06a313 --- /dev/null +++ b/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java @@ -0,0 +1,52 @@ +package com.wizardlybump17.wlib.command.sender; + +import org.bukkit.command.BlockCommandSender; +import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Player; +import org.jetbrains.annotations.NotNull; + +public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender { + + private final @NotNull org.bukkit.command.CommandSender handle; + + public CommandSender(@NotNull org.bukkit.command.CommandSender handle) { + this.handle = handle; + } + + @Override + public org.bukkit.command.CommandSender getHandle() { + return handle; + } + + @Override + public void sendMessage(String message) { + handle.sendMessage(message); + } + + @Override + public void sendMessage(String... message) { + handle.sendMessage(String.join("\n", message)); + } + + @Override + public String getName() { + return handle.getName(); + } + + @Override + public boolean hasPermission(String permission) { + return handle.hasPermission(permission); + } + + public @NotNull BlockCommandSender asBlockCommand() { + return (BlockCommandSender) handle; + } + + public @NotNull ConsoleCommandSender asConsole() { + return (ConsoleCommandSender) handle; + } + + public @NotNull Player asPlayer() { + return (Player) handle; + } +} diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/ConsoleSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/ConsoleSender.java deleted file mode 100644 index 46e55e01..00000000 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/ConsoleSender.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.wizardlybump17.wlib.command.sender; - -import org.bukkit.command.ConsoleCommandSender; - -public class ConsoleSender extends AbstractSender { - - public ConsoleSender(ConsoleCommandSender handle) { - super(handle); - } -} diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/GenericSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/GenericSender.java deleted file mode 100644 index d223526d..00000000 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/GenericSender.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.wizardlybump17.wlib.command.sender; - -import lombok.NonNull; -import org.bukkit.command.CommandSender; - -public class GenericSender extends AbstractSender { - - public GenericSender(CommandSender handle) { - super(handle); - } - - @Override - @NonNull - public GenericSender toGeneric() { - return this; - } - - public static boolean isGeneric() { - return true; - } -} diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/PlayerSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/PlayerSender.java deleted file mode 100644 index ac2f08eb..00000000 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/PlayerSender.java +++ /dev/null @@ -1,10 +0,0 @@ -package com.wizardlybump17.wlib.command.sender; - -import org.bukkit.entity.Player; - -public class PlayerSender extends AbstractSender { - - public PlayerSender(Player handle) { - super(handle); - } -} From b0181a6880d63241918a8ce63cac9cd53efb7a41 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Tue, 12 Nov 2024 23:48:58 -0300 Subject: [PATCH 02/47] registering the command extractors --- .../wizardlybump17/wlib/command/CommandManager.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 957ee572..6f8509a9 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -3,18 +3,18 @@ import com.wizardlybump17.wlib.command.data.CommandData; import com.wizardlybump17.wlib.command.exception.CommandException; import com.wizardlybump17.wlib.command.extractor.CommandExtractor; +import com.wizardlybump17.wlib.command.extractor.DirectCommandExtractor; +import com.wizardlybump17.wlib.command.extractor.MethodCommandExtractor; import com.wizardlybump17.wlib.command.holder.CommandHolder; import com.wizardlybump17.wlib.command.registered.RegisteredCommand; import lombok.Getter; import lombok.NonNull; -import lombok.RequiredArgsConstructor; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Field; import java.util.*; @Getter -@RequiredArgsConstructor public class CommandManager { private final List commands = new ArrayList<>(); @@ -22,6 +22,12 @@ public class CommandManager { private final @NonNull Map, Map> fieldCache = new HashMap<>(); private final @NotNull Set commandExtractors = new HashSet<>(); + public CommandManager(@NotNull CommandHolder holder) { + this.holder = holder; + commandExtractors.add(new MethodCommandExtractor()); + commandExtractors.add(new DirectCommandExtractor()); + } + public void registerCommands(Object... objects) { for (Object object : objects) for (CommandExtractor extractor : commandExtractors) From 45288435cb5a3e98acc4e862eae635e987f573b4 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Tue, 12 Nov 2024 23:49:07 -0300 Subject: [PATCH 03/47] fixed the arguments list --- .../wlib/command/registered/RegisteredMethodCommand.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java index afa08270..5641650b 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java @@ -49,7 +49,8 @@ public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object obje protected CommandExecutor createExecutor() { return (sender, args) -> { List objects = new ArrayList<>(args.values()); - objects.add(0, sender); + objects.add(0, object); + objects.add(1, sender); try { methodHandle.invokeWithArguments(objects); } catch (Throwable e) { From 272137f2284e8876a953bf6f09a2992e38075159 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 16:46:56 -0300 Subject: [PATCH 04/47] weakened the constructor's visibility --- .../wlib/command/registered/RegisteredCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index 3106237f..d204d162 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -26,7 +26,7 @@ public RegisteredCommand(@NotNull CommandData command, @NotNull List n this.executor = executor; } - public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes) { + protected RegisteredCommand(@NotNull CommandData command, @NotNull List nodes) { this.command = command; this.nodes = nodes; executor = createExecutor(); From e22e0131bc546f478a6f5d75b17a4ad8650aa0e4 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 17:06:36 -0300 Subject: [PATCH 05/47] added the CommandManager#registerCommands(Collection) method --- .../java/com/wizardlybump17/wlib/command/CommandManager.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 6f8509a9..feb35181 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -35,6 +35,11 @@ public void registerCommands(Object... objects) { commands.sort(null); } + public void registerCommands(@NotNull Collection commands) { + this.commands.addAll(commands); + this.commands.sort(null); + } + public void unregisterCommands() { commands.clear(); } From f58b63cf8bcd39f2a32c3277179b8efa841d1749 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 17:34:01 -0300 Subject: [PATCH 06/47] added the CommandData#getName() method --- .../com/wizardlybump17/wlib/command/data/CommandData.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java index 9c077a5d..bf0c98e2 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -63,4 +63,8 @@ default int getPriority() { default @Nullable String getInvalidSenderMessage() { return null; } + + default @NotNull String getName() { + return getExecution().split(" ")[0]; + } } From 8046e8f79d193e95bf9ff1ba9f973f2366a4a443 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 17:34:29 -0300 Subject: [PATCH 07/47] added the RegisteredCommand#onRegister(CommandManager) method --- .../wizardlybump17/wlib/command/CommandManager.java | 11 ++++++++--- .../wlib/command/registered/RegisteredCommand.java | 4 ++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index feb35181..6c4146f7 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -29,13 +29,18 @@ public CommandManager(@NotNull CommandHolder holder) { } public void registerCommands(Object... objects) { - for (Object object : objects) - for (CommandExtractor extractor : commandExtractors) - commands.addAll(extractor.extract(this, holder, object)); + for (Object object : objects) { + for (CommandExtractor extractor : commandExtractors) { + List commands = extractor.extract(this, holder, object); + commands.forEach(command -> command.onRegister(this)); + this.commands.addAll(commands); + } + } commands.sort(null); } public void registerCommands(@NotNull Collection commands) { + commands.forEach(command -> command.onRegister(this)); this.commands.addAll(commands); this.commands.sort(null); } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index d204d162..26e7d3f7 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -1,5 +1,6 @@ package com.wizardlybump17.wlib.command.registered; +import com.wizardlybump17.wlib.command.CommandManager; import com.wizardlybump17.wlib.command.CommandResult; import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.ArgsNode; @@ -128,4 +129,7 @@ public String toString() { public boolean isOwnedBy(@NotNull Object object) { return false; } + + public void onRegister(@NotNull CommandManager manager) { + } } \ No newline at end of file From 3f0866b5b5f2481b3714bf3a5937038ae7a2a789 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 17:34:39 -0300 Subject: [PATCH 08/47] added the RegisteredPluginCommand class --- .../registered/RegisteredPluginCommand.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java new file mode 100644 index 00000000..f1a6fd8e --- /dev/null +++ b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java @@ -0,0 +1,44 @@ +package com.wizardlybump17.wlib.command.registered; + +import com.wizardlybump17.wlib.command.CommandManager; +import com.wizardlybump17.wlib.command.args.ArgsNode; +import com.wizardlybump17.wlib.command.data.CommandData; +import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.executor.CommandExecutor; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.CommandSender; +import org.jetbrains.annotations.NotNull; + +import java.util.List; +import java.util.logging.Level; + +public class RegisteredPluginCommand extends RegisteredCommand { + + private final @NotNull String fallback; + + public RegisteredPluginCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor, @NotNull String fallback) { + super(command, nodes, executor); + this.fallback = fallback; + } + + public @NotNull String getFallback() { + return fallback; + } + + @Override + public void onRegister(@NotNull CommandManager manager) { + CommandMap commandMap = null; + commandMap.register(getCommand().getName(), fallback, new Command(getCommand().getName()) { + @Override + public boolean execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) { + try { + RegisteredPluginCommand.this.execute(new com.wizardlybump17.wlib.command.sender.CommandSender(sender), String.join(" ", args)); + } catch (CommandException e) { + manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command.", e); + } + return false; + } + }); + } +} From 5b24ac621fab46bcba79c9008bdf5ffee2f390d3 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:00:16 -0300 Subject: [PATCH 09/47] added the CommandMapAdapter system --- .../adapter/command/CommandMapAdapter.java | 28 ++++++++++++++++ .../v1_16_R3/command/CommandMapAdapter.java | 33 +++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 versions/adapter/src/main/java/com/wizardlybump17/wlib/adapter/command/CommandMapAdapter.java create mode 100644 versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java diff --git a/versions/adapter/src/main/java/com/wizardlybump17/wlib/adapter/command/CommandMapAdapter.java b/versions/adapter/src/main/java/com/wizardlybump17/wlib/adapter/command/CommandMapAdapter.java new file mode 100644 index 00000000..14a001ae --- /dev/null +++ b/versions/adapter/src/main/java/com/wizardlybump17/wlib/adapter/command/CommandMapAdapter.java @@ -0,0 +1,28 @@ +package com.wizardlybump17.wlib.adapter.command; + +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public abstract class CommandMapAdapter { + + private static CommandMapAdapter instance; + + public abstract @NotNull CommandMap getCommandMap(); + + public abstract void unregisterCommand(@NotNull String command); + + public abstract @NotNull Map getCommands(); + + public static CommandMapAdapter getInstance() { + return instance; + } + + public static void setInstance(@NotNull CommandMapAdapter instance) { + if (CommandMapAdapter.instance != null) + throw new IllegalStateException("The CommandAdapter instance is already set."); + CommandMapAdapter.instance = instance; + } +} diff --git a/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java b/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java new file mode 100644 index 00000000..b9e7ea3f --- /dev/null +++ b/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_16_R3.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} From 88b3209b231610d6d331c9dd3778fc4b26e13eff Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:00:42 -0300 Subject: [PATCH 10/47] added the RegisteredCommand#onUnregister(CommandManager) method --- .../wlib/command/registered/RegisteredCommand.java | 3 +++ .../wlib/command/registered/RegisteredPluginCommand.java | 8 +++++++- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index 26e7d3f7..03a6faac 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -132,4 +132,7 @@ public boolean isOwnedBy(@NotNull Object object) { public void onRegister(@NotNull CommandManager manager) { } + + public void onUnregister(@NotNull CommandManager manager) { + } } \ No newline at end of file diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java index f1a6fd8e..a5e79ff1 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java @@ -1,5 +1,6 @@ package com.wizardlybump17.wlib.command.registered; +import com.wizardlybump17.wlib.adapter.command.CommandMapAdapter; import com.wizardlybump17.wlib.command.CommandManager; import com.wizardlybump17.wlib.command.args.ArgsNode; import com.wizardlybump17.wlib.command.data.CommandData; @@ -28,7 +29,7 @@ public RegisteredPluginCommand(@NotNull CommandData command, @NotNull List Date: Wed, 13 Nov 2024 22:05:59 -0300 Subject: [PATCH 11/47] added the CommandMapAdapter for the other versions --- .../java/com/wizardlybump17/wlib/WLib.java | 11 +++++++ .../v1_17_R1/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_18_R1/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_18_R2/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_19_R1/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_19_R2/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_19_R3/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_20_R1/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_20_R2/command/CommandMapAdapter.java | 33 +++++++++++++++++++ .../v1_20_R3/command/CommandMapAdapter.java | 33 +++++++++++++++++++ 10 files changed, 308 insertions(+) create mode 100644 versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java create mode 100644 versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java create mode 100644 versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java create mode 100644 versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java create mode 100644 versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java create mode 100644 versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java create mode 100644 versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java create mode 100644 versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java create mode 100644 versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java diff --git a/core/src/main/java/com/wizardlybump17/wlib/WLib.java b/core/src/main/java/com/wizardlybump17/wlib/WLib.java index 5360dca2..e4905f1f 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/WLib.java +++ b/core/src/main/java/com/wizardlybump17/wlib/WLib.java @@ -3,6 +3,7 @@ import com.wizardlybump17.wlib.adapter.EnchantmentAdapter; import com.wizardlybump17.wlib.adapter.ItemAdapter; import com.wizardlybump17.wlib.adapter.PotionEffectTypeAdapter; +import com.wizardlybump17.wlib.adapter.command.CommandMapAdapter; import com.wizardlybump17.wlib.adapter.v1_19_R2.player.PlayerAdapter; import com.wizardlybump17.wlib.command.args.ArgsReaderRegistry; import com.wizardlybump17.wlib.command.reader.*; @@ -121,60 +122,70 @@ private void setupAdapters() { PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_16_R3.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_16_R3.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_16_R3.command.CommandMapAdapter()); } case V1_17_1 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_17_R1.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_17_R1.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_17_R1.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_17_R1.command.CommandMapAdapter()); } case V1_18 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R1.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R1.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R1.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R1.command.CommandMapAdapter()); } case V1_18_2 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R2.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R2.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R2.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_18_R2.command.CommandMapAdapter()); } case V1_19 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R1.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R1.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R1.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R1.command.CommandMapAdapter()); } case V1_19_3 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R2.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R2.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R2.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R2.command.CommandMapAdapter()); } case V1_19_4 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R3.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R3.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R3.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_19_R3.command.CommandMapAdapter()); } case V1_20_1 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R1.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R1.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R1.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R1.command.CommandMapAdapter()); } case V1_20_2 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R2.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R2.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R2.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R2.command.CommandMapAdapter()); } case V1_20_4 -> { ItemAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R3.ItemAdapter()); PlayerAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R3.player.PlayerAdapter()); PotionEffectTypeAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R3.PotionEffectTypeAdapter()); EnchantmentAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R3.EnchantmentAdapter()); + CommandMapAdapter.setInstance(new com.wizardlybump17.wlib.adapter.v1_20_R3.command.CommandMapAdapter()); } } diff --git a/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java b/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java new file mode 100644 index 00000000..6faf8ecc --- /dev/null +++ b/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_17_R1.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java b/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java new file mode 100644 index 00000000..9b01a952 --- /dev/null +++ b/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_18_R1.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java b/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java new file mode 100644 index 00000000..9302abad --- /dev/null +++ b/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_18_R2.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java b/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java new file mode 100644 index 00000000..6866c760 --- /dev/null +++ b/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_19_R1.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java b/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java new file mode 100644 index 00000000..e48d139b --- /dev/null +++ b/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_19_R2.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java b/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java new file mode 100644 index 00000000..d9a8b9b4 --- /dev/null +++ b/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_19_R3.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java b/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java new file mode 100644 index 00000000..0eb5914f --- /dev/null +++ b/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_20_R1.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java b/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java new file mode 100644 index 00000000..df1da3a4 --- /dev/null +++ b/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_20_R2.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} diff --git a/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java b/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java new file mode 100644 index 00000000..6aedfc2b --- /dev/null +++ b/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java @@ -0,0 +1,33 @@ +package com.wizardlybump17.wlib.adapter.v1_20_R3.command; + +import com.wizardlybump17.wlib.util.ReflectionUtil; +import org.bukkit.Bukkit; +import org.bukkit.command.Command; +import org.bukkit.command.CommandMap; +import org.bukkit.command.SimpleCommandMap; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { + + public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); + public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + + @Override + public @NotNull CommandMap getCommandMap() { + return COMMAND_MAP; + } + + @Override + public void unregisterCommand(@NotNull String command) { + Command removed = COMMANDS.remove(command); + if (removed != null) + removed.unregister(COMMAND_MAP); + } + + @Override + public @NotNull Map getCommands() { + return COMMANDS; + } +} From c60e2ec2a0996a4a4c9f7bb6511d9c68a8c8f0fa Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:07:22 -0300 Subject: [PATCH 12/47] renamed to RegisteredBukkitCommand --- ...teredPluginCommand.java => RegisteredBukkitCommand.java} | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) rename core/src/main/java/com/wizardlybump17/wlib/command/registered/{RegisteredPluginCommand.java => RegisteredBukkitCommand.java} (90%) diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java similarity index 90% rename from core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java rename to core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java index a5e79ff1..e1c55caa 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredPluginCommand.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java @@ -14,11 +14,11 @@ import java.util.List; import java.util.logging.Level; -public class RegisteredPluginCommand extends RegisteredCommand { +public class RegisteredBukkitCommand extends RegisteredCommand { private final @NotNull String fallback; - public RegisteredPluginCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor, @NotNull String fallback) { + public RegisteredBukkitCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor, @NotNull String fallback) { super(command, nodes, executor); this.fallback = fallback; } @@ -34,7 +34,7 @@ public void onRegister(@NotNull CommandManager manager) { @Override public boolean execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) { try { - RegisteredPluginCommand.this.execute(new com.wizardlybump17.wlib.command.sender.CommandSender(sender), String.join(" ", args)); + RegisteredBukkitCommand.this.execute(new com.wizardlybump17.wlib.command.sender.CommandSender(sender), String.join(" ", args)); } catch (CommandException e) { manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command.", e); } From c81482e043d6cf117faa689b03f9d6b6f5a8444a Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:12:25 -0300 Subject: [PATCH 13/47] added the ArgsNode.literal(String) method --- .../java/com/wizardlybump17/wlib/command/args/ArgsNode.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java index afb04352..f0149eb2 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java @@ -42,4 +42,8 @@ public Object parse(String input) throws ArgsReaderException { return object; } + + public static @NotNull ArgsNode literal(@NotNull String string) { + return new ArgsNode(string, false, null, null, false); + } } From 0afb70e7dbf6bf25ee6be63604cecd48c371b99f Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:25:06 -0300 Subject: [PATCH 14/47] calling the RegisteredCommand#onUnregister(CommandManager) method when unregistering the commands --- .../java/com/wizardlybump17/wlib/command/CommandManager.java | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 6c4146f7..1a1888f2 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -46,6 +46,7 @@ public void registerCommands(@NotNull Collection commands) { } public void unregisterCommands() { + commands.forEach(command -> command.onUnregister(this)); commands.clear(); } From b4cdfc155feeb18f9c854d1ae80559ad87b0553c Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:48:08 -0300 Subject: [PATCH 15/47] using the public lookup --- .../wlib/command/registered/RegisteredMethodCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java index 5641650b..721860f0 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java @@ -41,7 +41,7 @@ public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object obje this.annotation = annotation; this.object = object; this.method = method; - methodHandle = MethodHandles.lookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); + methodHandle = MethodHandles.publicLookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); prepareNodes(); } From f0749457e8b69680df0936c707b008360cfe8420 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 22:56:12 -0300 Subject: [PATCH 16/47] unregistering the command properly --- .../wlib/command/registered/RegisteredBukkitCommand.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java index e1c55caa..b303c099 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java @@ -45,6 +45,10 @@ public boolean execute(@NotNull CommandSender sender, @NotNull String label, @No @Override public void onUnregister(@NotNull CommandManager manager) { - CommandMapAdapter.getInstance().unregisterCommand(fallback + ":" + getCommand().getName()); + CommandMapAdapter adapter = CommandMapAdapter.getInstance(); + + String name = getCommand().getName(); + adapter.unregisterCommand(name); + adapter.unregisterCommand(fallback + ":" + name); } } From a6f8dcc8b6e689463e77f6cbcb3e05734d1d4810 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 13 Nov 2024 23:07:10 -0300 Subject: [PATCH 17/47] using the fields instead of their values --- .../v1_16_R3/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_17_R1/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_18_R1/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_18_R2/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_19_R1/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_19_R2/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_19_R3/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_20_R1/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_20_R2/command/CommandMapAdapter.java | 16 ++++++++++------ .../v1_20_R3/command/CommandMapAdapter.java | 16 ++++++++++------ 10 files changed, 100 insertions(+), 60 deletions(-) diff --git a/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java b/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java index b9e7ea3f..f2461fd8 100644 --- a/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java +++ b/versions/v1_16_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_16_R3/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java b/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java index 6faf8ecc..3e6d948d 100644 --- a/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java +++ b/versions/v1_17_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_17_R1/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java b/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java index 9b01a952..aa747170 100644 --- a/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java +++ b/versions/v1_18_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R1/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java b/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java index 9302abad..cdb45016 100644 --- a/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java +++ b/versions/v1_18_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_18_R2/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java b/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java index 6866c760..a5ce6522 100644 --- a/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java +++ b/versions/v1_19_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R1/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java b/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java index e48d139b..4b7b0639 100644 --- a/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java +++ b/versions/v1_19_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R2/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java b/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java index d9a8b9b4..e0b6df28 100644 --- a/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java +++ b/versions/v1_19_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_19_R3/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java b/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java index 0eb5914f..98a07dd8 100644 --- a/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java +++ b/versions/v1_20_R1/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R1/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java b/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java index df1da3a4..b14f309f 100644 --- a/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java +++ b/versions/v1_20_R2/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R2/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } diff --git a/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java b/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java index 6aedfc2b..2c867d02 100644 --- a/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java +++ b/versions/v1_20_R3/src/main/java/com/wizardlybump17/wlib/adapter/v1_20_R3/command/CommandMapAdapter.java @@ -7,27 +7,31 @@ import org.bukkit.command.SimpleCommandMap; import org.jetbrains.annotations.NotNull; +import java.lang.reflect.Field; import java.util.Map; public class CommandMapAdapter extends com.wizardlybump17.wlib.adapter.command.CommandMapAdapter { - public static final @NotNull CommandMap COMMAND_MAP = ReflectionUtil.getFieldValue(ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()), Bukkit.getServer()); - public static final @NotNull Map COMMANDS = ReflectionUtil.getFieldValue(ReflectionUtil.getField("knownCommands", SimpleCommandMap.class), COMMAND_MAP); + public static final @NotNull Field COMMAND_MAP = ReflectionUtil.getField("commandMap", Bukkit.getServer().getClass()); + public static final @NotNull Field COMMANDS = ReflectionUtil.getField("knownCommands", SimpleCommandMap.class); @Override public @NotNull CommandMap getCommandMap() { - return COMMAND_MAP; + return ReflectionUtil.getFieldValue(COMMAND_MAP, Bukkit.getServer()); } @Override public void unregisterCommand(@NotNull String command) { - Command removed = COMMANDS.remove(command); + Map commands = getCommands(); + CommandMap commandMap = getCommandMap(); + + Command removed = commands.remove(command); if (removed != null) - removed.unregister(COMMAND_MAP); + removed.unregister(commandMap); } @Override public @NotNull Map getCommands() { - return COMMANDS; + return ReflectionUtil.getFieldValue(COMMANDS, getCommandMap()); } } From 519250dbf6e04ccb244cc9b1388152d84c126d26 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Thu, 14 Nov 2024 18:58:01 -0300 Subject: [PATCH 18/47] added the RequiredSenderType annotation --- .../command/annotation/RequiredSenderType.java | 15 +++++++++++++++ .../registered/RegisteredMethodCommand.java | 10 ++++++++++ 2 files changed, 25 insertions(+) create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java new file mode 100644 index 00000000..23b76cb8 --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java @@ -0,0 +1,15 @@ +package com.wizardlybump17.wlib.command.annotation; + +import org.jetbrains.annotations.NotNull; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Target(ElementType.METHOD) +@Retention(RetentionPolicy.RUNTIME) +public @interface RequiredSenderType { + + @NotNull Class value(); +} diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java index 721860f0..32cd5520 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java @@ -1,8 +1,10 @@ package com.wizardlybump17.wlib.command.registered; import com.wizardlybump17.wlib.command.Argument; +import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.Description; import com.wizardlybump17.wlib.command.annotation.Command; +import com.wizardlybump17.wlib.command.annotation.RequiredSenderType; import com.wizardlybump17.wlib.command.args.ArgsNode; import com.wizardlybump17.wlib.command.args.ArgsReaderRegistry; import com.wizardlybump17.wlib.command.args.ArgsReaderType; @@ -12,6 +14,7 @@ import com.wizardlybump17.wlib.command.executor.CommandExecutor; import lombok.Getter; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; @@ -29,6 +32,7 @@ public class RegisteredMethodCommand extends RegisteredCommand { private final @NotNull Object object; private final @NotNull Method method; private final @NotNull MethodHandle methodHandle; + private final @Nullable RequiredSenderType requiredSenderType; public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object object, @NotNull Method method) throws NoSuchMethodException, IllegalAccessException { super( @@ -43,6 +47,7 @@ public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object obje this.method = method; methodHandle = MethodHandles.publicLookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); prepareNodes(); + requiredSenderType = method.getAnnotation(RequiredSenderType.class); } @Override @@ -120,4 +125,9 @@ private static String trim(String string) { private static boolean isRequiredArgs(String string) { return string.startsWith("<") && string.endsWith(">"); } + + @Override + public boolean canExecute(@NotNull CommandSender sender) { + return requiredSenderType == null || requiredSenderType.value().isInstance(sender); + } } \ No newline at end of file From 9417cd907b707e4628f6d4bfacdda9edd4aa2800 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Thu, 14 Nov 2024 19:01:29 -0300 Subject: [PATCH 19/47] moved the sender check to the RegisteredCommand --- .../command/registered/RegisteredCommand.java | 10 +++++++--- .../registered/RegisteredMethodCommand.java | 16 +++++++--------- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index 03a6faac..911f9fdb 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -10,6 +10,7 @@ import com.wizardlybump17.wlib.command.executor.CommandExecutor; import com.wizardlybump17.wlib.util.StringUtil; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.LinkedHashMap; import java.util.List; @@ -20,16 +21,19 @@ public class RegisteredCommand implements Comparable { private final @NotNull CommandData command; private final @NotNull List nodes; private final @NotNull CommandExecutor executor; + private final @Nullable Class requiredSenderType; - public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor) { + public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor, @Nullable Class requiredSenderType) { this.command = command; this.nodes = nodes; this.executor = executor; + this.requiredSenderType = requiredSenderType; } - protected RegisteredCommand(@NotNull CommandData command, @NotNull List nodes) { + protected RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @Nullable Class requiredSenderType) { this.command = command; this.nodes = nodes; + this.requiredSenderType = requiredSenderType; executor = createExecutor(); } @@ -106,7 +110,7 @@ protected boolean isValidArgs(@NotNull LinkedHashMap args) { } public boolean canExecute(@NotNull CommandSender sender) { - return true; + return requiredSenderType == null || requiredSenderType.isInstance(sender.getHandle()); } @Override diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java index 32cd5520..dba6d9ee 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java @@ -1,7 +1,6 @@ package com.wizardlybump17.wlib.command.registered; import com.wizardlybump17.wlib.command.Argument; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.Description; import com.wizardlybump17.wlib.command.annotation.Command; import com.wizardlybump17.wlib.command.annotation.RequiredSenderType; @@ -32,7 +31,6 @@ public class RegisteredMethodCommand extends RegisteredCommand { private final @NotNull Object object; private final @NotNull Method method; private final @NotNull MethodHandle methodHandle; - private final @Nullable RequiredSenderType requiredSenderType; public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object object, @NotNull Method method) throws NoSuchMethodException, IllegalAccessException { super( @@ -40,14 +38,19 @@ public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object obje annotation, object ), - new ArrayList<>() + new ArrayList<>(), + getRequireSenderType(method) ); this.annotation = annotation; this.object = object; this.method = method; methodHandle = MethodHandles.publicLookup().findVirtual(object.getClass(), method.getName(), MethodType.methodType(method.getReturnType(), method.getParameterTypes())); prepareNodes(); - requiredSenderType = method.getAnnotation(RequiredSenderType.class); + } + + protected static @Nullable Class getRequireSenderType(@NotNull Method method) { + RequiredSenderType type = method.getAnnotation(RequiredSenderType.class); + return type == null ? null : type.value(); } @Override @@ -125,9 +128,4 @@ private static String trim(String string) { private static boolean isRequiredArgs(String string) { return string.startsWith("<") && string.endsWith(">"); } - - @Override - public boolean canExecute(@NotNull CommandSender sender) { - return requiredSenderType == null || requiredSenderType.value().isInstance(sender); - } } \ No newline at end of file From c45cd78b077124f739231aca7bb6966a60e28af9 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Thu, 14 Nov 2024 19:03:38 -0300 Subject: [PATCH 20/47] moved the sender type requirement to the CommandData interface --- .../wizardlybump17/wlib/command/data/CommandData.java | 7 +++++++ .../wlib/command/registered/RegisteredCommand.java | 11 ++++------- .../command/registered/RegisteredMethodCommand.java | 10 +--------- 3 files changed, 12 insertions(+), 16 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java index bf0c98e2..b6eb9874 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -67,4 +67,11 @@ default int getPriority() { default @NotNull String getName() { return getExecution().split(" ")[0]; } + + /** + * @return the {@link CommandSender#getHandle()} that can execute this command + */ + default @Nullable Class getSenderHandleType() { + return null; + } } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index 911f9fdb..e7e0c6c0 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -10,7 +10,6 @@ import com.wizardlybump17.wlib.command.executor.CommandExecutor; import com.wizardlybump17.wlib.util.StringUtil; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.util.LinkedHashMap; import java.util.List; @@ -21,19 +20,16 @@ public class RegisteredCommand implements Comparable { private final @NotNull CommandData command; private final @NotNull List nodes; private final @NotNull CommandExecutor executor; - private final @Nullable Class requiredSenderType; - public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor, @Nullable Class requiredSenderType) { + public RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @NotNull CommandExecutor executor) { this.command = command; this.nodes = nodes; this.executor = executor; - this.requiredSenderType = requiredSenderType; } - protected RegisteredCommand(@NotNull CommandData command, @NotNull List nodes, @Nullable Class requiredSenderType) { + protected RegisteredCommand(@NotNull CommandData command, @NotNull List nodes) { this.command = command; this.nodes = nodes; - this.requiredSenderType = requiredSenderType; executor = createExecutor(); } @@ -110,7 +106,8 @@ protected boolean isValidArgs(@NotNull LinkedHashMap args) { } public boolean canExecute(@NotNull CommandSender sender) { - return requiredSenderType == null || requiredSenderType.isInstance(sender.getHandle()); + Class type = command.getSenderHandleType(); + return type == null || type.isInstance(sender); } @Override diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java index dba6d9ee..721860f0 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredMethodCommand.java @@ -3,7 +3,6 @@ import com.wizardlybump17.wlib.command.Argument; import com.wizardlybump17.wlib.command.Description; import com.wizardlybump17.wlib.command.annotation.Command; -import com.wizardlybump17.wlib.command.annotation.RequiredSenderType; import com.wizardlybump17.wlib.command.args.ArgsNode; import com.wizardlybump17.wlib.command.args.ArgsReaderRegistry; import com.wizardlybump17.wlib.command.args.ArgsReaderType; @@ -13,7 +12,6 @@ import com.wizardlybump17.wlib.command.executor.CommandExecutor; import lombok.Getter; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import java.lang.invoke.MethodHandle; import java.lang.invoke.MethodHandles; @@ -38,8 +36,7 @@ public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object obje annotation, object ), - new ArrayList<>(), - getRequireSenderType(method) + new ArrayList<>() ); this.annotation = annotation; this.object = object; @@ -48,11 +45,6 @@ public RegisteredMethodCommand(@NotNull Command annotation, @NotNull Object obje prepareNodes(); } - protected static @Nullable Class getRequireSenderType(@NotNull Method method) { - RequiredSenderType type = method.getAnnotation(RequiredSenderType.class); - return type == null ? null : type.value(); - } - @Override protected CommandExecutor createExecutor() { return (sender, args) -> { From ecbdb5c3a37560541f61791847d4ae4cfb40cd4c Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Thu, 14 Nov 2024 19:05:56 -0300 Subject: [PATCH 21/47] deleted the unused file --- .../command/annotation/RequiredSenderType.java | 15 --------------- 1 file changed, 15 deletions(-) delete mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java deleted file mode 100644 index 23b76cb8..00000000 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/RequiredSenderType.java +++ /dev/null @@ -1,15 +0,0 @@ -package com.wizardlybump17.wlib.command.annotation; - -import org.jetbrains.annotations.NotNull; - -import java.lang.annotation.ElementType; -import java.lang.annotation.Retention; -import java.lang.annotation.RetentionPolicy; -import java.lang.annotation.Target; - -@Target(ElementType.METHOD) -@Retention(RetentionPolicy.RUNTIME) -public @interface RequiredSenderType { - - @NotNull Class value(); -} From a874f8aaf7b4f3ea39f3dc32b07ae3f87ac5dec8 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Thu, 14 Nov 2024 19:06:21 -0300 Subject: [PATCH 22/47] improved the sender checking --- .../com/wizardlybump17/wlib/command/annotation/Command.java | 6 ++++++ .../wlib/command/data/AnnotationCommandData.java | 4 ++++ .../com/wizardlybump17/wlib/command/data/CommandData.java | 4 ++-- .../wlib/command/registered/RegisteredCommand.java | 3 +-- 4 files changed, 13 insertions(+), 4 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java index 1009d78b..1628fcf0 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java @@ -1,6 +1,7 @@ package com.wizardlybump17.wlib.command.annotation; import com.wizardlybump17.wlib.command.CommandSender; +import org.jetbrains.annotations.NotNull; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; @@ -83,4 +84,9 @@ * @return if the {@link #invalidSenderMessage()} is a field in the class that have this annotation */ boolean invalidSenderMessageIsField() default false; + + /** + * @return the type of the {@link CommandSender#getHandle()} that can execute this command + */ + @NotNull Class senderType() default Object.class; } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java index b659ea2e..38fc9b9b 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java @@ -71,6 +71,10 @@ public int getPriority() { return annotation; } + @Override + public @NotNull Class getSenderType() { + return annotation.senderType(); + } @Override public boolean equals(Object object1) { diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java index b6eb9874..504eb6c9 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -71,7 +71,7 @@ default int getPriority() { /** * @return the {@link CommandSender#getHandle()} that can execute this command */ - default @Nullable Class getSenderHandleType() { - return null; + default @NotNull Class getSenderType() { + return Object.class; } } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index e7e0c6c0..dc438338 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -106,8 +106,7 @@ protected boolean isValidArgs(@NotNull LinkedHashMap args) { } public boolean canExecute(@NotNull CommandSender sender) { - Class type = command.getSenderHandleType(); - return type == null || type.isInstance(sender); + return command.getSenderType().isInstance(sender); } @Override From 04af805ab1bdc5df68ceba8fa51ca6d7583bfd3f Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Thu, 14 Nov 2024 21:58:46 -0300 Subject: [PATCH 23/47] fixed the RegisteredCommand#canExecute(CommandSender) method by checking the CommandSender#getHandle() --- .../wlib/command/registered/RegisteredCommand.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index dc438338..bb7d19a6 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -106,7 +106,7 @@ protected boolean isValidArgs(@NotNull LinkedHashMap args) { } public boolean canExecute(@NotNull CommandSender sender) { - return command.getSenderType().isInstance(sender); + return command.getSenderType().isInstance(sender.getHandle()); } @Override From 4ba17578fa31f03e25268462e584ac9e7c9adcae Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 00:45:30 -0300 Subject: [PATCH 24/47] made CommandData be an abstract class --- .../command/data/AnnotationCommandData.java | 12 ++++++------ .../wlib/command/data/CommandData.java | 18 +++++++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java index 38fc9b9b..e45d26f0 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/AnnotationCommandData.java @@ -10,7 +10,7 @@ import java.util.Map; import java.util.Objects; -public class AnnotationCommandData implements CommandData { +public class AnnotationCommandData extends CommandData { private final @NotNull Command annotation; private final @NotNull Object object; @@ -29,29 +29,29 @@ public AnnotationCommandData(@NotNull Command annotation, @NotNull Object object @Override public @Nullable String getPermission() { String permission = annotation.permission(); - return permission.isBlank() ? CommandData.super.getPermission() : permission; + return permission.isBlank() ? super.getPermission() : permission; } @Override public @Nullable String getPermissionMessage() { - return getMessage(annotation.permissionMessage(), annotation.permissionMessageIsField(), CommandData.super.getPermissionMessage()); + return getMessage(annotation.permissionMessage(), annotation.permissionMessageIsField(), super.getPermissionMessage()); } @Override public int getPriority() { int priority = annotation.priority(); - return priority == -1 ? CommandData.super.getPriority() : priority; + return priority == -1 ? super.getPriority() : priority; } @Override public @Nullable String getDescription() { String description = annotation.description(); - return description.isBlank() ? CommandData.super.getDescription() : description; + return description.isBlank() ? super.getDescription() : description; } @Override public @Nullable String getInvalidSenderMessage() { - return getMessage(annotation.invalidSenderMessage(), annotation.invalidSenderMessageIsField(), CommandData.super.getInvalidSenderMessage()); + return getMessage(annotation.invalidSenderMessage(), annotation.invalidSenderMessageIsField(), super.getInvalidSenderMessage()); } protected @Nullable String getMessage(@NotNull String message, boolean isField, @Nullable String defaultMessage) { diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java index 504eb6c9..c38112cb 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -4,7 +4,7 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -public interface CommandData { +public abstract class CommandData { /** *

How the sender must type the command in order for it to be triggered. @@ -25,12 +25,12 @@ public interface CommandData { * @return how the command must be sent to be triggered * @see com.wizardlybump17.wlib.command.args.reader.ArgsReader */ - @NotNull String getExecution(); + public abstract @NotNull String getExecution(); /** * @return which permission the sender must have to trigger this command */ - default @Nullable String getPermission() { + public @Nullable String getPermission() { return null; } @@ -38,21 +38,21 @@ public interface CommandData { *

Used when the {@link CommandSender} does not have the required {@link #getPermission()}.

* @return the message to be sent when the {@link CommandSender} does not have the required {@link #getPermission()} */ - default @Nullable String getPermissionMessage() { + public @Nullable String getPermissionMessage() { return null; } /** * @return the priority of this command */ - default int getPriority() { + public int getPriority() { return getExecution().split(" ").length; } /** * @return the description of this command */ - default @Nullable String getDescription() { + public @Nullable String getDescription() { return null; } @@ -60,18 +60,18 @@ default int getPriority() { *

Used when the {@link CommandSender} is not valid for this command.

* @return the message to be sent when the {@link CommandSender} is not valid for this command */ - default @Nullable String getInvalidSenderMessage() { + public @Nullable String getInvalidSenderMessage() { return null; } - default @NotNull String getName() { + public final @NotNull String getName() { return getExecution().split(" ")[0]; } /** * @return the {@link CommandSender#getHandle()} that can execute this command */ - default @NotNull Class getSenderType() { + public @NotNull Class getSenderType() { return Object.class; } } From 25b9e5bd66d01c142273cfdfd9a29a1ffb76502c Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 00:45:58 -0300 Subject: [PATCH 25/47] added the SimpleCommandData system --- .../wlib/command/data/CommandData.java | 4 + .../wlib/command/data/SimpleCommandData.java | 151 ++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java index c38112cb..4d32e6d8 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -74,4 +74,8 @@ public int getPriority() { public @NotNull Class getSenderType() { return Object.class; } + + public static @NotNull SimpleCommandData.Builder builder() { + return new SimpleCommandData.Builder(); + } } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java new file mode 100644 index 00000000..f26df07f --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java @@ -0,0 +1,151 @@ +package com.wizardlybump17.wlib.command.data; + +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; + +public class SimpleCommandData extends CommandData { + + private final @NotNull String execution; + private final @Nullable String permission; + private final @Nullable String permissionMessage; + private final int priority; + private final @Nullable String description; + private final @Nullable String invalidSenderMessage; + private final @NotNull Class senderType; + + public SimpleCommandData(@NotNull String execution, @Nullable String permission, @Nullable String permissionMessage, int priority, @Nullable String description, @Nullable String invalidSenderMessage, @NotNull Class senderType) { + this.execution = execution; + this.permission = permission; + this.permissionMessage = permissionMessage; + this.priority = priority; + this.description = description; + this.invalidSenderMessage = invalidSenderMessage; + this.senderType = senderType; + } + + @Override + public @NotNull String getExecution() { + return execution; + } + + @Override + public @Nullable String getPermission() { + return permission; + } + + @Override + public @Nullable String getPermissionMessage() { + return permissionMessage; + } + + @Override + public int getPriority() { + return priority; + } + + @Override + public @Nullable String getDescription() { + return description; + } + + @Override + public @Nullable String getInvalidSenderMessage() { + return invalidSenderMessage; + } + + @Override + public @NotNull Class getSenderType() { + return senderType; + } + + public static class Builder { + + Builder() { + } + + private @Nullable String execution; + private @Nullable String permission; + private @Nullable String permissionMessage; + private @Nullable Integer priority; + private @Nullable String description; + private @Nullable String invalidSenderMessage; + private @Nullable Class senderType; + + public @Nullable String execution() { + return execution; + } + + public @NotNull Builder execution(@Nullable String execution) { + this.execution = execution; + return this; + } + + public @Nullable String permission() { + return permission; + } + + public @NotNull Builder permission(@Nullable String permission) { + this.permission = permission; + return this; + } + + public @Nullable String permissionMessage() { + return permissionMessage; + } + + public @NotNull Builder permissionMessage(@Nullable String permissionMessage) { + this.permissionMessage = permissionMessage; + return this; + } + + public @Nullable Integer priority() { + return priority; + } + + public @NotNull Builder priority(@Nullable Integer priority) { + this.priority = priority; + return this; + } + + public @Nullable String description() { + return description; + } + + public @NotNull Builder description(@Nullable String description) { + this.description = description; + return this; + } + + public @Nullable String invalidSenderMessage() { + return invalidSenderMessage; + } + + public @NotNull Builder invalidSenderMessage(@Nullable String invalidSenderMessage) { + this.invalidSenderMessage = invalidSenderMessage; + return this; + } + + public @Nullable Class senderType() { + return senderType; + } + + public @NotNull Builder senderType(@Nullable Class senderType) { + this.senderType = senderType; + return this; + } + + public @NotNull CommandData build() { + return new SimpleCommandData( + Objects.requireNonNull(execution, "The execution can not be null"), + permission, + permissionMessage, + priority == null ? execution.split(" ").length : priority, + description, + invalidSenderMessage, + senderType == null ? Object.class : senderType + ); + } + } +} From 9052f1f68ec3c5427e6059f8e282d74365332234 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 18:03:47 -0300 Subject: [PATCH 26/47] added the ArgsNode.userInput(String, ArgsReader) method --- .../java/com/wizardlybump17/wlib/command/args/ArgsNode.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java index f0149eb2..daf4b08b 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java @@ -46,4 +46,8 @@ public Object parse(String input) throws ArgsReaderException { public static @NotNull ArgsNode literal(@NotNull String string) { return new ArgsNode(string, false, null, null, false); } + + public static @NotNull ArgsNode userInput(@NotNull String name, @NotNull ArgsReader reader) { + return new ArgsNode(name, true, reader, null, false); + } } From f3eb68a990f9103fc3c3fb0ba975fa8b7b463cca Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 18:13:44 -0300 Subject: [PATCH 27/47] added the CommandSender#hasId(UUID) method --- .../wlib/bungee/command/sender/CommandSender.java | 7 +++++++ .../com/wizardlybump17/wlib/command/CommandSender.java | 6 ++++++ .../wizardlybump17/wlib/command/sender/CommandSender.java | 8 ++++++++ 3 files changed, 21 insertions(+) diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java index ef0ddda4..2796fa68 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java @@ -6,6 +6,8 @@ import net.md_5.bungee.api.connection.ProxiedPlayer; import org.jetbrains.annotations.NotNull; +import java.util.UUID; + @RequiredArgsConstructor public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender { @@ -43,4 +45,9 @@ public boolean hasPermission(String permission) { public @NotNull ConnectedPlayer asConnectedPlayer() { return (ConnectedPlayer) handle; } + + @Override + public boolean hasId(@NotNull UUID id) { + return handle instanceof ProxiedPlayer player && player.getUniqueId().equals(id); + } } diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java index 127a3037..29f36f6c 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java @@ -1,5 +1,9 @@ package com.wizardlybump17.wlib.command; +import org.jetbrains.annotations.NotNull; + +import java.util.UUID; + /** * Represents a command sender, someone that can trigger commands. * @@ -19,4 +23,6 @@ public interface CommandSender { String getName(); boolean hasPermission(String permission); + + boolean hasId(@NotNull UUID id); } diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java index db06a313..4d25ce26 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java @@ -2,9 +2,12 @@ import org.bukkit.command.BlockCommandSender; import org.bukkit.command.ConsoleCommandSender; +import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.UUID; + public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender { private final @NotNull org.bukkit.command.CommandSender handle; @@ -49,4 +52,9 @@ public boolean hasPermission(String permission) { public @NotNull Player asPlayer() { return (Player) handle; } + + @Override + public boolean hasId(@NotNull UUID id) { + return handle instanceof Entity entity && entity.getUniqueId().equals(id); + } } From 8fac630155375ef80d1402d3d7eb90a18ddb82c6 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 19:20:04 -0300 Subject: [PATCH 28/47] added the ArgsNode.userInput(String, Class) method --- .../java/com/wizardlybump17/wlib/command/args/ArgsNode.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java index daf4b08b..23d07d47 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/ArgsNode.java @@ -50,4 +50,8 @@ public Object parse(String input) throws ArgsReaderException { public static @NotNull ArgsNode userInput(@NotNull String name, @NotNull ArgsReader reader) { return new ArgsNode(name, true, reader, null, false); } + + public static @NotNull ArgsNode userInput(@NotNull String name, @NotNull Class type) { + return userInput(name, ArgsReaderRegistry.INSTANCE.getReader(type)); + } } From 421a46f3d8bcb1406c496f3a0c75d5cdae05528a Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 21:35:36 -0300 Subject: [PATCH 29/47] using the CommandManager to execute the command instead --- .../command/registered/RegisteredBukkitCommand.java | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java index b303c099..ef9f9b61 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java @@ -13,6 +13,7 @@ import java.util.List; import java.util.logging.Level; +import java.util.logging.Logger; public class RegisteredBukkitCommand extends RegisteredCommand { @@ -30,13 +31,16 @@ public RegisteredBukkitCommand(@NotNull CommandData command, @NotNull List Date: Fri, 15 Nov 2024 21:35:56 -0300 Subject: [PATCH 30/47] removed the unused field --- .../java/com/wizardlybump17/wlib/command/CommandManager.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 1a1888f2..cb4cda0f 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -11,7 +11,6 @@ import lombok.NonNull; import org.jetbrains.annotations.NotNull; -import java.lang.reflect.Field; import java.util.*; @Getter @@ -19,7 +18,6 @@ public class CommandManager { private final List commands = new ArrayList<>(); protected final CommandHolder holder; - private final @NonNull Map, Map> fieldCache = new HashMap<>(); private final @NotNull Set commandExtractors = new HashSet<>(); public CommandManager(@NotNull CommandHolder holder) { From faadaeab23290e8bc81979292504ec2c30725384 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 21:45:52 -0300 Subject: [PATCH 31/47] made the CommandManager store commands using a Map where the key is the command name. Removed lombok --- .../wlib/command/CommandManager.java | 82 +++++++++++-------- 1 file changed, 48 insertions(+), 34 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index cb4cda0f..09eedbe5 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -7,17 +7,14 @@ import com.wizardlybump17.wlib.command.extractor.MethodCommandExtractor; import com.wizardlybump17.wlib.command.holder.CommandHolder; import com.wizardlybump17.wlib.command.registered.RegisteredCommand; -import lombok.Getter; -import lombok.NonNull; import org.jetbrains.annotations.NotNull; import java.util.*; -@Getter public class CommandManager { - private final List commands = new ArrayList<>(); - protected final CommandHolder holder; + private final @NotNull Map> commands = new HashMap<>(); + protected final @NotNull CommandHolder holder; private final @NotNull Set commandExtractors = new HashSet<>(); public CommandManager(@NotNull CommandHolder holder) { @@ -26,33 +23,41 @@ public CommandManager(@NotNull CommandHolder holder) { commandExtractors.add(new DirectCommandExtractor()); } - public void registerCommands(Object... objects) { - for (Object object : objects) { - for (CommandExtractor extractor : commandExtractors) { - List commands = extractor.extract(this, holder, object); - commands.forEach(command -> command.onRegister(this)); - this.commands.addAll(commands); - } - } - commands.sort(null); + public void registerCommands(@NotNull Object @NotNull ... objects) { + for (Object object : objects) + for (CommandExtractor extractor : commandExtractors) + registerCommands(extractor.extract(this, holder, object)); } public void registerCommands(@NotNull Collection commands) { - commands.forEach(command -> command.onRegister(this)); - this.commands.addAll(commands); - this.commands.sort(null); + commands.forEach(this::registerCommand); + } + + public void registerCommand(@NotNull RegisteredCommand command) { + command.onRegister(this); + commands.computeIfAbsent(command.getName().toLowerCase(), $ -> new TreeSet<>()).add(command); } public void unregisterCommands() { - commands.forEach(command -> command.onUnregister(this)); + commands.forEach((name, commands) -> { + commands.forEach(command -> command.onUnregister(this)); + commands.clear(); + }); commands.clear(); } - public void execute(CommandSender sender, String string) throws CommandException { + public void execute(@NotNull CommandSender sender, @NotNull String string) throws CommandException { if (commands.isEmpty()) return; - for (RegisteredCommand registeredCommand : commands) { + int spaceIndex = string.indexOf(' '); + String name; + if (spaceIndex == -1) + name = string; + else + name = string.substring(0, spaceIndex); + + for (RegisteredCommand registeredCommand : commands.getOrDefault(name, Set.of())) { CommandData command = registeredCommand.getCommand(); CommandResult result = registeredCommand.execute(sender, string); @@ -78,24 +83,33 @@ public void execute(CommandSender sender, String string) throws CommandExcept } } - @NonNull - public List<@NonNull String> autoComplete(@NonNull CommandSender sender, @NonNull String string) { + public @NotNull List<@NotNull String> autoComplete(@NotNull CommandSender sender, @NotNull String string) { return List.of(); } - public List getCommand(String name) { - List commands = new ArrayList<>(this.commands.size()); - for (RegisteredCommand command : this.commands) - if (command.getName().equalsIgnoreCase(name)) - commands.add(command); - return commands; + public @NotNull List getCommands(@NotNull String name) { + Set commands = this.commands.get(name); + return commands == null ? List.of() : List.copyOf(commands); + } + + public @NotNull List getCommands(@NotNull Object object) { + List result = new ArrayList<>(commands.size()); + for (Set commands : this.commands.values()) + for (RegisteredCommand command : commands) + if (command.isOwnedBy(object)) + result.add(command); + return result; + } + + public @NotNull CommandHolder getHolder() { + return holder; + } + + public @NotNull Map> getCommands() { + return Map.copyOf(commands); } - public List getCommands(@NotNull Object object) { - List commands = new ArrayList<>(this.commands.size()); - for (RegisteredCommand command : this.commands) - if (command.isOwnedBy(object)) - commands.add(command); - return commands; + public @NotNull Set getCommandExtractors() { + return Set.copyOf(commandExtractors); } } \ No newline at end of file From 7f15598907a48a7d307600c82fea1f0b2c682b95 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 21:46:04 -0300 Subject: [PATCH 32/47] annotated with NotNull --- .../wlib/bungee/command/BungeeCommandManager.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java index 416de0ef..bfd1bafa 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandManager.java @@ -4,6 +4,7 @@ import com.wizardlybump17.wlib.command.registered.RegisteredCommand; import net.md_5.bungee.api.plugin.Command; import net.md_5.bungee.api.plugin.Plugin; +import org.jetbrains.annotations.NotNull; public class BungeeCommandManager extends CommandManager { @@ -12,7 +13,7 @@ public BungeeCommandManager(BungeeCommandHolder holder) { } @Override - public void registerCommands(Object... objects) { + public void registerCommands(@NotNull Object @NotNull ... objects) { super.registerCommands(objects); for (Object object : objects) { for (RegisteredCommand command : getCommands(object)) { From a83e178f74ab00688097137f9dc137fa861ed392 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 21:47:48 -0300 Subject: [PATCH 33/47] added methods to manipulate the CommandManager#getCommandExtractors() --- .../wizardlybump17/wlib/command/CommandManager.java | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 09eedbe5..1594d6d9 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -112,4 +112,16 @@ public void execute(@NotNull CommandSender sender, @NotNull String string) th public @NotNull Set getCommandExtractors() { return Set.copyOf(commandExtractors); } + + public void addCommandExtractor(@NotNull CommandExtractor extractor) { + commandExtractors.add(extractor); + } + + public void removeCommandExtractor(@NotNull CommandExtractor extractor) { + commandExtractors.remove(extractor); + } + + public boolean hasCommandExtractor(@NotNull CommandExtractor extractor) { + return commandExtractors.contains(extractor); + } } \ No newline at end of file From e7d02f136f98962f4bd026eaa500b95daa686335 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Fri, 15 Nov 2024 22:00:31 -0300 Subject: [PATCH 34/47] using a List instead --- .../wlib/command/CommandManager.java | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 1594d6d9..449d4636 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -13,7 +13,7 @@ public class CommandManager { - private final @NotNull Map> commands = new HashMap<>(); + private final @NotNull Map> commands = new HashMap<>(); protected final @NotNull CommandHolder holder; private final @NotNull Set commandExtractors = new HashSet<>(); @@ -35,7 +35,9 @@ public void registerCommands(@NotNull Collection commands) { public void registerCommand(@NotNull RegisteredCommand command) { command.onRegister(this); - commands.computeIfAbsent(command.getName().toLowerCase(), $ -> new TreeSet<>()).add(command); + List commands = this.commands.computeIfAbsent(command.getName().toLowerCase(), $ -> new ArrayList<>()); + commands.add(command); + commands.sort(null); } public void unregisterCommands() { @@ -57,7 +59,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String string) th else name = string.substring(0, spaceIndex); - for (RegisteredCommand registeredCommand : commands.getOrDefault(name, Set.of())) { + for (RegisteredCommand registeredCommand : commands.getOrDefault(name, List.of())) { CommandData command = registeredCommand.getCommand(); CommandResult result = registeredCommand.execute(sender, string); @@ -88,13 +90,13 @@ public void execute(@NotNull CommandSender sender, @NotNull String string) th } public @NotNull List getCommands(@NotNull String name) { - Set commands = this.commands.get(name); + List commands = this.commands.get(name); return commands == null ? List.of() : List.copyOf(commands); } public @NotNull List getCommands(@NotNull Object object) { List result = new ArrayList<>(commands.size()); - for (Set commands : this.commands.values()) + for (List commands : this.commands.values()) for (RegisteredCommand command : commands) if (command.isOwnedBy(object)) result.add(command); @@ -105,7 +107,7 @@ public void execute(@NotNull CommandSender sender, @NotNull String string) th return holder; } - public @NotNull Map> getCommands() { + public @NotNull Map> getCommands() { return Map.copyOf(commands); } From fb83f0bb9922704741860f9676c70229f4892048 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 23 Nov 2024 20:56:25 -0300 Subject: [PATCH 35/47] moved to the proper package and renamed the classes implementing CommandSender --- .../wlib/bungee/command/BungeeCommandExecutor.java | 6 +++--- .../sender/{CommandSender.java => BungeeCommandSender.java} | 2 +- .../com/wizardlybump17/wlib/command/CommandManager.java | 1 + .../com/wizardlybump17/wlib/command/annotation/Command.java | 2 +- .../wizardlybump17/wlib/command/args/reader/ArgsReader.java | 2 +- .../wlib/command/args/reader/BooleanReader.java | 2 +- .../wizardlybump17/wlib/command/args/reader/ByteReader.java | 2 +- .../wlib/command/args/reader/DoubleReader.java | 2 +- .../wlib/command/args/reader/FloatReader.java | 2 +- .../wlib/command/args/reader/IntegerReader.java | 2 +- .../wlib/command/args/reader/ShortReader.java | 2 +- .../wlib/command/args/reader/StringReader.java | 2 +- .../wizardlybump17/wlib/command/args/reader/UUIDReader.java | 2 +- .../wlib/command/completer/ArgumentCompleter.java | 2 +- .../com/wizardlybump17/wlib/command/data/CommandData.java | 2 +- .../wlib/command/executor/CommandExecutor.java | 2 +- .../wlib/command/extractor/MethodCommandExtractor.java | 2 +- .../wizardlybump17/wlib/command/holder/CommandExecutor.java | 2 +- .../wlib/command/registered/RegisteredCommand.java | 2 +- .../wlib/command/{ => sender}/CommandSender.java | 2 +- .../wizardlybump17/wlib/command/BukkitCommandExecutor.java | 6 ++++-- .../wlib/command/completer/EntityTypeArgumentCompleter.java | 2 +- .../wlib/command/completer/MaterialArgumentCompleter.java | 2 +- .../wlib/command/completer/PlayerArgumentCompleter.java | 2 +- .../wlib/command/reader/BlockDataArgsReader.java | 2 +- .../wlib/command/reader/EntityTypeArgsReader.java | 2 +- .../wlib/command/reader/MapJsonArgsReader.java | 2 +- .../wizardlybump17/wlib/command/reader/MaterialReader.java | 2 +- .../wlib/command/reader/OfflinePlayerReader.java | 2 +- .../wizardlybump17/wlib/command/reader/PlayerReader.java | 2 +- .../wlib/command/registered/RegisteredBukkitCommand.java | 3 ++- .../sender/{CommandSender.java => BukkitCommandSender.java} | 4 ++-- 32 files changed, 39 insertions(+), 35 deletions(-) rename bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/{CommandSender.java => BungeeCommandSender.java} (91%) rename commands/src/main/java/com/wizardlybump17/wlib/command/{ => sender}/CommandSender.java (91%) rename core/src/main/java/com/wizardlybump17/wlib/command/sender/{CommandSender.java => BukkitCommandSender.java} (87%) diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java index 743da1c2..cca749bd 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/BungeeCommandExecutor.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.bungee.command; -import com.wizardlybump17.wlib.bungee.command.sender.CommandSender; +import com.wizardlybump17.wlib.bungee.command.sender.BungeeCommandSender; import com.wizardlybump17.wlib.command.CommandManager; import com.wizardlybump17.wlib.command.exception.CommandException; import com.wizardlybump17.wlib.command.holder.CommandExecutor; @@ -18,14 +18,14 @@ public BungeeCommandExecutor(CommandManager manager, String name) { } @Override - public void execute(com.wizardlybump17.wlib.command.CommandSender sender, String commandName, String[] args) throws CommandException { + public void execute(com.wizardlybump17.wlib.command.sender.CommandSender sender, String commandName, String[] args) throws CommandException { manager.execute(sender, commandName + " " + String.join(" ", args)); } @Override public void execute(net.md_5.bungee.api.CommandSender sender, String[] args) { try { - execute(new CommandSender(sender), getName(), args); + execute(new BungeeCommandSender(sender), getName(), args); } catch (CommandException e) { manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command", e); } diff --git a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/BungeeCommandSender.java similarity index 91% rename from bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java rename to bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/BungeeCommandSender.java index 2796fa68..702f31eb 100644 --- a/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/CommandSender.java +++ b/bungee/src/main/java/com/wizardlybump17/wlib/bungee/command/sender/BungeeCommandSender.java @@ -9,7 +9,7 @@ import java.util.UUID; @RequiredArgsConstructor -public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender { +public class BungeeCommandSender implements com.wizardlybump17.wlib.command.sender.CommandSender { private final net.md_5.bungee.api.CommandSender handle; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java index 449d4636..08bff110 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/CommandManager.java @@ -7,6 +7,7 @@ import com.wizardlybump17.wlib.command.extractor.MethodCommandExtractor; import com.wizardlybump17.wlib.command.holder.CommandHolder; import com.wizardlybump17.wlib.command.registered.RegisteredCommand; +import com.wizardlybump17.wlib.command.sender.CommandSender; import org.jetbrains.annotations.NotNull; import java.util.*; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java index 1628fcf0..b941b23b 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/annotation/Command.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.annotation; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import org.jetbrains.annotations.NotNull; import java.lang.annotation.ElementType; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ArgsReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ArgsReader.java index 3ca7ecee..d8a6fa5a 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ArgsReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ArgsReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.jetbrains.annotations.Nullable; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/BooleanReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/BooleanReader.java index ef854c6a..539558be 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/BooleanReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/BooleanReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ByteReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ByteReader.java index 6b7e920d..00c9056c 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ByteReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ByteReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/DoubleReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/DoubleReader.java index 9dd12592..b7c4f356 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/DoubleReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/DoubleReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/FloatReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/FloatReader.java index 1541cf68..f90a346a 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/FloatReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/FloatReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/IntegerReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/IntegerReader.java index 111c3c09..5123f358 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/IntegerReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/IntegerReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ShortReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ShortReader.java index af95f3ff..14e84005 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ShortReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/ShortReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/StringReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/StringReader.java index e6719a67..93f2c9c4 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/StringReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/StringReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/UUIDReader.java b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/UUIDReader.java index d061710b..d9f4c7db 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/UUIDReader.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/args/reader/UUIDReader.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.args.reader; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/completer/ArgumentCompleter.java b/commands/src/main/java/com/wizardlybump17/wlib/command/completer/ArgumentCompleter.java index 187191c0..f0145c0d 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/completer/ArgumentCompleter.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/completer/ArgumentCompleter.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.completer; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java index 4d32e6d8..9dd74ef5 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/CommandData.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.data; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java index 298f73b7..a0c3720a 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/executor/CommandExecutor.java @@ -1,7 +1,7 @@ package com.wizardlybump17.wlib.command.executor; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.sender.CommandSender; import org.jetbrains.annotations.NotNull; import java.util.Map; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java index 3e650e68..354da31d 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/extractor/MethodCommandExtractor.java @@ -1,11 +1,11 @@ package com.wizardlybump17.wlib.command.extractor; import com.wizardlybump17.wlib.command.CommandManager; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.annotation.Command; import com.wizardlybump17.wlib.command.holder.CommandHolder; import com.wizardlybump17.wlib.command.registered.RegisteredCommand; import com.wizardlybump17.wlib.command.registered.RegisteredMethodCommand; +import com.wizardlybump17.wlib.command.sender.CommandSender; import org.jetbrains.annotations.NotNull; import java.lang.reflect.Method; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java b/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java index 24bf890b..05321945 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/holder/CommandExecutor.java @@ -1,8 +1,8 @@ package com.wizardlybump17.wlib.command.holder; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.sender.CommandSender; /** * Interface for intercepting commands when they are called diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java index bb7d19a6..aa31aea1 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredCommand.java @@ -2,12 +2,12 @@ import com.wizardlybump17.wlib.command.CommandManager; import com.wizardlybump17.wlib.command.CommandResult; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.ArgsNode; import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; import com.wizardlybump17.wlib.command.data.CommandData; import com.wizardlybump17.wlib.command.exception.CommandException; import com.wizardlybump17.wlib.command.executor.CommandExecutor; +import com.wizardlybump17.wlib.command.sender.CommandSender; import com.wizardlybump17.wlib.util.StringUtil; import org.jetbrains.annotations.NotNull; diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java b/commands/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java similarity index 91% rename from commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java rename to commands/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java index 29f36f6c..0786ac74 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/CommandSender.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java @@ -1,4 +1,4 @@ -package com.wizardlybump17.wlib.command; +package com.wizardlybump17.wlib.command.sender; import org.jetbrains.annotations.NotNull; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java b/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java index c17fb0b0..c878081f 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/BukkitCommandExecutor.java @@ -1,6 +1,8 @@ package com.wizardlybump17.wlib.command; import com.wizardlybump17.wlib.command.exception.CommandException; +import com.wizardlybump17.wlib.command.sender.BukkitCommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.RequiredArgsConstructor; import org.bukkit.command.Command; import org.bukkit.command.TabExecutor; @@ -18,7 +20,7 @@ public class BukkitCommandExecutor implements TabExecutor, com.wizardlybump17.wl @Override public boolean onCommand(@NotNull org.bukkit.command.CommandSender sender, @NotNull Command command, @NotNull String label, String[] args) { try { - execute(new com.wizardlybump17.wlib.command.sender.CommandSender(sender), command.getName(), args); + execute(new BukkitCommandSender(sender), command.getName(), args); } catch (CommandException e) { manager.getHolder().getLogger().log(Level.SEVERE, "Error while executing a command", e); } @@ -26,7 +28,7 @@ public boolean onCommand(@NotNull org.bukkit.command.CommandSender sender, @NotN } @Override - public void execute(com.wizardlybump17.wlib.command.CommandSender sender, String commandName, String[] args) throws CommandException { + public void execute(CommandSender sender, String commandName, String[] args) throws CommandException { String commandExecution = commandName + " " + String.join(" ", args); manager.execute(sender, commandExecution); } diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/completer/EntityTypeArgumentCompleter.java b/core/src/main/java/com/wizardlybump17/wlib/command/completer/EntityTypeArgumentCompleter.java index de7bf8f4..2068365d 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/completer/EntityTypeArgumentCompleter.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/completer/EntityTypeArgumentCompleter.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.completer; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.entity.EntityType; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/completer/MaterialArgumentCompleter.java b/core/src/main/java/com/wizardlybump17/wlib/command/completer/MaterialArgumentCompleter.java index 6ded4874..8c72508f 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/completer/MaterialArgumentCompleter.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/completer/MaterialArgumentCompleter.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.completer; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.Material; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/completer/PlayerArgumentCompleter.java b/core/src/main/java/com/wizardlybump17/wlib/command/completer/PlayerArgumentCompleter.java index 2d963104..29145e42 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/completer/PlayerArgumentCompleter.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/completer/PlayerArgumentCompleter.java @@ -1,6 +1,6 @@ package com.wizardlybump17.wlib.command.completer; -import com.wizardlybump17.wlib.command.CommandSender; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.entity.Player; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/reader/BlockDataArgsReader.java b/core/src/main/java/com/wizardlybump17/wlib/command/reader/BlockDataArgsReader.java index dca102c3..8b9ca548 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/reader/BlockDataArgsReader.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/reader/BlockDataArgsReader.java @@ -1,8 +1,8 @@ package com.wizardlybump17.wlib.command.reader; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReader; import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.Material; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/reader/EntityTypeArgsReader.java b/core/src/main/java/com/wizardlybump17/wlib/command/reader/EntityTypeArgsReader.java index 4ca9a7ce..c6986ffb 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/reader/EntityTypeArgsReader.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/reader/EntityTypeArgsReader.java @@ -1,7 +1,7 @@ package com.wizardlybump17.wlib.command.reader; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReader; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.entity.EntityType; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/reader/MapJsonArgsReader.java b/core/src/main/java/com/wizardlybump17/wlib/command/reader/MapJsonArgsReader.java index a456c1c8..d7c82251 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/reader/MapJsonArgsReader.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/reader/MapJsonArgsReader.java @@ -2,9 +2,9 @@ import com.google.gson.Gson; import com.google.gson.JsonSyntaxException; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReader; import com.wizardlybump17.wlib.command.args.reader.ArgsReaderException; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import java.util.List; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/reader/MaterialReader.java b/core/src/main/java/com/wizardlybump17/wlib/command/reader/MaterialReader.java index 8251dd9c..2daf7f90 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/reader/MaterialReader.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/reader/MaterialReader.java @@ -1,7 +1,7 @@ package com.wizardlybump17.wlib.command.reader; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReader; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.Material; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/reader/OfflinePlayerReader.java b/core/src/main/java/com/wizardlybump17/wlib/command/reader/OfflinePlayerReader.java index 5da69881..46c29ea4 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/reader/OfflinePlayerReader.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/reader/OfflinePlayerReader.java @@ -1,7 +1,7 @@ package com.wizardlybump17.wlib.command.reader; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReader; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.OfflinePlayer; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/reader/PlayerReader.java b/core/src/main/java/com/wizardlybump17/wlib/command/reader/PlayerReader.java index ddb38aa7..1521ff37 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/reader/PlayerReader.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/reader/PlayerReader.java @@ -1,7 +1,7 @@ package com.wizardlybump17.wlib.command.reader; -import com.wizardlybump17.wlib.command.CommandSender; import com.wizardlybump17.wlib.command.args.reader.ArgsReader; +import com.wizardlybump17.wlib.command.sender.CommandSender; import lombok.NonNull; import org.bukkit.Bukkit; import org.bukkit.entity.Player; diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java index ef9f9b61..46ea8992 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/registered/RegisteredBukkitCommand.java @@ -6,6 +6,7 @@ import com.wizardlybump17.wlib.command.data.CommandData; import com.wizardlybump17.wlib.command.exception.CommandException; import com.wizardlybump17.wlib.command.executor.CommandExecutor; +import com.wizardlybump17.wlib.command.sender.BukkitCommandSender; import org.bukkit.command.Command; import org.bukkit.command.CommandMap; import org.bukkit.command.CommandSender; @@ -38,7 +39,7 @@ public void onRegister(@NotNull CommandManager manager) { @Override public boolean execute(@NotNull CommandSender sender, @NotNull String label, @NotNull String[] args) { try { - manager.execute(new com.wizardlybump17.wlib.command.sender.CommandSender(sender), name + " " + String.join(" ", args)); + manager.execute(new BukkitCommandSender(sender), name + " " + String.join(" ", args)); } catch (CommandException e) { logger.log(Level.SEVERE, "Error while executing a command.", e); } diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java similarity index 87% rename from core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java rename to core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java index 4d25ce26..e1f3fc9a 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/CommandSender.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java @@ -8,11 +8,11 @@ import java.util.UUID; -public class CommandSender implements com.wizardlybump17.wlib.command.CommandSender { +public class BukkitCommandSender implements CommandSender { private final @NotNull org.bukkit.command.CommandSender handle; - public CommandSender(@NotNull org.bukkit.command.CommandSender handle) { + public BukkitCommandSender(@NotNull org.bukkit.command.CommandSender handle) { this.handle = handle; } From 47e9fe107102601532250867dc405af0aac36319 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 23 Nov 2024 21:00:34 -0300 Subject: [PATCH 36/47] added the BasicCommandSender class --- .../command/sender/BasicCommandSender.java | 92 +++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java b/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java new file mode 100644 index 00000000..bc51474e --- /dev/null +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java @@ -0,0 +1,92 @@ +package com.wizardlybump17.wlib.command.sender; + +import org.jetbrains.annotations.NotNull; + +import java.util.Objects; +import java.util.UUID; +import java.util.function.Consumer; +import java.util.function.Predicate; + +public class BasicCommandSender implements CommandSender { + + private final @NotNull S handle; + private final @NotNull String name; + private final @NotNull UUID id; + private final @NotNull Consumer messageConsumer; + private final @NotNull Predicate permissionTest; + + public BasicCommandSender(@NotNull S handle, @NotNull String name, @NotNull UUID id, @NotNull Consumer messageConsumer, @NotNull Predicate permissionTest) { + this.handle = handle; + this.name = name; + this.id = id; + this.messageConsumer = messageConsumer; + this.permissionTest = permissionTest; + } + + @Override + public @NotNull S getHandle() { + return handle; + } + + @Override + public void sendMessage(String message) { + messageConsumer.accept(message); + } + + @Override + public void sendMessage(String... messages) { + sendMessage(String.join("\n", messages)); + } + + @Override + public @NotNull String getName() { + return name; + } + + @Override + public boolean hasPermission(String permission) { + return permissionTest.test(permission); + } + + @Override + public boolean hasId(@NotNull UUID id) { + return id.equals(this.id); + } + + public @NotNull UUID getId() { + return id; + } + + public @NotNull Consumer getMessageConsumer() { + return messageConsumer; + } + + public @NotNull Predicate getPermissionTest() { + return permissionTest; + } + + @Override + public boolean equals(Object o) { + if (o == null || getClass() != o.getClass()) + return false; + BasicCommandSender that = (BasicCommandSender) o; + return Objects.equals(handle, that.handle) && Objects.equals(name, that.name) && Objects.equals(id, that.id) + && Objects.equals(messageConsumer, that.messageConsumer) && Objects.equals(permissionTest, that.permissionTest); + } + + @Override + public int hashCode() { + return Objects.hash(handle, name, id, messageConsumer, permissionTest); + } + + @Override + public String toString() { + return "BasicCommandSender{" + + "handle=" + handle + + ", name='" + name + '\'' + + ", id=" + id + + ", messageConsumer=" + messageConsumer + + ", permissionTest=" + permissionTest + + '}'; + } +} From 5f1c5cd434761d98494d8ab09328682ce19bb109 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 23 Nov 2024 21:05:29 -0300 Subject: [PATCH 37/47] added the builder system --- .../command/sender/BasicCommandSender.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java b/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java index bc51474e..8f2d8f86 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/sender/BasicCommandSender.java @@ -1,6 +1,7 @@ package com.wizardlybump17.wlib.command.sender; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; import java.util.Objects; import java.util.UUID; @@ -89,4 +90,75 @@ public String toString() { ", permissionTest=" + permissionTest + '}'; } + + public static Builder builder() { + return new Builder<>(); + } + + public static class Builder { + + private @Nullable S handle; + private @Nullable String name; + private @Nullable UUID id; + private @Nullable Consumer messageConsumer; + private @Nullable Predicate permissionTest; + + protected Builder() { + } + + public @Nullable S handle() { + return handle; + } + + public @NotNull Builder handle(@Nullable S handle) { + this.handle = handle; + return this; + } + + public @Nullable String name() { + return name; + } + + public @NotNull Builder name(@Nullable String name) { + this.name = name; + return this; + } + + public @Nullable UUID id() { + return id; + } + + public @NotNull Builder id(@Nullable UUID id) { + this.id = id; + return this; + } + + public @Nullable Consumer messageConsumer() { + return messageConsumer; + } + + public @NotNull Builder messageConsumer(@Nullable Consumer messageConsumer) { + this.messageConsumer = messageConsumer; + return this; + } + + public @Nullable Predicate permissionTest() { + return permissionTest; + } + + public @NotNull Builder permissionTest(@Nullable Predicate permissionTest) { + this.permissionTest = permissionTest; + return this; + } + + public @NotNull BasicCommandSender build() { + return new BasicCommandSender<>( + Objects.requireNonNull(handle, "the 'handler' can not be null"), + Objects.requireNonNull(name, "the 'name' can not be null"), + Objects.requireNonNull(id, "the 'id' can not be null"), + messageConsumer == null ? System.out::println : messageConsumer, + permissionTest == null ? permission -> true : permissionTest + ); + } + } } From 986fb3959880ed28d4fd07fa4d124dd99ccf3805 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 23 Nov 2024 21:11:28 -0300 Subject: [PATCH 38/47] added some tests for the BasicCommandSender --- .../test/sender/BasicCommandSenderTests.java | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 commands/src/test/java/com/wizardlybump17/wlib/command/test/sender/BasicCommandSenderTests.java diff --git a/commands/src/test/java/com/wizardlybump17/wlib/command/test/sender/BasicCommandSenderTests.java b/commands/src/test/java/com/wizardlybump17/wlib/command/test/sender/BasicCommandSenderTests.java new file mode 100644 index 00000000..6c66a360 --- /dev/null +++ b/commands/src/test/java/com/wizardlybump17/wlib/command/test/sender/BasicCommandSenderTests.java @@ -0,0 +1,64 @@ +package com.wizardlybump17.wlib.command.test.sender; + +import com.wizardlybump17.wlib.command.sender.BasicCommandSender; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +import java.util.UUID; + +class BasicCommandSenderTests { + + @Test + void testPermissionsAllTrue() { + String name = "Test Sender"; + BasicCommandSender sender = BasicCommandSender.builder() + .handle(new Object()) + .name(name) + .id(UUID.nameUUIDFromBytes(name.getBytes())) + .permissionTest(permission -> true) + .build(); + Assertions.assertTrue(sender.hasPermission("test0.permission0")); + Assertions.assertTrue(sender.hasPermission("test0.permission1")); + Assertions.assertTrue(sender.hasPermission("test0.permission2")); + Assertions.assertTrue(sender.hasPermission("test1.permission0")); + Assertions.assertTrue(sender.hasPermission("test1.permission1")); + Assertions.assertTrue(sender.hasPermission("test1.permission2")); + } + + @Test + void testPermissionsAllFalse() { + String name = "Test Sender"; + BasicCommandSender sender = BasicCommandSender.builder() + .handle(new Object()) + .name(name) + .id(UUID.nameUUIDFromBytes(name.getBytes())) + .permissionTest(permission -> false) + .build(); + Assertions.assertFalse(sender.hasPermission("test0.permission0")); + Assertions.assertFalse(sender.hasPermission("test0.permission1")); + Assertions.assertFalse(sender.hasPermission("test0.permission2")); + Assertions.assertFalse(sender.hasPermission("test1.permission0")); + Assertions.assertFalse(sender.hasPermission("test1.permission1")); + Assertions.assertFalse(sender.hasPermission("test1.permission2")); + } + + @Test + void testSpecificPermissions() { + String name = "Test Sender"; + BasicCommandSender sender = BasicCommandSender.builder() + .handle(new Object()) + .name(name) + .id(UUID.nameUUIDFromBytes(name.getBytes())) + .permissionTest(permission -> permission.startsWith("test0.")) + .build(); + Assertions.assertTrue(sender.hasPermission("test0.permission0")); + Assertions.assertTrue(sender.hasPermission("test0.permission1")); + Assertions.assertTrue(sender.hasPermission("test0.permission2")); + Assertions.assertFalse(sender.hasPermission("test1.permission0")); + Assertions.assertFalse(sender.hasPermission("test1.permission1")); + Assertions.assertFalse(sender.hasPermission("test1.permission2")); + Assertions.assertFalse(sender.hasPermission("test2.permission0")); + Assertions.assertFalse(sender.hasPermission("test2.permission1")); + Assertions.assertFalse(sender.hasPermission("test2.permission2")); + } +} From f63124803de8db0de319fb309ba50be6f77c2f7c Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 4 Dec 2024 23:07:40 -0300 Subject: [PATCH 39/47] added the annotations dependency to the test scope --- commands/build.gradle | 1 + 1 file changed, 1 insertion(+) diff --git a/commands/build.gradle b/commands/build.gradle index f57b9327..2497418c 100644 --- a/commands/build.gradle +++ b/commands/build.gradle @@ -11,6 +11,7 @@ dependencies { implementation("com.github.sisyphsu:dateparser:1.0.11") { exclude(group: "org.projectlombok", module: "lombok") } + testCompileOnly("org.jetbrains:annotations:26.0.1") } shadowJar { From fe6cbc3e2defb63c000640e1fadf271b60b899cc Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Tue, 14 Jan 2025 18:06:50 -0300 Subject: [PATCH 40/47] version changed --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index b3f35803..c3dc551d 100644 --- a/build.gradle +++ b/build.gradle @@ -7,7 +7,7 @@ apply plugin: 'maven-publish' allprojects { group = 'com.wizardlybump17.wlib' - version = '1.6.7' + version = '1.6.7-ncs' apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'java' From f3d9f582c2c100925c535df9565b749f46b24117 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Tue, 14 Jan 2025 18:09:51 -0300 Subject: [PATCH 41/47] improved the build.gradle --- build.gradle | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/build.gradle b/build.gradle index c3dc551d..dd316298 100644 --- a/build.gradle +++ b/build.gradle @@ -3,14 +3,13 @@ plugins { id 'maven-publish' } -apply plugin: 'maven-publish' - allprojects { - group = 'com.wizardlybump17.wlib' - version = '1.6.7-ncs' - apply plugin: 'com.github.johnrengelman.shadow' apply plugin: 'java' + apply plugin: 'maven-publish' + + group = 'com.wizardlybump17.wlib' + version = '1.6.7-ncs' repositories { mavenLocal() @@ -24,6 +23,10 @@ allprojects { compileJava { options.encoding = 'UTF-8' } + + assemble { + dependsOn(shadowJar) + } } java { @@ -32,8 +35,6 @@ allprojects { } subprojects { - apply plugin: 'maven-publish' - if (project.name == 'versions' || project.name.matches('v\\d_\\d+_R\\d+')) { tasks.withType(PublishToMavenRepository).configureEach { it.enabled = false @@ -79,8 +80,4 @@ subprojects { events "passed", "skipped", "failed", "standardOut", "standardError" } } - - build { - dependsOn(test, shadowJar) - } } \ No newline at end of file From 6017fed0990fc5bcd79f72643537dfb31f86b7d5 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 15 Feb 2025 17:54:56 -0300 Subject: [PATCH 42/47] added the MiniMessageUtil class --- bukkit-utils/build.gradle | 3 ++ .../wlib/util/bukkit/MiniMessageUtil.java | 39 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 bukkit-utils/src/main/java/com/wizardlybump17/wlib/util/bukkit/MiniMessageUtil.java diff --git a/bukkit-utils/build.gradle b/bukkit-utils/build.gradle index 00e702b8..970dd5b2 100644 --- a/bukkit-utils/build.gradle +++ b/bukkit-utils/build.gradle @@ -4,6 +4,9 @@ dependencies { 'org.spigotmc:spigot:1.17.1-R0.1-SNAPSHOT', 'org.jetbrains:annotations:23.0.0', ) + compileOnly("net.kyori:adventure-api:4.18.0") + compileOnly("net.kyori:adventure-text-minimessage:4.18.0") + compileOnly("net.kyori:adventure-platform-bukkit:4.3.4") implementation( project(':utils') ) diff --git a/bukkit-utils/src/main/java/com/wizardlybump17/wlib/util/bukkit/MiniMessageUtil.java b/bukkit-utils/src/main/java/com/wizardlybump17/wlib/util/bukkit/MiniMessageUtil.java new file mode 100644 index 00000000..6cbc701a --- /dev/null +++ b/bukkit-utils/src/main/java/com/wizardlybump17/wlib/util/bukkit/MiniMessageUtil.java @@ -0,0 +1,39 @@ +package com.wizardlybump17.wlib.util.bukkit; + +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.minimessage.MiniMessage; +import net.kyori.adventure.text.minimessage.tag.Tag; +import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver; +import org.jetbrains.annotations.NotNull; + +import java.util.Map; + +public final class MiniMessageUtil { + + private MiniMessageUtil() { + } + + public static @NotNull Component getMessage(@NotNull String message, @NotNull Map placeholders) { + MiniMessage miniMessage = MiniMessage.miniMessage(); + if (placeholders.isEmpty()) + return miniMessage.deserialize(message); + + TagResolver[] resolvers = new TagResolver[placeholders.size()]; + int resolverIndex = 0; + for (Map.Entry entry : placeholders.entrySet()) { + String key = entry.getKey(); + Object value = entry.getValue(); + resolvers[resolverIndex++] = TagResolver.builder() + .tag( + key, + Tag.inserting(value instanceof Component component ? component : Component.text(String.valueOf(value))) + ) + .build(); + } + return miniMessage.deserialize(message, resolvers); + } + + public static @NotNull Component getMessage(@NotNull String message) { + return getMessage(message, Map.of()); + } +} From 1384fd877e5c392fa362cb51f002a506bf0555c5 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 15 Feb 2025 22:27:26 -0300 Subject: [PATCH 43/47] added support for the Adventure API on non-Paper environments --- core/build.gradle | 3 +++ .../main/java/com/wizardlybump17/wlib/WLib.java | 14 ++++++++++++++ .../wlib/command/sender/BukkitCommandSender.java | 11 +++++++++++ core/src/main/resources/plugin.yml | 6 +++++- 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/core/build.gradle b/core/build.gradle index 776fb140..b31666b8 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -4,6 +4,9 @@ dependencies { 'org.jetbrains:annotations:23.1.0', 'org.spigotmc:spigot:1.17.1-R0.1-SNAPSHOT', ) + compileOnly("net.kyori:adventure-api:4.18.0") + compileOnly("net.kyori:adventure-text-minimessage:4.18.0") + compileOnly("net.kyori:adventure-platform-bukkit:4.3.4") annotationProcessor('org.projectlombok:lombok:1.18.24') implementation( diff --git a/core/src/main/java/com/wizardlybump17/wlib/WLib.java b/core/src/main/java/com/wizardlybump17/wlib/WLib.java index e4905f1f..bf90d6db 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/WLib.java +++ b/core/src/main/java/com/wizardlybump17/wlib/WLib.java @@ -29,15 +29,18 @@ import com.wizardlybump17.wlib.util.bukkit.particle.*; import lombok.Getter; import lombok.NonNull; +import net.kyori.adventure.platform.bukkit.BukkitAudiences; import org.bukkit.Bukkit; import org.bukkit.configuration.serialization.ConfigurationSerialization; import org.bukkit.event.HandlerList; import org.bukkit.plugin.java.JavaPlugin; +import org.jetbrains.annotations.NotNull; @Getter public class WLib extends JavaPlugin { private final SaveControllersTask saveControllersTask = new SaveControllersTask(getLogger()); + private BukkitAudiences audiences; @Override public void onLoad() { @@ -64,6 +67,8 @@ protected void initConfigs() { @Override public void onEnable() { + audiences = BukkitAudiences.create(this); + Bukkit.getPluginManager().registerEvents(new EntityListener(), this); Bukkit.getPluginManager().registerEvents(new PlayerListener(this), this); @@ -76,6 +81,11 @@ public void onEnable() { public void onDisable() { HandlerList.unregisterAll(this); saveControllersTask.cancel(); + + if (audiences != null) { + audiences.close(); + audiences = null; + } } private void initCommandSystem() { @@ -205,4 +215,8 @@ public static WLib getInstance() { public static @NonNull String getServerVersion() { return Bukkit.getServer().getClass().getName().split("\\.")[3]; } + + public @NotNull BukkitAudiences getAudiences() { + return audiences; + } } diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java index e1f3fc9a..d0b7f928 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java @@ -1,11 +1,14 @@ package com.wizardlybump17.wlib.command.sender; +import com.wizardlybump17.wlib.WLib; +import com.wizardlybump17.wlib.util.bukkit.MiniMessageUtil; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Entity; import org.bukkit.entity.Player; import org.jetbrains.annotations.NotNull; +import java.util.Map; import java.util.UUID; public class BukkitCommandSender implements CommandSender { @@ -31,6 +34,14 @@ public void sendMessage(String... message) { handle.sendMessage(String.join("\n", message)); } + public void sendMiniMessage(@NotNull String message, @NotNull Map placeholders) { + WLib.getInstance().getAudiences().sender(handle).sendMessage(MiniMessageUtil.getMessage(message, placeholders)); + } + + public void sendMiniMessage(@NotNull String message) { + sendMiniMessage(message, Map.of()); + } + @Override public String getName() { return handle.getName(); diff --git a/core/src/main/resources/plugin.yml b/core/src/main/resources/plugin.yml index dee76513..35676d43 100644 --- a/core/src/main/resources/plugin.yml +++ b/core/src/main/resources/plugin.yml @@ -3,4 +3,8 @@ main: com.wizardlybump17.wlib.WLib version: '${version}' author: WizardlyBump17 api-version: '1.13' -load: STARTUP \ No newline at end of file +load: STARTUP +libraries: + - net.kyori:adventure-api:4.18.0 + - net.kyori:adventure-text-minimessage:4.18.0 + - net.kyori:adventure-platform-bukkit:4.3.4 From 618c6ce3c90150644a25aa32ba3db1c1387c21ae Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 15 Feb 2025 22:54:41 -0300 Subject: [PATCH 44/47] added some methods with some Adventure stuff --- .../command/sender/BukkitCommandSender.java | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java index d0b7f928..d13d6355 100644 --- a/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java +++ b/core/src/main/java/com/wizardlybump17/wlib/command/sender/BukkitCommandSender.java @@ -2,6 +2,9 @@ import com.wizardlybump17.wlib.WLib; import com.wizardlybump17.wlib.util.bukkit.MiniMessageUtil; +import net.kyori.adventure.chat.ChatType; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.ComponentLike; import org.bukkit.command.BlockCommandSender; import org.bukkit.command.ConsoleCommandSender; import org.bukkit.entity.Entity; @@ -42,6 +45,22 @@ public void sendMiniMessage(@NotNull String message) { sendMiniMessage(message, Map.of()); } + public void sendMessage(@NotNull Component message) { + WLib.getInstance().getAudiences().sender(handle).sendMessage(message); + } + + public void sendMessage(@NotNull Component message, @NotNull ChatType.Bound type) { + WLib.getInstance().getAudiences().sender(handle).sendMessage(message, type); + } + + public void sendMessage(@NotNull ComponentLike message) { + WLib.getInstance().getAudiences().sender(handle).sendMessage(message); + } + + public void sendMessage(@NotNull ComponentLike message, @NotNull ChatType.Bound type) { + WLib.getInstance().getAudiences().sender(handle).sendMessage(message, type); + } + @Override public String getName() { return handle.getName(); From 8c12af809cb5e45d644e500c72fe419f6ecd7dc3 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Wed, 16 Apr 2025 23:59:22 -0300 Subject: [PATCH 45/47] version changed :D --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index dd316298..4be88fab 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ allprojects { apply plugin: 'maven-publish' group = 'com.wizardlybump17.wlib' - version = '1.6.7-ncs' + version = '1.6.7-ncs-1' repositories { mavenLocal() From 756ca770fe343e4ec625cea2c08026adc375aac8 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 24 May 2025 14:23:02 -0300 Subject: [PATCH 46/47] using suppliers --- .../wlib/command/data/SimpleCommandData.java | 131 +++++++++++++----- 1 file changed, 98 insertions(+), 33 deletions(-) diff --git a/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java b/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java index f26df07f..66905c05 100644 --- a/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java +++ b/commands/src/main/java/com/wizardlybump17/wlib/command/data/SimpleCommandData.java @@ -3,19 +3,19 @@ import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Objects; +import java.util.function.Supplier; public class SimpleCommandData extends CommandData { - private final @NotNull String execution; - private final @Nullable String permission; - private final @Nullable String permissionMessage; - private final int priority; - private final @Nullable String description; - private final @Nullable String invalidSenderMessage; - private final @NotNull Class senderType; + private final @NotNull Supplier execution; + private final @NotNull Supplier permission; + private final @NotNull Supplier permissionMessage; + private final @NotNull Supplier priority; + private final @NotNull Supplier description; + private final @NotNull Supplier invalidSenderMessage; + private final @NotNull Supplier> senderType; - public SimpleCommandData(@NotNull String execution, @Nullable String permission, @Nullable String permissionMessage, int priority, @Nullable String description, @Nullable String invalidSenderMessage, @NotNull Class senderType) { + public SimpleCommandData(@NotNull Supplier execution, @NotNull Supplier permission, @NotNull Supplier permissionMessage, @NotNull Supplier priority, @NotNull Supplier description, @NotNull Supplier invalidSenderMessage, @NotNull Supplier> senderType) { this.execution = execution; this.permission = permission; this.permissionMessage = permissionMessage; @@ -25,39 +25,51 @@ public SimpleCommandData(@NotNull String execution, @Nullable String permission, this.senderType = senderType; } + public SimpleCommandData(@NotNull String execution, @Nullable String permission, @Nullable String permissionMessage, int priority, @Nullable String description, @Nullable String invalidSenderMessage, @NotNull Class senderType) { + this( + () -> execution, + () -> permission, + () -> permissionMessage, + () -> priority, + () -> description, + () -> invalidSenderMessage, + () -> senderType + ); + } + @Override public @NotNull String getExecution() { - return execution; + return execution.get(); } @Override public @Nullable String getPermission() { - return permission; + return permission.get(); } @Override public @Nullable String getPermissionMessage() { - return permissionMessage; + return permissionMessage.get(); } @Override public int getPriority() { - return priority; + return priority.get(); } @Override public @Nullable String getDescription() { - return description; + return description.get(); } @Override public @Nullable String getInvalidSenderMessage() { - return invalidSenderMessage; + return invalidSenderMessage.get(); } @Override public @NotNull Class getSenderType() { - return senderType; + return senderType.get(); } public static class Builder { @@ -65,86 +77,139 @@ public static class Builder { Builder() { } - private @Nullable String execution; - private @Nullable String permission; - private @Nullable String permissionMessage; - private @Nullable Integer priority; - private @Nullable String description; - private @Nullable String invalidSenderMessage; - private @Nullable Class senderType; + private @Nullable Supplier execution; + private @Nullable Supplier permission; + private @Nullable Supplier permissionMessage; + private @Nullable Supplier priority; + private @Nullable Supplier description; + private @Nullable Supplier invalidSenderMessage; + private @Nullable Supplier> senderType; public @Nullable String execution() { - return execution; + return execution == null ? null : execution.get(); } public @NotNull Builder execution(@Nullable String execution) { + this.execution = () -> execution; + return this; + } + + public @NotNull Builder execution(@Nullable Supplier execution) { this.execution = execution; return this; } public @Nullable String permission() { - return permission; + return permission == null ? null : permission.get(); } public @NotNull Builder permission(@Nullable String permission) { + this.permission = () -> permission; + return this; + } + + public @NotNull Builder permission(@Nullable Supplier permission) { this.permission = permission; return this; } public @Nullable String permissionMessage() { - return permissionMessage; + return permissionMessage == null ? null : permissionMessage.get(); } public @NotNull Builder permissionMessage(@Nullable String permissionMessage) { + this.permissionMessage = () -> permissionMessage; + return this; + } + + public @NotNull Builder permissionMessage(@Nullable Supplier permissionMessage) { this.permissionMessage = permissionMessage; return this; } public @Nullable Integer priority() { - return priority; + return priority == null ? null : priority.get(); } public @NotNull Builder priority(@Nullable Integer priority) { + this.priority = () -> priority; + return this; + } + + public @NotNull Builder priority(@Nullable Supplier priority) { this.priority = priority; return this; } public @Nullable String description() { - return description; + return description == null ? null : description.get(); } public @NotNull Builder description(@Nullable String description) { + this.description = () -> description; + return this; + } + + public @NotNull Builder description(@Nullable Supplier description) { this.description = description; return this; } public @Nullable String invalidSenderMessage() { - return invalidSenderMessage; + return invalidSenderMessage == null ? null : invalidSenderMessage.get(); } public @NotNull Builder invalidSenderMessage(@Nullable String invalidSenderMessage) { + this.invalidSenderMessage = () -> invalidSenderMessage; + return this; + } + + public @NotNull Builder invalidSenderMessage(@Nullable Supplier invalidSenderMessage) { this.invalidSenderMessage = invalidSenderMessage; return this; } public @Nullable Class senderType() { - return senderType; + return senderType == null ? null : senderType.get(); } public @NotNull Builder senderType(@Nullable Class senderType) { + this.senderType = () -> senderType; + return this; + } + + public @NotNull Builder senderType(@Nullable Supplier> senderType) { this.senderType = senderType; return this; } public @NotNull CommandData build() { + String execution = execution(); + if (execution == null) + throw new NullPointerException("The execution can not be null"); + + String permission = permission(); + String permissionMessage = permissionMessage(); + + Integer priority = priority(); + if (priority == null) + priority = execution.split(" ").length; + + String description = description(); + String invalidSenderMessage = invalidSenderMessage(); + + Class senderType = senderType(); + if (senderType == null) + senderType = Object.class; + return new SimpleCommandData( - Objects.requireNonNull(execution, "The execution can not be null"), + execution, permission, permissionMessage, - priority == null ? execution.split(" ").length : priority, + priority, description, invalidSenderMessage, - senderType == null ? Object.class : senderType + senderType ); } } From 5d9087eb361fb3d24ef68210a85f2b63796c5517 Mon Sep 17 00:00:00 2001 From: WizardlyBump17 Date: Sat, 24 May 2025 17:59:40 -0300 Subject: [PATCH 47/47] version changed --- build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/build.gradle b/build.gradle index 4be88fab..2809708c 100644 --- a/build.gradle +++ b/build.gradle @@ -9,7 +9,7 @@ allprojects { apply plugin: 'maven-publish' group = 'com.wizardlybump17.wlib' - version = '1.6.7-ncs-1' + version = '1.6.7-ncs-2' repositories { mavenLocal()