From fd94af0bdfe656b1a113204fae57a5fb12da3a8d Mon Sep 17 00:00:00 2001 From: Robin Cheney Date: Tue, 25 Nov 2025 09:34:52 +0100 Subject: [PATCH] test --- .../utils/io/error/InvalidArgumentsError.java | 5 +- .../utils/io/input/AvailableCommands.java | 7 +- .../ddnss/eternal/utils/io/input/Command.java | 32 ++++--- .../eternal/utils/io/input/CommandOption.java | 4 +- .../utils/io/input/GenericCommand.java | 92 +++++++++++++++++++ .../utils/io/input/GenericCommandOption.java | 23 +++++ .../utils/io/input/user/CMDCommands.java | 4 +- 7 files changed, 143 insertions(+), 24 deletions(-) create mode 100644 src/main/java/de/ddnss/eternal/utils/io/input/GenericCommand.java create mode 100644 src/main/java/de/ddnss/eternal/utils/io/input/GenericCommandOption.java diff --git a/src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java b/src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java index cb200c0..0eb07bb 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java +++ b/src/main/java/de/ddnss/eternal/utils/io/error/InvalidArgumentsError.java @@ -1,9 +1,10 @@ package de.ddnss.eternal.utils.io.error; -import de.ddnss.eternal.utils.io.input.CommandOption; +import de.ddnss.eternal.utils.io.input.GenericCommandOption; /** - * Thrown when an {@link CommandOption} object does not fit the requirements of + * Thrown when an {@link GenericCommandOption} object does not fit the + * requirements of * the function it was passed to * * @author Robin Cheney diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java b/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java index 6dbf500..4da656e 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/AvailableCommands.java @@ -14,7 +14,7 @@ public enum AvailableCommands implements CommandEnumInterface { System.exit(0); }), - SAY((CommandOption args) -> { + SAY((CommandOption args) -> { System.out.println("HI"); System.out.println(args.get("SAY")); return null; @@ -35,7 +35,8 @@ public enum AvailableCommands implements CommandEnumInterface { * @param command A {@link Function} that returns null * @param arguments A {@link CommandOption} object */ - private AvailableCommands(Function, Void> command, CommandOption arguments) { + private AvailableCommands(Function command, + CommandOption arguments) { this.command = new Command(command, arguments); } @@ -44,7 +45,7 @@ public enum AvailableCommands implements CommandEnumInterface { * * @param command A {@link Function} that returns null */ - private AvailableCommands(Function, Void> command) { + private AvailableCommands(Function command) { this.command = new Command(command); } diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/Command.java b/src/main/java/de/ddnss/eternal/utils/io/input/Command.java index fe83474..7eef2c5 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/Command.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/Command.java @@ -15,26 +15,26 @@ public class Command { /** * The command to be executed. Must be passed as a runnable to the constructor * or if parameters are necessary, it must be a {@link Function} that takes a - * {@link CommandOption} as the single parameter and always returns null + * {@link GenericCommandOption} as the single parameter and always returns null */ - final Function, Void> command; - CommandOption arguments; + final Function command; + CommandOption arguments; /** * * @param command * @param arguments */ - public Command(Function, Void> command, CommandOption arguments) { + public Command(Function command, CommandOption arguments) { this.command = command; this.arguments = arguments; } /** * - * @param command A {@link Function} that takes a {@link CommandOption} + * @param command A {@link Function} that takes a {@link GenericCommandOption} */ - public Command(Function, Void> command) { + public Command(Function command) { this.command = command; } @@ -44,7 +44,7 @@ public class Command { * @param command A {@link Runnable} to execute */ public Command(Runnable command) { - this.command = (CommandOption undefined) -> { + this.command = (CommandOption undefined) -> { command.run(); return null; }; @@ -54,9 +54,9 @@ public class Command { * Execute the command with the previously provided arguments * * @apiNote This method has a shorthand property where you can provide - * {@link CommandOption command options} - * @see #exec(CommandOption) - * @see #bindArguments(CommandOption) + * {@link GenericCommandOption command options} + * @see #exec(GenericCommandOption) + * @see #bindArguments(GenericCommandOption) */ public void exec() { this.command.apply(arguments); @@ -65,22 +65,24 @@ public class Command { /** * Binds arguments to the command * - * @param args A {@link CommandOption} object holding the command arguments and + * @param args A {@link GenericCommandOption} object holding the command + * arguments and * their values * @return the {@link Command} object itself */ - public Command bindArguments(CommandOption args) { + public Command bindArguments(CommandOption args) { this.arguments = args; return this; }; /** - * Executes a command with a {@link CommandOption} + * Executes a command with a {@link GenericCommandOption} * - * @param arguments A {@link CommandOption} object holding necessary parameters + * @param arguments A {@link GenericCommandOption} object holding necessary + * parameters * @throws InvalidArgumentsError */ - public void exec(CommandOption arguments) throws InvalidArgumentsError { + public void exec(CommandOption arguments) throws InvalidArgumentsError { try { this.command.apply(arguments); } catch (NullPointerException String) { diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/CommandOption.java b/src/main/java/de/ddnss/eternal/utils/io/input/CommandOption.java index ac55e28..8155264 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/CommandOption.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/CommandOption.java @@ -11,8 +11,8 @@ import java.util.HashMap; * * @since 1.1 */ -public class CommandOption extends HashMap { - public CommandOption(String string, E e) { +public class CommandOption extends GenericCommandOption { + public CommandOption(String string, String e) { super(); this.put(string, e); } diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/GenericCommand.java b/src/main/java/de/ddnss/eternal/utils/io/input/GenericCommand.java new file mode 100644 index 0000000..f59477b --- /dev/null +++ b/src/main/java/de/ddnss/eternal/utils/io/input/GenericCommand.java @@ -0,0 +1,92 @@ +package de.ddnss.eternal.utils.io.input; + +import java.util.function.Function; + +import de.ddnss.eternal.utils.io.error.InvalidArgumentsError; + +/** + * Class for instantiating commands with parameter type {@code String} + * + * @author Robin Cheney + * + * @since 1.2 + */ +public class GenericCommand { + /** + * The command to be executed. Must be passed as a runnable to the constructor + * or if parameters are necessary, it must be a {@link Function} that takes a + * {@link GenericCommandOption} as the single parameter and always returns null + */ + final Function, Void> command; + GenericCommandOption arguments; + + /** + * + * @param command + * @param arguments + */ + public GenericCommand(Function, Void> command, GenericCommandOption arguments) { + this.command = command; + this.arguments = arguments; + } + + /** + * + * @param command A {@link Function} that takes a {@link GenericCommandOption} + */ + public GenericCommand(Function, Void> command) { + this.command = command; + } + + /** + * Constructs a {@link GenericCommand} without any parameters + * + * @param command A {@link Runnable} to execute + */ + public GenericCommand(Runnable command) { + this.command = (GenericCommandOption undefined) -> { + command.run(); + return null; + }; + } + + /** + * Execute the command with the previously provided arguments + * + * @apiNote This method has a shorthand property where you can provide + * {@link GenericCommandOption command options} + * @see #exec(GenericCommandOption) + * @see #bindArguments(GenericCommandOption) + */ + public void exec() { + this.command.apply(arguments); + }; + + /** + * Binds arguments to the command + * + * @param args A {@link GenericCommandOption} object holding the command + * arguments and + * their values + * @return the {@link GenericCommand} object itself + */ + public GenericCommand bindArguments(GenericCommandOption args) { + this.arguments = args; + return this; + }; + + /** + * Executes a command with a {@link GenericCommandOption} + * + * @param arguments A {@link GenericCommandOption} object holding necessary + * parameters + * @throws InvalidArgumentsError + */ + public void exec(GenericCommandOption arguments) throws InvalidArgumentsError { + try { + this.command.apply(arguments); + } catch (NullPointerException String) { + throw new InvalidArgumentsError(String); + } + }; +} \ No newline at end of file diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/GenericCommandOption.java b/src/main/java/de/ddnss/eternal/utils/io/input/GenericCommandOption.java new file mode 100644 index 0000000..f31ca58 --- /dev/null +++ b/src/main/java/de/ddnss/eternal/utils/io/input/GenericCommandOption.java @@ -0,0 +1,23 @@ +package de.ddnss.eternal.utils.io.input; + +import java.util.HashMap; + +/** + * Command options container + * + * @apiNote extends {@link HashMap} whose functions you can use + * + * @author Robin Cheney + * + * @since 1.2 + */ +public class GenericCommandOption extends HashMap { + public GenericCommandOption(String string, E e) { + super(); + this.put(string, e); + } + + public GenericCommandOption() { + super(); + } +} \ No newline at end of file diff --git a/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java b/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java index 567c0e5..eda694e 100644 --- a/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java +++ b/src/main/java/de/ddnss/eternal/utils/io/input/user/CMDCommands.java @@ -3,9 +3,9 @@ package de.ddnss.eternal.utils.io.input.user; import java.util.Scanner; import de.ddnss.eternal.utils.io.input.AvailableCommands; -import de.ddnss.eternal.utils.io.input.CommandOption; import de.ddnss.eternal.utils.io.input.Command; import de.ddnss.eternal.utils.io.input.CommandEnumInterface; +import de.ddnss.eternal.utils.io.input.CommandOption; /** * Should be useful for capturing console command @@ -45,7 +45,7 @@ public class CMDCommands { break; } - command.getCommand().bindArguments(new CommandOption("SAY", parts[0])).exec(); + command.getCommand().bindArguments(new CommandOption("SAY", parts[0])).exec(); } } } -- 2.43.0