executeWithoutArgs(CommandContext)   A
last analyzed

Complexity

Conditions 1

Size

Total Lines 22
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 9
dl 0
loc 22
rs 9.95
c 0
b 0
f 0
1
package de.pewpewproject.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.pewpewproject.lasertag.command.CommandFeedback;
7
import de.pewpewproject.lasertag.command.ServerFeedbackCommand;
8
import de.pewpewproject.lasertag.command.suggestions.LasertagGameModeSuggestionProvider;
9
import de.pewpewproject.lasertag.lasertaggame.gamemode.GameModes;
10
import net.minecraft.server.command.ServerCommandSource;
11
import net.minecraft.text.Text;
12
import net.minecraft.util.Formatting;
13
14
import java.util.Optional;
15
16
import static com.mojang.brigadier.arguments.StringArgumentType.word;
17
import static net.minecraft.server.command.CommandManager.argument;
18
import static net.minecraft.server.command.CommandManager.literal;
19
20
/**
21
 * The lasertag game mode command
22
 *
23
 * @author Étienne Muser
24
 */
25
public class LasertagGameModeCommand extends ServerFeedbackCommand {
26
27
    @Override
28
    protected Optional<CommandFeedback> execute(CommandContext<ServerCommandSource> context) {
29
30
        // Get the game managers
31
        var gameManager = context.getSource().getWorld().getServerLasertagManager();
32
        var gameModeManager = gameManager.getGameModeManager();
33
34
        // Get the game mode translatable name
35
        var gameModeTranslatableName = StringArgumentType.getString(context, "gamemode");
36
37
        // Get the new game mode
38
        var newGameMode = GameModes.GAME_MODES.get(gameModeTranslatableName);
39
40
        // Sanity check
41
        if (newGameMode == null) {
42
            return Optional.of(new CommandFeedback(Text.literal("Invalid game mode.").formatted(Formatting.RED), false, false));
43
        }
44
45
        // Set the game mode
46
        gameModeManager.setGameMode(newGameMode);
47
48
        // Translate the game mode name
49
        var translatedGameModeName = Text.translatable(gameModeTranslatableName).getString();
50
51
        return Optional.of(new CommandFeedback(Text.literal("Game mode changed to '" + translatedGameModeName + "'."), false, true));
52
    }
53
54
    private static int executeWithoutArgs(CommandContext<ServerCommandSource> context) {
55
56
        // Get the source
57
        var source = context.getSource();
58
59
        // Get the player
60
        var player = source.getPlayer();
61
62
        // Get the game managers
63
        var gameManager = source.getWorld().getServerLasertagManager();
64
        var gameModeManager = gameManager.getGameModeManager();
65
66
        // Get the game mode name
67
        var gameModeName = Text.translatable(gameModeManager.getGameMode().getTranslatableName());
68
69
        // Build the message string
70
        var message = Text.translatable("chat.message.game_mode_print", gameModeName);
71
72
        // Send chat message to the player
73
        player.sendMessage(message, false);
74
75
        return SINGLE_SUCCESS;
76
    }
77
78
    static void register(LiteralArgumentBuilder<ServerCommandSource> lab) {
79
        lab.then(literal("gamemode")
80
                        .executes(LasertagGameModeCommand::executeWithoutArgs)
81
                        .then(argument("gamemode", word())
82
                                .requires(s -> s.hasPermissionLevel(1))
83
                                .suggests(LasertagGameModeSuggestionProvider.getInstance())
84
                                .executes(new LasertagGameModeCommand())));
85
    }
86
}
87