mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2025-01-25 01:31:24 +01:00
Merge pull request #2997 from Multiverse/better-flag-handling
Better flag handling
This commit is contained in:
commit
c71bdd83d6
@ -1,9 +1,12 @@
|
|||||||
package com.onarandombox.MultiverseCore.commandtools;
|
package com.onarandombox.MultiverseCore.commandtools;
|
||||||
|
|
||||||
import co.aikar.commands.BaseCommand;
|
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.CommandFlagGroup;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagsManager;
|
import com.onarandombox.MultiverseCore.commandtools.flags.CommandFlagsManager;
|
||||||
import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags;
|
import com.onarandombox.MultiverseCore.commandtools.flags.ParsedCommandFlags;
|
||||||
|
import jakarta.annotation.PostConstruct;
|
||||||
import org.jetbrains.annotations.NotNull;
|
import org.jetbrains.annotations.NotNull;
|
||||||
import org.jvnet.hk2.annotations.Contract;
|
import org.jvnet.hk2.annotations.Contract;
|
||||||
|
|
||||||
@ -12,21 +15,39 @@ public abstract class MultiverseCommand extends BaseCommand {
|
|||||||
|
|
||||||
protected final MVCommandManager commandManager;
|
protected final MVCommandManager commandManager;
|
||||||
private String flagGroupName;
|
private String flagGroupName;
|
||||||
|
private CommandFlagGroup.Builder flagGroupBuilder;
|
||||||
|
|
||||||
protected MultiverseCommand(@NotNull MVCommandManager commandManager) {
|
protected MultiverseCommand(@NotNull MVCommandManager commandManager) {
|
||||||
this.commandManager = commandManager;
|
this.commandManager = commandManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
private void postConstruct() {
|
||||||
|
if (flagGroupBuilder != null) {
|
||||||
|
registerFlagGroup(flagGroupBuilder.build());
|
||||||
|
flagGroupBuilder = null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
protected CommandFlagsManager getFlagsManager() {
|
protected CommandFlagsManager getFlagsManager() {
|
||||||
return commandManager.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) {
|
protected void registerFlagGroup(@NotNull CommandFlagGroup flagGroup) {
|
||||||
if (flagGroupName != null) {
|
if (flagGroupName != null) {
|
||||||
throw new IllegalStateException("Flag group already registered! (name: " + flagGroupName + ")");
|
throw new IllegalStateException("Flag group already registered! (name: " + flagGroupName + ")");
|
||||||
}
|
}
|
||||||
getFlagsManager().registerFlagGroup(flagGroup);
|
getFlagsManager().registerFlagGroup(flagGroup);
|
||||||
flagGroupName = flagGroup.getName();
|
flagGroupName = flagGroup.getName();
|
||||||
|
Logging.fine("Registered flag group: " + flagGroupName);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected @NotNull ParsedCommandFlags parseFlags(@NotNull String[] flags) {
|
protected @NotNull ParsedCommandFlags parseFlags(@NotNull String[] flags) {
|
||||||
|
@ -15,7 +15,7 @@ public class ParsedCommandFlags
|
|||||||
|
|
||||||
private final Map<String, Object> flagValues;
|
private final Map<String, Object> flagValues;
|
||||||
|
|
||||||
public ParsedCommandFlags() {
|
ParsedCommandFlags() {
|
||||||
flagValues = new HashMap<>();
|
flagValues = new HashMap<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,6 +29,16 @@ public class ParsedCommandFlags
|
|||||||
flagValues.put(key, value);
|
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.
|
* Check if a flag is present.
|
||||||
*
|
*
|
||||||
@ -39,6 +49,12 @@ public class ParsedCommandFlags
|
|||||||
return this.flagValues.containsKey(key);
|
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) {
|
public boolean hasFlagValue(@Nullable String key) {
|
||||||
return flagValue(key, Object.class) != null;
|
return flagValue(key, Object.class) != null;
|
||||||
}
|
}
|
||||||
@ -46,7 +62,20 @@ public class ParsedCommandFlags
|
|||||||
/**
|
/**
|
||||||
* Get the value of a flag.
|
* 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.
|
* @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) {
|
public @Nullable <T> T flagValue(@Nullable String key, @NotNull Class<T> type) {
|
||||||
@ -54,6 +83,27 @@ public class ParsedCommandFlags
|
|||||||
return (T) value;
|
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) {
|
public @NotNull <T> T flagValue(@Nullable String key, @NotNull T defaultValue, @NotNull Class<T> type) {
|
||||||
T value = flagValue(key, type);
|
T value = flagValue(key, type);
|
||||||
return value != null ? value : defaultValue;
|
return value != null ? value : defaultValue;
|
||||||
|
Loading…
Reference in New Issue
Block a user