mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-26 04:25:37 +01:00
Refactor a better method of registering and using command flags
This commit is contained in:
parent
0b7f3246a1
commit
3cdf9736ec
@ -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) {
|
||||
|
@ -29,6 +29,10 @@ public class ParsedCommandFlags
|
||||
flagValues.put(key, value);
|
||||
}
|
||||
|
||||
public boolean hasFlag(@NotNull CommandFlag flag) {
|
||||
return hasFlag(flag.getKey());
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a flag is present.
|
||||
*
|
||||
@ -43,6 +47,10 @@ public class ParsedCommandFlags
|
||||
return flagValue(key, Object.class) != null;
|
||||
}
|
||||
|
||||
public @Nullable <T> T flagValue(@NotNull CommandFlag flag, @NotNull Class<T> type) {
|
||||
return flagValue(flag.getKey(), type);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a flag.
|
||||
*
|
||||
@ -54,6 +62,10 @@ public class ParsedCommandFlags
|
||||
return (T) value;
|
||||
}
|
||||
|
||||
public @Nullable <T> T flagValue(@NotNull CommandFlag flag, @NotNull T defaultValue, @NotNull Class<T> type) {
|
||||
return flagValue(flag.getKey(), defaultValue, type);
|
||||
}
|
||||
|
||||
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;
|
||||
|
Loading…
Reference in New Issue
Block a user