getSuggestions(CommandContext,SuggestionsBuilder)   A
last analyzed

Complexity

Conditions 5

Size

Total Lines 23
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 14
dl 0
loc 23
rs 9.2333
c 0
b 0
f 0
1
package de.pewpewproject.lasertag.command.suggestions;
2
3
import com.mojang.brigadier.arguments.StringArgumentType;
4
import com.mojang.brigadier.context.CommandContext;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import com.mojang.brigadier.suggestion.SuggestionProvider;
7
import com.mojang.brigadier.suggestion.Suggestions;
8
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
9
import net.minecraft.server.command.ServerCommandSource;
10
11
import java.util.concurrent.CompletableFuture;
12
13
/**
14
 * Suggestion provider for enum settings
15
 *
16
 * @author Étienne Muser
17
 */
18
public class EnumSettingSuggestionProvider implements SuggestionProvider<ServerCommandSource> {
19
20
    private final Class<?> enumType;
21
22
    public EnumSettingSuggestionProvider(Class<?> enumType) {
23
        this.enumType = enumType;
24
    }
25
26
    @Override
27
    public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) throws CommandSyntaxException {
28
        boolean inputEmpty = false;
29
        // Try to get current input
30
        String input = null;
31
        try {
32
            input = StringArgumentType.getString(context, "team");
33
        } catch (IllegalArgumentException ex) {
34
            inputEmpty = true;
35
        }
36
37
        // Get the enum values
38
        var enumValues = this.enumType.getEnumConstants();
39
40
        for (var enumValue : enumValues) {
41
            var enumName = ((Enum<?>)enumValue).name();
42
43
            if (inputEmpty || enumName.toLowerCase().startsWith(input.toLowerCase())) {
44
                builder.suggest(enumName);
45
            }
46
        }
47
48
        return builder.buildFuture();
49
    }
50
}
51