mirror of
https://github.com/Multiverse/Multiverse-Core.git
synced 2024-11-25 12:05:14 +01:00
Add documentation to flag classes,
This commit is contained in:
parent
7a86227a94
commit
826fe08cfc
@ -4,17 +4,34 @@ import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* <p>Represents a flag that can be used in commands. This works as a key value pair.</p>
|
||||
*
|
||||
* <p>Key is the {@link #identifier} and {@link #aliases} set.</p>
|
||||
* <p>Value is the {@link T} parsed based on 3 scenarios during command input:</p>
|
||||
* <ol>
|
||||
* <li>Flag completely not present. {@link #getDefaultValue()}</li>
|
||||
* <li>Flag key present but no value. {@link #getValue()}</li>
|
||||
* <li>Flag key and value present. {@link #getValue(String)}</li>
|
||||
* </ol>
|
||||
*
|
||||
* @param <T> The flag Type.
|
||||
*/
|
||||
public abstract class CommandFlag<T> {
|
||||
|
||||
protected final String name;
|
||||
protected final String identifier;
|
||||
protected final Class<T> type;
|
||||
protected final ValueRequirement valueRequirement;
|
||||
protected final List<String> aliases;
|
||||
protected final Collection<String> aliases;
|
||||
|
||||
protected CommandFlag(@NotNull String name,
|
||||
@NotNull String identifier,
|
||||
@NotNull Class<T> type,
|
||||
@NotNull ValueRequirement valueRequirement) {
|
||||
|
||||
public CommandFlag(String name, String identifier, Class<T> type, ValueRequirement valueRequirement) {
|
||||
this.name = name;
|
||||
this.identifier = identifier;
|
||||
this.type = type;
|
||||
@ -22,39 +39,95 @@ public abstract class CommandFlag<T> {
|
||||
this.aliases = new ArrayList<>();
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets name of the Command Flag.
|
||||
*
|
||||
* @return The Command Flag name.
|
||||
*/
|
||||
@NotNull
|
||||
public String getName() {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets identifier of the Command Flag.
|
||||
*
|
||||
* @return The Command Flag identifier.
|
||||
*/
|
||||
@NotNull
|
||||
public String getIdentifier() {
|
||||
return this.identifier;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets {@link T} Type of the Command Flag.
|
||||
*
|
||||
* @return The Command Flag type.
|
||||
*/
|
||||
@NotNull
|
||||
public Class<T> getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the requirements of this Command Flag user input value.
|
||||
*
|
||||
* @return The {@link ValueRequirement}.
|
||||
*/
|
||||
@NotNull
|
||||
public ValueRequirement getValueRequirement() {
|
||||
return this.valueRequirement;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets all the alternative key identifiers set for this Command Flag.
|
||||
*
|
||||
* @return Collection of aliases.
|
||||
*/
|
||||
@NotNull
|
||||
public Collection<String> getAliases() {
|
||||
return this.aliases;
|
||||
}
|
||||
|
||||
CommandFlag<T> addAlias(String alias) {
|
||||
this.aliases.add(alias);
|
||||
/**
|
||||
* Add alternative key identifiers for this Command Flag.
|
||||
*
|
||||
* @param aliases Alias(es) to be added.
|
||||
* @return A {@link CommandFlag}.
|
||||
*/
|
||||
public CommandFlag<T> addAliases(String...aliases) {
|
||||
Collections.addAll(this.aliases, aliases);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Tab-complete suggestion for this Command Flag values.
|
||||
*
|
||||
* @return Collection of suggested values available.
|
||||
*/
|
||||
public abstract Collection<String> suggestValue();
|
||||
|
||||
/**
|
||||
* When this Command Flag can get value by a user input.
|
||||
*
|
||||
* @return The {@link T} value.
|
||||
*/
|
||||
public abstract T getValue(@NotNull String input) throws FlagParseFailedException;
|
||||
|
||||
/**
|
||||
* When this Command Flag user input value is null/not present.
|
||||
*
|
||||
* @return The {@link T} value.
|
||||
*/
|
||||
public T getValue() throws FlagParseFailedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* When this Command Flag is not present in command input.
|
||||
*
|
||||
* @return The {@link T} value.
|
||||
*/
|
||||
public T getDefaultValue() {
|
||||
return null;
|
||||
}
|
||||
@ -71,8 +144,19 @@ public abstract class CommandFlag<T> {
|
||||
}
|
||||
|
||||
public enum ValueRequirement {
|
||||
/**
|
||||
* No user input needed for value.
|
||||
*/
|
||||
NONE,
|
||||
|
||||
/**
|
||||
* User input for value is optional.
|
||||
*/
|
||||
OPTIONAL,
|
||||
|
||||
/**
|
||||
* User input is required for Command Flag value.
|
||||
*/
|
||||
REQUIRED
|
||||
}
|
||||
}
|
||||
|
@ -1,12 +1,13 @@
|
||||
package com.onarandombox.MultiverseCore.commandtools.flags;
|
||||
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class FlagGroup {
|
||||
|
||||
@ -14,36 +15,36 @@ public class FlagGroup {
|
||||
return new FlagGroup(flags);
|
||||
}
|
||||
|
||||
private final Set<CommandFlag<?>> flags;
|
||||
private final List<String> flagIdentifiers;
|
||||
private final Map<String, CommandFlag<?>> keyFlagMap;
|
||||
private final Map<String, CommandFlag<?>> flagKeyMap;
|
||||
|
||||
private FlagGroup(CommandFlag<?>[] commandFlags) {
|
||||
this.flags = new HashSet<>(commandFlags.length);
|
||||
this.flagIdentifiers = new ArrayList<>(commandFlags.length);
|
||||
this.keyFlagMap = new HashMap<>();
|
||||
this.flagKeyMap = new HashMap<>();
|
||||
for (CommandFlag<?> flag : commandFlags) {
|
||||
addFlag(flag);
|
||||
}
|
||||
}
|
||||
|
||||
private void addFlag(CommandFlag<?> flag) {
|
||||
this.flags.add(flag);
|
||||
this.flagIdentifiers.add(flag.getIdentifier());
|
||||
this.keyFlagMap.put(flag.getIdentifier(), flag);
|
||||
this.flagKeyMap.put(flag.getIdentifier(), flag);
|
||||
for (String flagAlias : flag.getAliases()) {
|
||||
this.keyFlagMap.put(flagAlias, flag);
|
||||
this.flagKeyMap.put(flagAlias, flag);
|
||||
}
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public FlagResult calculateResult(String[] args) {
|
||||
return FlagResult.parse(args,this);
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public CommandFlag<?> getByKey(String key) {
|
||||
return this.keyFlagMap.get(key);
|
||||
}
|
||||
|
||||
public Collection<CommandFlag<?>> getFlags() {
|
||||
return this.flags;
|
||||
return this.flagKeyMap.get(key);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public Collection<String> getFlagIdentifiers() {
|
||||
return flagIdentifiers;
|
||||
}
|
||||
@ -51,8 +52,8 @@ public class FlagGroup {
|
||||
@Override
|
||||
public String toString() {
|
||||
return "FlagGroup{" +
|
||||
"flags=" + flags +
|
||||
", keyFlagMap=" + keyFlagMap +
|
||||
"flagIdentifiers=" + flagIdentifiers +
|
||||
", keyFlagMap=" + flagKeyMap +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
@ -22,8 +22,11 @@ public class MVFlags {
|
||||
multiverse = plugin;
|
||||
}
|
||||
|
||||
/**
|
||||
* Flag for custom seed.
|
||||
*/
|
||||
public static final CommandFlag<String> SEED = new RequiredCommandFlag<String>
|
||||
("Seed", "-s", String.class) {
|
||||
("Seed", "--seed", String.class) {
|
||||
@Override
|
||||
public Collection<String> suggestValue() {
|
||||
return Arrays.asList("seed", String.valueOf(new Random().nextLong()));
|
||||
@ -33,10 +36,13 @@ public class MVFlags {
|
||||
public String getValue(@NotNull String input) throws FlagParseFailedException {
|
||||
return input;
|
||||
}
|
||||
};
|
||||
}.addAliases("-s");
|
||||
|
||||
/**
|
||||
* Flag for custom seed. No value means random seed.
|
||||
*/
|
||||
public static final CommandFlag<String> RANDOM_SEED = new OptionalCommandFlag<String>
|
||||
("Seed", "-s", String.class) {
|
||||
("Seed", "--seed", String.class) {
|
||||
@Override
|
||||
public Collection<String> suggestValue() {
|
||||
return Arrays.asList("seed", String.valueOf(new Random().nextLong()));
|
||||
@ -46,10 +52,13 @@ public class MVFlags {
|
||||
public String getValue(@NotNull String input) throws FlagParseFailedException {
|
||||
return input;
|
||||
}
|
||||
};
|
||||
}.addAliases("-s");
|
||||
|
||||
/**
|
||||
* Flag for world type used.
|
||||
*/
|
||||
public static final CommandFlag<WorldType> WORLD_TYPE = new RequiredCommandFlag<WorldType>
|
||||
("WorldType", "-t", WorldType.class) {
|
||||
("WorldType", "--type", WorldType.class) {
|
||||
|
||||
private final Map<String, WorldType> typeAlias = new HashMap<String, WorldType>(4){{
|
||||
put("normal", WorldType.NORMAL);
|
||||
@ -81,10 +90,13 @@ public class MVFlags {
|
||||
public WorldType getDefaultValue() {
|
||||
return WorldType.NORMAL;
|
||||
}
|
||||
};
|
||||
}.addAliases("-t");
|
||||
|
||||
/**
|
||||
* Flag for world generator.
|
||||
*/
|
||||
public static final CommandFlag<String> GENERATOR = new RequiredCommandFlag<String>
|
||||
("Generator", "-g", String.class) {
|
||||
("Generator", "--gen", String.class) {
|
||||
@Override
|
||||
public Collection<String> suggestValue() {
|
||||
return multiverse.getMVWorldManager().getAvailableWorldGenerators();
|
||||
@ -104,10 +116,13 @@ public class MVFlags {
|
||||
}
|
||||
return input;
|
||||
}
|
||||
};
|
||||
}.addAliases("-g");
|
||||
|
||||
/**
|
||||
* Flag to toggle if world should generate structures.
|
||||
*/
|
||||
public static final CommandFlag<Boolean> GENERATE_STRUCTURES = new RequiredCommandFlag<Boolean>
|
||||
("GenerateStructures", "-a", Boolean.class) {
|
||||
("GenerateStructures", "--structures", Boolean.class) {
|
||||
@Override
|
||||
public Collection<String> suggestValue() {
|
||||
return Arrays.asList("true", "false");
|
||||
@ -122,10 +137,13 @@ public class MVFlags {
|
||||
public Boolean getDefaultValue() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}.addAliases("--structure", "-a");
|
||||
|
||||
/**
|
||||
* Flag to toggle if world spawn should be adjusted.
|
||||
*/
|
||||
public static final CommandFlag<Boolean> SPAWN_ADJUST = new NoValueCommandFlag<Boolean>
|
||||
("AdjustSpawn", "-n", Boolean.class) {
|
||||
("AdjustSpawn", "--dont-adjust-spawn", Boolean.class) {
|
||||
@Override
|
||||
public Boolean getValue() throws FlagParseFailedException {
|
||||
return false;
|
||||
@ -135,8 +153,11 @@ public class MVFlags {
|
||||
public Boolean getDefaultValue() {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}.addAliases("-n");
|
||||
|
||||
/**
|
||||
* Flag to specify a paste service.
|
||||
*/
|
||||
public static final CommandFlag<PasteServiceType> PASTE_SERVICE_TYPE = new OptionalCommandFlag<PasteServiceType>
|
||||
("PasteServiceType", "--paste", PasteServiceType.class) {
|
||||
|
||||
@ -169,8 +190,11 @@ public class MVFlags {
|
||||
public PasteServiceType getDefaultValue() {
|
||||
return PasteServiceType.NONE;
|
||||
}
|
||||
}.addAlias("-p");
|
||||
}.addAliases("-p");
|
||||
|
||||
/**
|
||||
* Flag to toggle if plugin list should be included.
|
||||
*/
|
||||
public static final CommandFlag<Boolean> INCLUDE_PLUGIN_LIST = new NoValueCommandFlag<Boolean>
|
||||
("IncludePlugins", "--include-plugin-list", Boolean.class) {
|
||||
@Override
|
||||
@ -182,5 +206,5 @@ public class MVFlags {
|
||||
public Boolean getDefaultValue() {
|
||||
return true;
|
||||
}
|
||||
}.addAlias("-pl");
|
||||
}.addAliases("-pl");
|
||||
}
|
||||
|
@ -5,17 +5,31 @@ import org.jetbrains.annotations.NotNull;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
/**
|
||||
* Command Flag with {@link ValueRequirement#NONE}.
|
||||
* This flag will always not require a user input to parse value.
|
||||
*
|
||||
* @param <T> The flag Type.
|
||||
*/
|
||||
public abstract class NoValueCommandFlag<T> extends CommandFlag<T> {
|
||||
|
||||
public NoValueCommandFlag(String name, String identifier, Class<T> type) {
|
||||
super(name, identifier, type, ValueRequirement.NONE);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link NoValueCommandFlag} will always not require a user input to parse value.
|
||||
* Thus, no value suggestion needed.
|
||||
*/
|
||||
@Override
|
||||
public final Collection<String> suggestValue() {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link NoValueCommandFlag} will always not require a user input to parse value.
|
||||
* Thus, this operation is not allowed.
|
||||
*/
|
||||
@Override
|
||||
public final T getValue(@NotNull String input) throws FlagParseFailedException {
|
||||
throw new FlagParseFailedException("%s flag '%s' does not require a value.", this.name, this.identifier);
|
||||
|
@ -1,5 +1,10 @@
|
||||
package com.onarandombox.MultiverseCore.commandtools.flags;
|
||||
|
||||
/**
|
||||
* Command Flag with {@link ValueRequirement#OPTIONAL}.
|
||||
*
|
||||
* @param <T> The flag Type.
|
||||
*/
|
||||
public abstract class OptionalCommandFlag<T> extends CommandFlag<T> {
|
||||
|
||||
public OptionalCommandFlag(String name, String identifier, Class<T> type) {
|
||||
|
@ -1,11 +1,21 @@
|
||||
package com.onarandombox.MultiverseCore.commandtools.flags;
|
||||
|
||||
/**
|
||||
* Command Flag with {@link ValueRequirement#REQUIRED}.
|
||||
* This flag will always require a user input to parse value.
|
||||
*
|
||||
* @param <T> The flag Type.
|
||||
*/
|
||||
public abstract class RequiredCommandFlag<T> extends CommandFlag<T> {
|
||||
|
||||
public RequiredCommandFlag(String name, String identifier, Class<T> type) {
|
||||
super(name, identifier, type, ValueRequirement.REQUIRED);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@link RequiredCommandFlag} will always require a user input to parse value.
|
||||
* Thus, this operation is not allowed.
|
||||
*/
|
||||
@Override
|
||||
public final T getValue() throws FlagParseFailedException {
|
||||
throw new FlagParseFailedException("%s flag '%s' requires a value input.", this.name, this.identifier);
|
||||
|
@ -65,8 +65,7 @@ public class CreateCommand extends MultiverseCoreCommand {
|
||||
@Description("Other world settings. See: http://gg.gg/nn8bl")
|
||||
String[] flagsArray) {
|
||||
|
||||
FlagResult flags = FlagResult.parse(flagsArray, this.getFlagGroup());
|
||||
Logging.info(String.valueOf(flags));
|
||||
FlagResult flags = this.getFlagGroup().calculateResult(flagsArray);
|
||||
|
||||
Command.broadcastCommandMessage(sender, String.format("Starting creation of world '%s'...", worldName));
|
||||
Command.broadcastCommandMessage(sender, (this.plugin.getMVWorldManager().addWorld(
|
||||
|
@ -55,7 +55,7 @@ public class ImportCommand extends MultiverseCoreCommand {
|
||||
@Description("Other world settings. See: http://gg.gg/nn8c2")
|
||||
String[] flagsArray) {
|
||||
|
||||
FlagResult flags = FlagResult.parse(flagsArray, this.getFlagGroup());
|
||||
FlagResult flags = this.getFlagGroup().calculateResult(flagsArray);
|
||||
|
||||
Command.broadcastCommandMessage(sender, String.format("Starting import of world '%s'...", worldName));
|
||||
Command.broadcastCommandMessage(sender, (this.plugin.getMVWorldManager().addWorld(worldName,
|
||||
|
@ -50,7 +50,7 @@ public class RegenCommand extends MultiverseCoreCommand {
|
||||
@Description("Other world settings. See: http://gg.gg/nn8lk")
|
||||
String[] flagsArray) {
|
||||
|
||||
FlagResult flags = FlagResult.parse(flagsArray, this.getFlagGroup());
|
||||
FlagResult flags = this.getFlagGroup().calculateResult(flagsArray);
|
||||
|
||||
this.plugin.getMVCommandManager().getQueueManager().addToQueue(new QueuedCommand.Builder()
|
||||
.sender(sender)
|
||||
|
Loading…
Reference in New Issue
Block a user