de.pewpewproject.lasertag.command.lasertag.game.KickPlayerCommand   A
last analyzed

Complexity

Total Complexity 5

Size/Duplication

Total Lines 35
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 35
rs 10
c 0
b 0
f 0
wmc 5

2 Methods

Rating   Name   Duplication   Size   Complexity  
A register(LiteralArgumentBuilder) 0 5 1
A execute(CommandContext) 0 25 4
1
package de.pewpewproject.lasertag.command.lasertag.game;
2
3
import com.mojang.brigadier.builder.LiteralArgumentBuilder;
4
import com.mojang.brigadier.context.CommandContext;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import de.pewpewproject.lasertag.command.CommandFeedback;
7
import de.pewpewproject.lasertag.command.ServerFeedbackCommand;
8
import net.minecraft.command.argument.EntityArgumentType;
9
import net.minecraft.server.command.ServerCommandSource;
10
import net.minecraft.server.network.ServerPlayerEntity;
11
import net.minecraft.text.Text;
12
import net.minecraft.util.Formatting;
13
14
import java.util.Collection;
15
import java.util.Optional;
16
17
import static net.minecraft.command.argument.EntityArgumentType.players;
18
import static net.minecraft.server.command.CommandManager.argument;
19
import static net.minecraft.server.command.CommandManager.literal;
20
21
/**
22
 * Command to kick a player out of his team
23
 *
24
 * @author Étienne Muser
25
 */
26
public class KickPlayerCommand extends ServerFeedbackCommand {
27
28
    @Override
29
    protected Optional<CommandFeedback> execute(CommandContext<ServerCommandSource> context) {
30
31
        // Get the game managers
32
        var gameManager = context.getSource().getWorld().getServerLasertagManager();
33
        var teamsManager = gameManager.getTeamsManager();
34
35
        Collection<ServerPlayerEntity> players;
36
        try {
37
            players = EntityArgumentType.getPlayers(context, "players");
38
        } catch (CommandSyntaxException e) {
39
            return Optional.of(new CommandFeedback(Text.literal("Invalid players").formatted(Formatting.RED), false, false));
40
        }
41
42
        for (var player : players) {
43
            teamsManager.playerLeaveHisTeam(player);
44
45
            player.getInventory().clear();
46
        }
47
48
        // Send successful feedback
49
        if (players.size() > 1) {
50
            return Optional.of(new CommandFeedback(Text.literal("Successfully kicked players from their teams."), false, true));
51
        } else {
52
            return Optional.of(new CommandFeedback(Text.literal("Successfully kicked player from his team."), false, true));
53
        }
54
    }
55
56
    static void register(LiteralArgumentBuilder<ServerCommandSource> lab) {
57
        lab.then(literal("kickPlayer")
58
                .requires(s -> s.hasPermissionLevel(1))
59
                .then(argument("players", players())
60
                        .executes(new KickPlayerCommand())));
61
    }
62
}
63