many fixes for color stuff

This commit is contained in:
Aikar
2017-04-30 17:30:37 -04:00
parent b38adbd6b0
commit b49598bf75
3 changed files with 30 additions and 6 deletions
+3 -2
View File
@@ -632,7 +632,7 @@ public final class ACFUtil {
return newLoc;
}
@Nullable public static Enum<?> simpleMatch(Class<? extends Enum<?>> list, String item) {
@Nullable public static <E extends Enum<E>> E simpleMatch(Class<? extends Enum<?>> list, String item) {
if (item == null) {
return null;
}
@@ -640,7 +640,8 @@ public final class ACFUtil {
for (Enum<?> s : list.getEnumConstants()) {
String simple = ACFUtil.simplifyString(s.name());
if (item.equals(simple)) {
return s;
//noinspection unchecked
return (E) s;
}
}
@@ -45,10 +45,11 @@ public class BukkitCommandCompletions extends CommandCompletions {
return normal.collect(Collectors.toList());
});
registerCompletion("chatcolors", (sender, config, input, c) -> {
final Stream<String> normal = Stream.of(ChatColor.values())
.filter(color -> color.ordinal() <= 0xF)
.map(color -> ACFUtil.simplifyString(color.name()));
return normal.collect(Collectors.toList());
Stream<ChatColor> colors = Stream.of(ChatColor.values());
if ("colorsonly".equalsIgnoreCase(config)) {
colors = colors.filter(color -> color.ordinal() <= 0xF);
}
return colors.map(color -> ACFUtil.simplifyString(color.name())).collect(Collectors.toList());
});
registerCompletion("worlds", (sender, config, input, c) -> (
Bukkit.getWorlds().stream().map(World::getName).collect(Collectors.toList())
@@ -26,12 +26,17 @@ package co.aikar.commands;
import co.aikar.commands.annotation.Optional;
import co.aikar.commands.contexts.OnlinePlayer;
import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.World;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.inventory.PlayerInventory;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@SuppressWarnings("WeakerAccess")
public class BukkitCommandContexts extends CommandContexts {
@@ -76,5 +81,22 @@ public class BukkitCommandContexts extends CommandContexts {
}
return player;
});
registerContext(ChatColor.class, c -> {
String first = c.popFirstArg();
Stream<ChatColor> colors = Stream.of(ChatColor.values());
if (c.hasFlag("colorsonly")) {
colors = colors.filter(color -> color.ordinal() <= 0xF);
}
ChatColor match = ACFUtil.simpleMatch(ChatColor.class, first);
if (match == null) {
String valid = colors
.map(color -> color + ACFUtil.simplifyString(color.name()))
.collect(Collectors.joining("&c, "));
throw new InvalidCommandArgument("Please specify one of: " + valid);
}
return match;
});
}
}