From 6a9cc29445c6d0fc7902602aa97ad34e957c4100 Mon Sep 17 00:00:00 2001 From: Mats Date: Tue, 9 Jul 2024 23:46:26 +0200 Subject: [PATCH] start implementing additional command flags --- .../commands/helpers/CommandParameters.java | 26 ++++++++++ .../interfaces/ParameteredSubCommand.java | 51 +++++++++++++++++++ 2 files changed, 77 insertions(+) create mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/helpers/CommandParameters.java create mode 100644 plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/ParameteredSubCommand.java diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/helpers/CommandParameters.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/helpers/CommandParameters.java new file mode 100644 index 0000000000..594ee2936f --- /dev/null +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/helpers/CommandParameters.java @@ -0,0 +1,26 @@ +package com.craftmend.openaudiomc.generic.commands.helpers; + +import java.util.HashMap; +import java.util.Map; + +public class CommandParameters { + + private Map parameters = new HashMap<>(); + + public CommandParameters(Map parameters) { + this.parameters = parameters; + } + + public String getParameter(String key) { + return parameters.get(key); + } + + public String getParameterOrDefault(String key, String defaultValue) { + return parameters.getOrDefault(key, defaultValue); + } + + public boolean hasParameter(String key) { + return parameters.containsKey(key); + } + +} diff --git a/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/ParameteredSubCommand.java b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/ParameteredSubCommand.java new file mode 100644 index 0000000000..0ad212a884 --- /dev/null +++ b/plugin/src/main/java/com/craftmend/openaudiomc/generic/commands/interfaces/ParameteredSubCommand.java @@ -0,0 +1,51 @@ +package com.craftmend.openaudiomc.generic.commands.interfaces; + +import com.craftmend.openaudiomc.generic.commands.helpers.CommandParameters; +import com.craftmend.openaudiomc.generic.user.User; + +import java.util.HashMap; +import java.util.Map; + +public abstract class ParameteredSubCommand extends SubCommand { + + public ParameteredSubCommand(String argument) { + super(argument); + } + + public ParameteredSubCommand(String argument, String... aliases) { + super(argument, aliases); + } + + @Override + public void onExecute(User sender, String[] args) { + // go over all args, and find ones that start with -- + // these are parameters, and should be parsed into a map + // we'll then reconstruct the args array without the parameters + Map parameters = new HashMap<>(); + + for (int i = 0; i < args.length; i++) { + String arg = args[i]; + if (arg.startsWith("--") && arg.contains("=")) { + String parameter = arg.substring(2); + String[] split = parameter.split("="); + String key = split[0]; + String value = split[1]; + parameters.put(key, value); + } + } + + // remove all parameters from the args array + String[] newArgs = new String[args.length - parameters.size()]; + int j = 0; + for (int i = 0; i < args.length; i++) { + if (!args[i].startsWith("--")) { + newArgs[j] = args[i]; + j++; + } + } + + onExecute(sender, newArgs, new CommandParameters(parameters)); + } + + public abstract void onExecute(User sender, String[] args, CommandParameters parameters); +}