mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-11-30 22:53:27 +01:00
Add new argument util method for reading context pairs
This commit is contained in:
parent
838fba9173
commit
ddc881af11
@ -25,6 +25,8 @@ package me.lucko.luckperms.common.commands.utils;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
import com.google.common.base.Splitter;
|
||||
|
||||
import me.lucko.luckperms.api.context.ContextSet;
|
||||
import me.lucko.luckperms.api.context.MutableContextSet;
|
||||
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.
|
||||
*/
|
||||
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 String handleString(int index, List<String> args) {
|
||||
return args.get(index).replace("{SPACE}", " ");
|
||||
}
|
||||
@ -132,6 +136,37 @@ public class ArgumentUtils {
|
||||
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 {
|
||||
try {
|
||||
return Integer.parseInt(args.get(index));
|
||||
|
Loading…
Reference in New Issue
Block a user