Mirror von
https://github.com/IntellectualSites/FastAsyncWorldEdit.git
synchronisiert 2024-11-05 19:10:07 +01:00
Added support for value flags to CommandContext.
Dieser Commit ist enthalten in:
Ursprung
8ac8cb77a5
Commit
799b84622f
@ -18,12 +18,16 @@
|
||||
|
||||
package com.sk89q.minecraft.util.commands;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
public class CommandContext {
|
||||
protected String[] args;
|
||||
protected Set<Character> flags = new HashSet<Character>();
|
||||
protected final String[] args;
|
||||
protected final Set<Character> booleanFlags = new HashSet<Character>();
|
||||
protected final Map<Character, String> valueFlags = new HashMap<Character, String>();
|
||||
|
||||
public CommandContext(String args) {
|
||||
this(args.split(" "));
|
||||
@ -36,7 +40,7 @@ public class CommandContext {
|
||||
// Ignore this
|
||||
} else if (args[i].charAt(0) == '-' && args[i].matches("^-[a-zA-Z]+$")) {
|
||||
for (int k = 1; k < args[i].length(); ++k) {
|
||||
flags.add(args[i].charAt(k));
|
||||
booleanFlags.add(args[i].charAt(k));
|
||||
}
|
||||
} else {
|
||||
break;
|
||||
@ -51,6 +55,54 @@ public class CommandContext {
|
||||
this.args = newArgs;
|
||||
}
|
||||
|
||||
public CommandContext(String[] args, Set<Character> isValueFlag) throws CommandException {
|
||||
int nextArg = 0;
|
||||
|
||||
booleanFlags.clear();
|
||||
valueFlags.clear();
|
||||
|
||||
while (nextArg < args.length) {
|
||||
// Fetch argument
|
||||
String arg = args[nextArg++];
|
||||
|
||||
// Empty argument? (multiple consecutive spaces)
|
||||
if (arg.isEmpty())
|
||||
continue;
|
||||
|
||||
// No more flags?
|
||||
if (arg.charAt(0) != '-' || arg.length() == 1) {
|
||||
--nextArg;
|
||||
break;
|
||||
}
|
||||
|
||||
// Handle flag parsing terminator --
|
||||
if (arg.equals("--"))
|
||||
break;
|
||||
|
||||
// Go through the flags
|
||||
for (int i = 1; i < arg.length(); ++i) {
|
||||
char flagName = arg.charAt(i);
|
||||
|
||||
if (isValueFlag.contains(flagName)) {
|
||||
// Skip empty arguments...
|
||||
while (nextArg < args.length && args[nextArg].isEmpty())
|
||||
++nextArg;
|
||||
|
||||
if (nextArg >= args.length)
|
||||
throw new CommandException("No value specified for "+flagName+" flag.");
|
||||
|
||||
// If it is a value flag, read another argument and add it
|
||||
valueFlags.put(flagName, args[nextArg++]);
|
||||
}
|
||||
else {
|
||||
booleanFlags.add(flagName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this.args = Arrays.copyOfRange(args, nextArg, args.length);
|
||||
}
|
||||
|
||||
public String getCommand() {
|
||||
return args[0];
|
||||
}
|
||||
@ -105,11 +157,51 @@ public class CommandContext {
|
||||
}
|
||||
|
||||
public boolean hasFlag(char ch) {
|
||||
return flags.contains(ch);
|
||||
return booleanFlags.contains(ch) || valueFlags.containsKey(ch);
|
||||
}
|
||||
|
||||
public Set<Character> getFlags() {
|
||||
return flags;
|
||||
return booleanFlags;
|
||||
}
|
||||
|
||||
public Map<Character, String> getValueFlags() {
|
||||
return valueFlags;
|
||||
}
|
||||
|
||||
public String getFlag(char ch) {
|
||||
return valueFlags.get(ch);
|
||||
}
|
||||
|
||||
public String getFlag(char ch, String def) {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null)
|
||||
return def;
|
||||
|
||||
return value;
|
||||
}
|
||||
|
||||
public int getFlagInteger(char ch) throws NumberFormatException {
|
||||
return Integer.parseInt(valueFlags.get(ch));
|
||||
}
|
||||
|
||||
public int getFlagInteger(char ch, int def) throws NumberFormatException {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null)
|
||||
return def;
|
||||
|
||||
return Integer.parseInt(value);
|
||||
}
|
||||
|
||||
public double getFlagDouble(char ch) throws NumberFormatException {
|
||||
return Double.parseDouble(valueFlags.get(ch));
|
||||
}
|
||||
|
||||
public double getFlagDouble(char ch, double def) throws NumberFormatException {
|
||||
final String value = valueFlags.get(ch);
|
||||
if (value == null)
|
||||
return def;
|
||||
|
||||
return Double.parseDouble(value);
|
||||
}
|
||||
|
||||
public int length() {
|
||||
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren