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

Complexity

Total Complexity 8

Size/Duplication

Total Lines 39
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 39
rs 10
c 0
b 0
f 0
wmc 8

3 Methods

Rating   Name   Duplication   Size   Complexity  
A getSuggestions(CommandContext,SuggestionsBuilder) 0 25 5
A TeamSuggestionProvider() 0 1 1
A getInstance() 0 6 2
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 net.minecraft.server.command.ServerCommandSource;
9
10
import java.util.concurrent.CompletableFuture;
11
12
/**
13
 * Suggestion provider for the teams
14
 *
15
 * @author Étienne Muser
16
 */
17
public class TeamSuggestionProvider implements SuggestionProvider<ServerCommandSource> {
18
    private static TeamSuggestionProvider instance = null;
19
20
    private TeamSuggestionProvider() {
21
    }
22
23
    public static TeamSuggestionProvider getInstance() {
24
        if (instance == null) {
25
            instance = new TeamSuggestionProvider();
26
        }
27
28
        return instance;
29
    }
30
31
    @Override
32
    public CompletableFuture<Suggestions> getSuggestions(CommandContext<ServerCommandSource> context, SuggestionsBuilder builder) {
33
34
        // Get the game managers
35
        var gameManager = context.getSource().getWorld().getServerLasertagManager();
36
        var teamsManager = gameManager.getTeamsManager();
37
        var syncedState = gameManager.getSyncedState();
38
        var teamsConfigState = syncedState.getTeamsConfigState();
39
40
        boolean inputEmpty = false;
41
        // Try to get current input
42
        String input = null;
43
        try {
44
            input = StringArgumentType.getString(context, "team");
45
        } catch (IllegalArgumentException ex) {
46
            inputEmpty = true;
47
        }
48
49
        for (var team :  teamsConfigState.getTeams()) {
50
            if (inputEmpty || team.name().toLowerCase().startsWith(input.toLowerCase())) {
51
                builder.suggest(team.name());
52
            }
53
        }
54
55
        return builder.buildFuture();
56
    }
57
}
58