From 694c374eb714f73c4b471b6041af32354c586a8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jozef=20Steinh=C3=BCbl?= Date: Mon, 24 Jun 2024 22:50:58 +0200 Subject: [PATCH] feat: map args --- bukkittest/build.gradle.kts | 4 ++-- .../common/command/CommandManager.java | 5 +++-- .../command/abstraction/ParentCommand.java | 4 +++- .../common/command/argument/Arguments.java | 22 +++++++++++++++---- .../sponge/SpongeLightEcoBootstrap.java | 6 +++++ 5 files changed, 32 insertions(+), 9 deletions(-) diff --git a/bukkittest/build.gradle.kts b/bukkittest/build.gradle.kts index 545c292..e30be23 100644 --- a/bukkittest/build.gradle.kts +++ b/bukkittest/build.gradle.kts @@ -6,8 +6,8 @@ plugins { } server { - version = "1.20.1" - type = "paper" + version = "1.20.6" + type = "purpur" } repositories { diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java index 0e3638e..d6e99d0 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/CommandManager.java @@ -36,13 +36,14 @@ public class CommandManager { public void execute(CommandSender sender, String name, String[] args) { Command command = this.commands.stream() .filter(cmd -> cmd.getName().equalsIgnoreCase(name) && cmd.getArgs().size() == args.length) - .findFirst().orElse(null); + .findFirst() + .orElse(null); if (command == null) { sender.sendMessage(Component.text("Command not found.")); // TODO: change return; } - command.execute(this.plugin, sender, new Arguments(List.of(args))); + command.execute(this.plugin, sender, new Arguments(plugin, command, List.of(args))); } } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/ParentCommand.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/ParentCommand.java index 9363afb..2b64ae9 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/ParentCommand.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/abstraction/ParentCommand.java @@ -5,6 +5,7 @@ import dev.xhyrom.lighteco.common.command.argument.type.StringArgument; import dev.xhyrom.lighteco.common.model.chat.CommandSender; import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; import lombok.Getter; +import net.kyori.adventure.text.Component; import org.checkerframework.checker.nullness.qual.NonNull; import org.checkerframework.checker.nullness.qual.Nullable; @@ -24,11 +25,12 @@ public class ParentCommand extends Command { public void execute(LightEcoPlugin plugin, CommandSender sender, Arguments args) { String childName = args.string("child"); Command child = Arrays.stream(getChildren()) - .filter(cmd -> cmd.getName().equalsIgnoreCase(childName)) + .filter(cmd -> cmd.getName().equalsIgnoreCase(childName) && cmd.getArgs().size() == args.size() - 1) .findFirst() .orElse(null); if (child == null) { + sender.sendMessage(Component.text("Command not found. / Parent Command")); return; } diff --git a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/Arguments.java b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/Arguments.java index 00f9916..5720576 100644 --- a/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/Arguments.java +++ b/common/src/main/java/dev/xhyrom/lighteco/common/command/argument/Arguments.java @@ -1,18 +1,28 @@ package dev.xhyrom.lighteco.common.command.argument; +import dev.xhyrom.lighteco.common.command.abstraction.Command; import dev.xhyrom.lighteco.common.model.user.User; +import dev.xhyrom.lighteco.common.plugin.LightEcoPlugin; +import java.util.HashMap; import java.util.List; import java.util.Map; public class Arguments { + private final LightEcoPlugin plugin; + private final Command command; private final List arguments; - private final Map mappedArguments; + private final Map mappedArguments = new HashMap<>(); - public Arguments(List arguments) { + public Arguments(LightEcoPlugin plugin, Command command, List arguments) { + this.plugin = plugin; + this.command = command; this.arguments = arguments; - this.mappedArguments = Map.of(); + for (int i = 0; i < arguments.size(); i++) { + String arg = arguments.get(i); + this.mappedArguments.put(command.getArgs().get(i).getName(), (Object) command.getArgs().get(i).parse(plugin, arg)); + } } public String string(String name) { @@ -35,7 +45,11 @@ public class Arguments { return (String) this.arguments.get(index); } + public int size() { + return this.arguments.size(); + } + public Arguments subList(int fromIndex) { - return new Arguments(this.arguments.subList(fromIndex, this.arguments.size())); + return new Arguments(this.plugin, this.command, this.arguments.subList(fromIndex, this.arguments.size())); } } diff --git a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java index ab281f0..4c5c55a 100644 --- a/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java +++ b/sponge-8/src/main/java/dev/xhyrom/lighteco/sponge/SpongeLightEcoBootstrap.java @@ -12,6 +12,7 @@ import org.spongepowered.plugin.builtin.jvm.Plugin; import java.io.InputStream; import java.nio.file.Path; import java.util.List; +import java.util.Optional; import java.util.UUID; @Plugin("lighteco-sponge") @@ -60,6 +61,11 @@ public class SpongeLightEcoBootstrap implements LightEcoBootstrap, LoaderBootstr return null; } + @Override + public Optional lookupUniqueId(String username) { + return Optional.empty(); + } + @Override public boolean isPlayerOnline(UUID uniqueId) { return false;