Passed
Push — main ( b981dd...7422f7 )
by Etienne
01:33
created

register(LiteralArgumentBuilder)   A

Complexity

Conditions 1

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 5
c 1
b 0
f 0
dl 0
loc 5
rs 10
1
package de.kleiner3.lasertag.command.lasertag.game;
2
3
import com.mojang.brigadier.arguments.StringArgumentType;
4
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
5
import com.mojang.brigadier.context.CommandContext;
6
import de.kleiner3.lasertag.command.CommandFeedback;
7
import de.kleiner3.lasertag.command.ServerFeedbackCommand;
8
import de.kleiner3.lasertag.command.suggestions.LasertagGameModeSuggestionProvider;
9
import de.kleiner3.lasertag.lasertaggame.management.LasertagGameManager;
10
import de.kleiner3.lasertag.lasertaggame.management.gamemode.GameModes;
11
import net.minecraft.server.command.ServerCommandSource;
12
import net.minecraft.text.Text;
13
import net.minecraft.util.Formatting;
14
15
import java.util.Optional;
16
17
import static com.mojang.brigadier.arguments.StringArgumentType.word;
18
import static net.minecraft.server.command.CommandManager.argument;
19
import static net.minecraft.server.command.CommandManager.literal;
20
21
/**
22
 * The lasertag game mode command
23
 *
24
 * @author Étienne Muser
25
 */
26
public class LasertagGameModeCommand extends ServerFeedbackCommand {
27
    @Override
28
    protected Optional<CommandFeedback> execute(CommandContext<ServerCommandSource> context) {
29
30
        // Get the game mode translatable name
31
        var gameModeTranslatableName = StringArgumentType.getString(context, "gamemode");
32
33
        // Get the server
34
        var server = context.getSource().getServer();
35
36
        // Get the new game mode
37
        var newGameMode = GameModes.GAME_MODES.get(gameModeTranslatableName);
38
39
        // Sanity check
40
        if (newGameMode == null) {
41
            return Optional.of(new CommandFeedback(Text.literal("Invalid game mode.").formatted(Formatting.RED), false, false));
42
        }
43
44
        // Set the game mode
45
        LasertagGameManager.getInstance().getGameModeManager().setGameMode(server, newGameMode);
46
47
        // Translate the game mode name
48
        var translatedGameModeName = Text.translatable(gameModeTranslatableName).getString();
49
50
        return Optional.of(new CommandFeedback(Text.literal("Game mode changed to '" + translatedGameModeName + "'."), false, true));
51
    }
52
53
    static void register(LiteralArgumentBuilder<ServerCommandSource> lab) {
54
        lab.then(literal("gamemode")
55
                        .then(argument("gamemode", word())
56
                                .suggests(LasertagGameModeSuggestionProvider.getInstance())
57
                                .executes(new LasertagGameModeCommand())));
58
    }
59
}
60