Skip to content

Commit

Permalink
Merge pull request #31 from Gaming32/resign
Browse files Browse the repository at this point in the history
Add `/bingo resign`
  • Loading branch information
Gaming32 authored Oct 24, 2024
2 parents d9fe72c + bf53a1c commit c17fce0
Show file tree
Hide file tree
Showing 4 changed files with 167 additions and 24 deletions.
8 changes: 2 additions & 6 deletions common/src/main/java/io/github/gaming32/bingo/Bingo.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,12 +112,8 @@ private static void registerEventHandlers() {
});

Event.SERVER_TICK_END.register(instance -> {
if (activeGame != null && activeGame.isRequireClient()) {
for (final ServerPlayer player : new ArrayList<>(instance.getPlayerList().getPlayers())) {
if (player.tickCount == 60 && !Bingo.isInstalledOnClient(player)) {
player.connection.disconnect(BingoGame.REQUIRED_CLIENT_KICK);
}
}
if (activeGame != null) {
activeGame.tick(instance);
}
});

Expand Down
63 changes: 61 additions & 2 deletions common/src/main/java/io/github/gaming32/bingo/BingoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import net.minecraft.commands.arguments.ResourceKeyArgument;
import net.minecraft.commands.arguments.ResourceLocationArgument;
import net.minecraft.commands.arguments.TeamArgument;
import net.minecraft.commands.arguments.TimeArgument;
import net.minecraft.core.Holder;
import net.minecraft.core.HolderSet;
import net.minecraft.core.Registry;
Expand All @@ -60,6 +61,7 @@
import net.minecraft.world.inventory.MenuType;
import net.minecraft.world.level.levelgen.RandomSupport;
import net.minecraft.world.scores.PlayerTeam;
import org.apache.commons.lang3.ArrayUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

Expand Down Expand Up @@ -108,6 +110,12 @@ public class BingoCommand {
));
private static final SimpleCommandExceptionType NO_TEAMS =
new SimpleCommandExceptionType(Bingo.translatable("bingo.no_teams"));
private static final SimpleCommandExceptionType NOT_IN_TEAM =
new SimpleCommandExceptionType(Bingo.translatable("bingo.not_in_team"));
private static final SimpleCommandExceptionType RESIGN_ALREADY_FINISHED =
new SimpleCommandExceptionType(Bingo.translatable("bingo.resign.already_finished"));
private static final DynamicCommandExceptionType TEAM_NOT_PLAYING =
new DynamicCommandExceptionType(team -> Bingo.translatableEscape("bingo.team_not_playing", team));

private static final SuggestionProvider<CommandSourceStack> ACTIVE_GOAL_SUGGESTOR = (context, builder) -> {
if (Bingo.activeGame == null) {
Expand Down Expand Up @@ -149,6 +157,14 @@ public static void register(
.requires(source -> source.hasPermission(2))
.executes(BingoCommand::resetGame)
)
.then(literal("resign")
.requires(source -> Bingo.activeGame != null)
.executes(ctx -> resign(ctx.getSource()))
.then(argument("team", TeamArgument.team())
.requires(source -> source.hasPermission(2) && Bingo.activeGame != null)
.executes(ctx -> resign(ctx.getSource(), TeamArgument.getTeam(ctx, "team")))
)
)
.then(literal("board")
.requires(source -> Bingo.activeGame != null)
.executes(ctx -> {
Expand Down Expand Up @@ -406,6 +422,11 @@ public Component getDisplayName() {
.then(literal("--continue-after-win")
.redirect(startCommand, CommandSourceStackExt.COPY_CONTEXT)
)
.then(literal("--auto-resign-time")
.then(argument("auto_resign_time", TimeArgument.time(0))
.redirect(startCommand, CommandSourceStackExt.COPY_CONTEXT)
)
)
)
);
CommandNode<CommandSourceStack> currentCommand = startCommand;
Expand Down Expand Up @@ -440,16 +461,21 @@ private static int startGame(CommandContext<CommandSourceStack> context) throws
final boolean requireClient = hasNode(context, "--require-client");
final boolean persistent = hasNode(context, "--persistent");
final boolean continueAfterWin = hasNode(context, "--continue-after-win");
final int autoResignTicks = getArg(context, "auto_resign_time", () -> BingoGame.DEFAULT_AUTO_RESIGN_TICKS, IntegerArgumentType::getInteger);

final Set<PlayerTeam> teams = new LinkedHashSet<>();
for (int i = 1; i <= 32; i++) {
final String argName = "team" + i;
if (!hasArg(context, argName)) break;
final PlayerTeam team = TeamArgument.getTeam(context, argName);
if (!teams.add(team)) {
boolean hasAnyActivePlayers = team.getPlayers().stream().anyMatch(playerName -> context.getSource().getServer().getPlayerList().getPlayerByName(playerName) != null);
if (hasAnyActivePlayers && !teams.add(team)) {
throw DUPLICATE_TEAMS.create(team);
}
}
if (teams.isEmpty()) {
throw NO_TEAMS.create();
}

final List<BingoGoal.GoalHolder> requiredGoals = requiredGoalIds.stream()
.map(id -> {
Expand Down Expand Up @@ -498,7 +524,7 @@ private static int startGame(CommandContext<CommandSourceStack> context) throws
}
Bingo.LOGGER.info("Generated board (seed {}):\n{}", seed, board);

Bingo.activeGame = new BingoGame(board, gamemode, requireClient, persistent, continueAfterWin, teams.toArray(PlayerTeam[]::new));
Bingo.activeGame = new BingoGame(board, gamemode, requireClient, persistent, continueAfterWin, autoResignTicks, teams.toArray(PlayerTeam[]::new));
Bingo.updateCommandTree(playerList);
new ArrayList<>(playerList.getPlayers()).forEach(Bingo.activeGame::addPlayer);
playerList.broadcastSystemMessage(
Expand All @@ -517,6 +543,39 @@ private static int resetGame(CommandContext<CommandSourceStack> context) {
return Command.SINGLE_SUCCESS;
}

private static int resign(CommandSourceStack source) throws CommandSyntaxException {
if (Bingo.activeGame == null) {
throw NO_GAME_RUNNING.create();
}
ServerPlayer player = source.getPlayerOrException();
BingoBoard.Teams team = Bingo.activeGame.getTeam(player);
if (!team.any()) {
throw NOT_IN_TEAM.create();
}
if (Bingo.activeGame.resign(source.getServer().getPlayerList(), team)) {
source.sendSuccess(() -> Bingo.translatable("bingo.resign.success"), false);
return Command.SINGLE_SUCCESS;
} else {
throw RESIGN_ALREADY_FINISHED.create();
}
}

private static int resign(CommandSourceStack source, PlayerTeam team) throws CommandSyntaxException {
if (Bingo.activeGame == null) {
throw NO_GAME_RUNNING.create();
}
int teamIndex = ArrayUtils.indexOf(Bingo.activeGame.getTeams(), team);
if (teamIndex == -1) {
throw TEAM_NOT_PLAYING.create(team.getFormattedDisplayName());
}
if (Bingo.activeGame.resign(source.getServer().getPlayerList(), BingoBoard.Teams.fromOne(teamIndex))) {
source.sendSuccess(() -> Bingo.translatable("bingo.resign.success.team", team.getFormattedDisplayName()), true);
return Command.SINGLE_SUCCESS;
} else {
throw RESIGN_ALREADY_FINISHED.create();
}
}

private static int randomizeTeams(
CommandContext<CommandSourceStack> context,
Collection<ServerPlayer> players,
Expand Down
Loading

0 comments on commit c17fce0

Please sign in to comment.