1 package com.irtimaled.bbor.client.commands;
3 import com.irtimaled.bbor.client.config.HexColor;
4 import com.mojang.brigadier.LiteralMessage;
5 import com.mojang.brigadier.StringReader;
6 import com.mojang.brigadier.arguments.ArgumentType;
7 import com.mojang.brigadier.context.CommandContext;
8 import com.mojang.brigadier.exceptions.CommandSyntaxException;
9 import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
10 import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
11 import com.mojang.brigadier.suggestion.Suggestions;
12 import com.mojang.brigadier.suggestion.SuggestionsBuilder;
14 import java.util.Arrays;
15 import java.util.Collection;
16 import java.util.List;
17 import java.util.concurrent.CompletableFuture;
19 public class HexColorArgument implements ArgumentType<HexColor> {
20 private static final List<String> EXAMPLES = Arrays.asList("#000000", "#ffffff");
21 public static final DynamicCommandExceptionType INVALID_HEX_COLOR = new DynamicCommandExceptionType(v -> new LiteralMessage("Invalid hex color, expected six digit hex color starting with # but found '" + v + "'"));
22 public static final SimpleCommandExceptionType EXPECTED_HEX_COLOR = new SimpleCommandExceptionType(new LiteralMessage("Expected hex color"));
25 public HexColor parse(StringReader reader) throws CommandSyntaxException {
26 String value = reader.getRemaining().split(" ")[0].toLowerCase();
27 if (value.isEmpty()) {
28 throw EXPECTED_HEX_COLOR.createWithContext(reader);
31 HexColor color = HexColor.from(value);
33 reader.setCursor(reader.getCursor() + 7);
37 throw INVALID_HEX_COLOR.createWithContext(reader, value);
41 public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
42 if (builder.getRemaining().length() == 0) builder.suggest("#");
43 return builder.buildFuture();
47 public Collection<String> getExamples() {