Add new argument util method for reading context pairs

This commit is contained in:
Luck 2017-03-09 17:56:15 +00:00
parent 838fba9173
commit ddc881af11
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B

View File

@ -25,6 +25,8 @@ package me.lucko.luckperms.common.commands.utils;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Getter; import lombok.Getter;
import com.google.common.base.Splitter;
import me.lucko.luckperms.api.context.ContextSet; import me.lucko.luckperms.api.context.ContextSet;
import me.lucko.luckperms.api.context.MutableContextSet; import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.commands.CommandException; import me.lucko.luckperms.common.commands.CommandException;
@ -38,8 +40,10 @@ import java.util.function.Function;
* Utility class to help process arguments, and throw checked exceptions if the arguments are invalid. * Utility class to help process arguments, and throw checked exceptions if the arguments are invalid.
*/ */
public class ArgumentUtils { public class ArgumentUtils {
private static final Splitter CONTEXT_SPLITTER = Splitter.on('=').limit(2).omitEmptyStrings();
public static final Function<String, String> WRAPPER = s -> s.contains(" ") ? "\"" + s + "\"" : s; public static final Function<String, String> WRAPPER = s -> s.contains(" ") ? "\"" + s + "\"" : s;
public static String handleString(int index, List<String> args) { public static String handleString(int index, List<String> args) {
return args.get(index).replace("{SPACE}", " "); return args.get(index).replace("{SPACE}", " ");
} }
@ -132,6 +136,37 @@ public class ArgumentUtils {
return args.size() > index ? args.get(index).toLowerCase() : null; return args.size() > index ? args.get(index).toLowerCase() : null;
} }
public static MutableContextSet handleContext(int fromIndex, List<String> args) {
if (args.size() > fromIndex) {
MutableContextSet set = MutableContextSet.create();
List<String> contexts = args.subList(fromIndex, args.size() - 1);
for (int i = 0; i < contexts.size(); i++) {
String pair = contexts.get(i);
// one of the first two values, and doesn't have a key
if (i <= 1 && !pair.contains("=")) {
String key = i == 0 ? "server" : "world";
set.add(key, pair);
continue;
}
List<String> keyValue = CONTEXT_SPLITTER.splitToList(pair);
if (keyValue.size() != 2) {
continue;
}
set.add(keyValue.get(0), keyValue.get(1));
}
return set;
} else {
return MutableContextSet.create();
}
}
public static int handlePriority(int index, List<String> args) throws ArgumentException { public static int handlePriority(int index, List<String> args) throws ArgumentException {
try { try {
return Integer.parseInt(args.get(index)); return Integer.parseInt(args.get(index));