mirror of
https://github.com/aikar/commands.git
synced 2026-05-31 06:11:55 +00:00
add new multi config system for completions and add chat colors filter
This commit is contained in:
@@ -32,6 +32,11 @@ import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.StringUtil;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
@@ -46,9 +51,17 @@ public class BukkitCommandCompletions extends CommandCompletions {
|
||||
});
|
||||
registerCompletion("chatcolors", (sender, config, input, c) -> {
|
||||
Stream<ChatColor> colors = Stream.of(ChatColor.values());
|
||||
if ("colorsonly".equalsIgnoreCase(config)) {
|
||||
if (c.hasConfig("colorsonly")) {
|
||||
colors = colors.filter(color -> color.ordinal() <= 0xF);
|
||||
}
|
||||
String filter = c.getConfig("filter");
|
||||
if (filter != null) {
|
||||
Set<String> filters = Arrays.stream(ACFPatterns.COLON.split(filter))
|
||||
.map(ACFUtil::simplifyString).collect(Collectors.toSet());
|
||||
|
||||
colors = colors.filter(color -> filters.contains(ACFUtil.simplifyString(color.name())));
|
||||
}
|
||||
|
||||
return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList());
|
||||
});
|
||||
registerCompletion("worlds", (sender, config, input, c) -> (
|
||||
|
||||
@@ -87,6 +87,12 @@ public class BukkitCommandContexts extends CommandContexts {
|
||||
if (c.hasFlag("colorsonly")) {
|
||||
colors = colors.filter(color -> color.ordinal() <= 0xF);
|
||||
}
|
||||
String filter = c.getFlagValue("filter", (String) null);
|
||||
if (filter != null) {
|
||||
filter = ACFUtil.simplifyString(filter);
|
||||
String finalFilter = filter;
|
||||
colors = colors.filter(color -> finalFilter.equals(ACFUtil.simplifyString(color.name())));
|
||||
}
|
||||
|
||||
ChatColor match = ACFUtil.simpleMatch(ChatColor.class, first);
|
||||
if (match == null) {
|
||||
|
||||
@@ -23,8 +23,10 @@
|
||||
|
||||
package co.aikar.commands;
|
||||
|
||||
import co.aikar.commands.annotation.Split;
|
||||
import com.google.common.collect.ImmutableList;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.bukkit.command.CommandSender;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
@@ -126,16 +128,43 @@ public class CommandCompletions {
|
||||
private final CommandSender sender;
|
||||
private final String input;
|
||||
private final String config;
|
||||
private final Map<String, String> configs = Maps.newHashMap();
|
||||
private final List<String> args;
|
||||
|
||||
CommandCompletionContext(RegisteredCommand command, CommandSender sender, String input, String config, String[] args) {
|
||||
this.command = command;
|
||||
this.sender = sender;
|
||||
this.input = input;
|
||||
this.config = config;
|
||||
if (config != null) {
|
||||
String[] configs = ACFPatterns.COMMA.split(config);
|
||||
for (String conf : configs) {
|
||||
String[] confsplit = ACFPatterns.EQUALS.split(conf, 2);
|
||||
this.configs.put(confsplit[0].toLowerCase(), confsplit.length > 1 ? confsplit[1] : null);
|
||||
}
|
||||
this.config = configs[0];
|
||||
} else {
|
||||
this.config = null;
|
||||
}
|
||||
|
||||
this.args = Lists.newArrayList(args);
|
||||
}
|
||||
|
||||
public Map<String, String> getConfigs() {
|
||||
return configs;
|
||||
}
|
||||
|
||||
public String getConfig(String key) {
|
||||
return getConfig(key, null);
|
||||
}
|
||||
|
||||
public String getConfig(String key, String def) {
|
||||
return this.configs.getOrDefault(key.toLowerCase(), def);
|
||||
}
|
||||
|
||||
public boolean hasConfig(String key) {
|
||||
return this.configs.containsKey(key.toLowerCase());
|
||||
}
|
||||
|
||||
public <T> T getContextValue(Class<? extends T> clazz) throws InvalidCommandArgument {
|
||||
return getContextValue(clazz, null);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user