Merge pull request #2997 from Multiverse/better-flag-handling

Better flag handling
This commit is contained in:
Ben Woo 2023-09-09 20:05:21 +08:00 committed by GitHub
commit c71bdd83d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 73 additions and 2 deletions

View File

@ -1,9 +1,12 @@
package com.onarandombox.MultiverseCore.commandtools;
import co.aikar.commands.BaseCommand;
import com.dumptruckman.minecraft.util.Logging;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlag;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagGroup;
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagsManager;
import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags;
import jakarta.annotation.PostConstruct;
import org.jetbrains.annotations.NotNull;
import org.jvnet.hk2.annotations.Contract;
@ -12,21 +15,39 @@ public abstract class MultiverseCommand extends BaseCommand {
protected final MVCommandManager commandManager;
private String flagGroupName;
private CommandFlagGroup.Builder flagGroupBuilder;
protected MultiverseCommand(@NotNull MVCommandManager commandManager) {
this.commandManager = commandManager;
}
@PostConstruct
private void postConstruct() {
if (flagGroupBuilder != null) {
registerFlagGroup(flagGroupBuilder.build());
flagGroupBuilder = null;
}
}
protected CommandFlagsManager getFlagsManager() {
return commandManager.getFlagsManager();
}
protected CommandFlag flag(CommandFlag flag) {
if (flagGroupBuilder == null) {
flagGroupBuilder = CommandFlagGroup.builder("mv" + getClass().getSimpleName().toLowerCase());
}
flagGroupBuilder.add(flag);
return flag;
}
protected void registerFlagGroup(@NotNull CommandFlagGroup flagGroup) {
if (flagGroupName != null) {
throw new IllegalStateException("Flag group already registered! (name: " + flagGroupName + ")");
}
getFlagsManager().registerFlagGroup(flagGroup);
flagGroupName = flagGroup.getName();
Logging.fine("Registered flag group: " + flagGroupName);
}
protected @NotNull ParsedCommandFlags parseFlags(@NotNull String[] flags) {

View File

@ -15,7 +15,7 @@ public class ParsedCommandFlags
private final Map<String, Object> flagValues;
public ParsedCommandFlags() {
ParsedCommandFlags() {
flagValues = new HashMap<>();
}
@ -29,6 +29,16 @@ public class ParsedCommandFlags
flagValues.put(key, value);
}
/**
* Check if a flag is present.
*
* @param flag The flag to check.
* @return True if the flag is present, false otherwise.
*/
public boolean hasFlag(@NotNull CommandFlag flag) {
return hasFlag(flag.getKey());
}
/**
* Check if a flag is present.
*
@ -39,6 +49,12 @@ public class ParsedCommandFlags
return this.flagValues.containsKey(key);
}
/**
* Check if a flag is present and has a value.
*
* @param key The key of the flag.
* @return True if the flag is present and has a value, false otherwise.
*/
public boolean hasFlagValue(@Nullable String key) {
return flagValue(key, Object.class) != null;
}
@ -46,7 +62,20 @@ public class ParsedCommandFlags
/**
* Get the value of a flag.
*
* @param key The key of the flag.
* @param <T> The type of the value.
* @param flag The flag to get the value of.
* @param type The type of the value.
* @return The value of the flag, null if flag does not exist or no value.
*/
public @Nullable <T> T flagValue(@NotNull CommandFlag flag, @NotNull Class<T> type) {
return flagValue(flag.getKey(), type);
}
/**
* Get the value of a flag.
*
* @param key The key of the flag to get the value of.
* @param type The type of the value.
* @return The value of the flag, null if flag does not exist or no value.
*/
public @Nullable <T> T flagValue(@Nullable String key, @NotNull Class<T> type) {
@ -54,6 +83,27 @@ public class ParsedCommandFlags
return (T) value;
}
/**
* Get the value of a flag.
*
* @param <T> The type of the value.
* @param flag The flag to get the value of.
* @param defaultValue The default value if flag does not exist or no value.
* @return The value of the flag, default value if flag does not exist or no value.
*/
public @NotNull <T> T flagValue(@NotNull CommandValueFlag<T> flag, @NotNull T defaultValue) {
return flagValue(flag.getKey(), defaultValue, flag.getType());
}
/**
* Get the value of a flag.
*
* @param <T> The type of the value.
* @param key The key of the flag to get the value of.
* @param defaultValue The default value if flag does not exist or no value.
* @param type The type of the value.
* @return The value of the flag, default value if flag does not exist or no value.
*/
public @NotNull <T> T flagValue(@Nullable String key, @NotNull T defaultValue, @NotNull Class<T> type) {
T value = flagValue(key, type);
return value != null ? value : defaultValue;