aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/chrisoft/trashyaddon/commands/argument/ColorCodeArgumentType.java
blob: e56ade386328dbd98cd8f1bc4928179087d9e72f (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package org.chrisoft.trashyaddon.commands.argument;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.arguments.ArgumentType;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import com.mojang.brigadier.suggestion.Suggestions;
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
import meteordevelopment.meteorclient.utils.render.color.Color;
import net.minecraft.command.CommandSource;
import net.minecraft.text.Text;
import net.minecraft.util.Formatting;

import java.util.Arrays;
import java.util.Collection;
import java.util.concurrent.CompletableFuture;

public class ColorCodeArgumentType implements ArgumentType<Color> {
    private static final Collection<String> EXAMPLES = Arrays.asList("#000", "#FFFFFF", "#AA667788", "red");
    private static final SimpleCommandExceptionType BAD_COLOR_CODE = new SimpleCommandExceptionType(Text.literal("Invalid color code."));
    private ColorCodeArgumentType() {}
    public static ColorCodeArgumentType colorCode() { return new ColorCodeArgumentType(); }

    @Override
    public Color parse(StringReader reader) throws CommandSyntaxException {
        if (reader.peek() == '#') {
            reader.read();
            String v = reader.readString();
            switch (v.length()) {
                case 3:
                    try {
                        int iv = Integer.parseInt(v, 16);
                        int r = iv & 0xF00 >> 8;
                        int g = iv & 0xF0  >> 4;
                        int b = iv & 0xF;
                        return new Color((r << 4) | r, (g << 4) | g, (b << 4) | b);
                    } catch (NumberFormatException __) {
                        throw BAD_COLOR_CODE.create();
                    }
                case 6:
                    try {
                        int iv = Integer.parseInt(v, 16);
                        int p = 0xFF000000 | iv;
                        return new Color(p);
                    } catch (NumberFormatException __) {
                        throw BAD_COLOR_CODE.create();
                    }
                case 8:
                    try {
                        int iv = Integer.parseUnsignedInt(v, 16);
                        return new Color(iv);
                    } catch (NumberFormatException __) {
                        throw BAD_COLOR_CODE.create();
                    }
                default: throw BAD_COLOR_CODE.create();
            }
        }
        String str = reader.readUnquotedString();
        Formatting lv = Formatting.byName(str);
        if (lv != null && lv.isColor()) {
            return new Color(lv.getColorValue()).a(255);
        }
        throw BAD_COLOR_CODE.create();
    }

    @Override
    public <S> CompletableFuture<Suggestions> listSuggestions(CommandContext<S> context, SuggestionsBuilder builder) {
        StringReader stringReader = new StringReader(builder.getRemaining());
        if (stringReader.canRead(1) && stringReader.peek() == '#')
            return builder.buildFuture();
        return CommandSource.suggestMatching(Formatting.getNames(true, false).stream().filter(x -> !x.equals("reset")), builder);
    }

    @Override
    public Collection<String> getExamples() {
        return EXAMPLES;
    }
}