de.pewpewproject.lasertag.command.suggestions.LasertagGameModeSuggestionProvider   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 34
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 34
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSuggestions(CommandContext,SuggestionsBuilder) 0 19 5
A getInstance() 0 6 2
A LasertagGameModeSuggestionProvider() 0 1 1
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.suggestion.SuggestionProvider;
6
import com.mojang.brigadier.suggestion.Suggestions;
7
import com.mojang.brigadier.suggestion.SuggestionsBuilder;
8
import de.pewpewproject.lasertag.lasertaggame.gamemode.GameModes;
9
import net.minecraft.server.command.ServerCommandSource;
10
11
import java.util.concurrent.CompletableFuture;
12
13
/**
14
 * Suggestion provider for game modes
15
 *
16
 * @author Étienne Muser
17
 */
18
public class LasertagGameModeSuggestionProvider implements SuggestionProvider<ServerCommandSource> {
19
20
    private static LasertagGameModeSuggestionProvider instance = null;
21
22
    private LasertagGameModeSuggestionProvider() {
23
    }
24
25
    public static LasertagGameModeSuggestionProvider getInstance() {
26
        if (instance == null) {
27
            instance = new LasertagGameModeSuggestionProvider();
28
        }
29
30
        return instance;
31
    }
32
33
    @Override
34
    public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
35
        boolean inputEmpty = false;
36
        // Try to get current input
37
        String input = null;
38
        try {
39
            input = StringArgumentType.getString(context, "gamemode");
40
        } catch (IllegalArgumentException ex) {
41
            inputEmpty = true;
42
        }
43
44
        for (var gameModeTranslatableName : GameModes.GAME_MODES.keySet()) {
45
46
            if (inputEmpty || gameModeTranslatableName.toLowerCase().contains(input.toLowerCase())) {
47
                builder.suggest(gameModeTranslatableName);
48
            }
49
        }
50
51
        return builder.buildFuture();
52
    }
53
}
54