Skip to content

Commit

Permalink
Add the kick subcommand to the game command
Browse files Browse the repository at this point in the history
  • Loading branch information
haykam821 authored and Gegy committed Sep 4, 2022
1 parent b866866 commit 15355b2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ public static void register(CommandDispatcher<ServerCommandSource> dispatcher) {
.executes(GameCommand::stopGameConfirmed)
)
)
.then(literal("kick")
.requires(source -> source.hasPermissionLevel(2))
.then(argument("targets", EntityArgumentType.players())
.executes(GameCommand::kickPlayers)
)
)
.then(literal("join")
.executes(GameCommand::joinGame)
.then(GameSpaceArgument.argument("game_space")
Expand Down Expand Up @@ -400,4 +406,29 @@ private static int listGames(CommandContext<ServerCommandSource> context) {

return Command.SINGLE_SUCCESS;
}

private static int kickPlayers(CommandContext<ServerCommandSource> context) throws CommandSyntaxException {
var source = context.getSource();
var playerManager = source.getServer().getPlayerManager();

var targets = EntityArgumentType.getPlayers(context, "targets");

int successes = 0;

for (var target : targets) {
var gameSpace = GameSpaceManager.get().byPlayer(target);
if (gameSpace != null) {
var message = GameTexts.Kick.kick(source, target).formatted(Formatting.GRAY);
playerManager.broadcast(message, false);

Scheduler.INSTANCE.submit(server -> {
gameSpace.getPlayers().kick(target);
});

successes += 1;
}
}

return successes;
}
}
14 changes: 14 additions & 0 deletions src/main/java/xyz/nucleoid/plasmid/game/GameTexts.java
Original file line number Diff line number Diff line change
Expand Up @@ -145,4 +145,18 @@ public static MutableText inOtherGame() {
);
}
}

public static final class Kick {
public static MutableText kick(ServerCommandSource source, ServerPlayerEntity target) {
return source.isExecutedByPlayer() ? kickBy(source.getPlayer(), target) : kick(target);
}

public static MutableText kickBy(ServerPlayerEntity source, ServerPlayerEntity target) {
return Text.translatable("text.plasmid.game.kick.by", target.getDisplayName(), source.getDisplayName());
}

public static MutableText kick(ServerPlayerEntity target) {
return Text.translatable("text.plasmid.game.kick", target.getDisplayName());
}
}
}
2 changes: 2 additions & 0 deletions src/main/resources/data/plasmid/lang/en_us.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
"text.plasmid.game.join.error": "An unexpected exception occurred while joining game!",
"text.plasmid.game.join.no_game_open": "No games are open!",
"text.plasmid.game.join.party.error": "%s players were unable to join this game!",
"text.plasmid.game.kick": "%s was kicked from the game!",
"text.plasmid.game.kick.by": "%s was kicked from the game by %s!",
"text.plasmid.game.list": "Registered games:",
"text.plasmid.game.locate.located": "%s is currently in the %s game. ",
"text.plasmid.game.locate.player_not_in_game": "%s is not in a game!",
Expand Down

0 comments on commit 15355b2

Please sign in to comment.