From 65bfe795f35a6f4c19e383b2f5f4304ce1dfed3a Mon Sep 17 00:00:00 2001 From: Matyrobbrt Date: Mon, 14 Oct 2024 13:12:05 +0300 Subject: [PATCH] Track wins correctly --- .../crafting_bee/CraftingBeeBehavior.java | 31 +++++++++++++++---- .../behaviour/VictoryPointsBehavior.java | 19 ++++++++++++ 2 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/lovetropics/minigames/common/content/crafting_bee/CraftingBeeBehavior.java b/src/main/java/com/lovetropics/minigames/common/content/crafting_bee/CraftingBeeBehavior.java index 1dce916cf..4ae575f2e 100644 --- a/src/main/java/com/lovetropics/minigames/common/content/crafting_bee/CraftingBeeBehavior.java +++ b/src/main/java/com/lovetropics/minigames/common/content/crafting_bee/CraftingBeeBehavior.java @@ -16,6 +16,7 @@ import com.lovetropics.minigames.common.core.game.client_state.GameClientState; import com.lovetropics.minigames.common.core.game.client_state.instance.CraftingBeeCrafts; import com.lovetropics.minigames.common.core.game.player.PlayerSet; +import com.lovetropics.minigames.common.core.game.state.statistics.PlayerKey; import com.lovetropics.minigames.common.core.game.state.statistics.StatisticKey; import com.lovetropics.minigames.common.core.game.state.team.GameTeam; import com.lovetropics.minigames.common.core.game.state.team.GameTeamKey; @@ -24,12 +25,18 @@ import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.ChatFormatting; +import net.minecraft.core.BlockPos; import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.level.ServerLevel; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.Container; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.InteractionResult; import net.minecraft.world.entity.player.Player; import net.minecraft.world.item.ItemStack; import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.phys.BlockHitResult; import java.util.ArrayList; import java.util.Collection; @@ -55,6 +62,7 @@ public class CraftingBeeBehavior implements IGameBehavior { private IGamePhase game; private ListMultimap tasks; + private volatile boolean done; public CraftingBeeBehavior(List selectors, List decomposers, int allowedHints) { this.selectors = selectors; @@ -72,8 +80,8 @@ public void register(IGamePhase game, EventRegistrar events) throws GameExceptio teams = game.instanceState().getOrThrow(TeamState.KEY); events.listen(GamePhaseEvents.START, this::start); - events.listen(GamePlayerEvents.CRAFT, this::onCraft); + events.listen(GamePlayerEvents.USE_BLOCK, this::useBlock); } private void start() { @@ -131,7 +139,7 @@ private Stream singleDecomposition(Ingredient ingredient) { private void onCraft(Player player, ItemStack crafted, Container container) { var team = teams.getTeamForPlayer(player); - if (team == null) return; + if (team == null || done) return; var teamTasks = tasks.get(team); var task = teamTasks.stream().filter(c -> ItemStack.isSameItemSameComponents(crafted, c.output)).findFirst().orElse(null); @@ -142,22 +150,33 @@ private void onCraft(Player player, ItemStack crafted, Container container) { sync(team); var completed = teamTasks.stream().filter(t -> t.done).count(); - var teamName = teams.getTeamByKey(team).config().styledName(); + var teamConfig = teams.getTeamByKey(team).config(); - game.allPlayers().sendMessage(CraftingBeeTexts.TEAM_HAS_COMPLETED_RECIPES.apply(teamName, completed, teamTasks.size())); + game.allPlayers().sendMessage(CraftingBeeTexts.TEAM_HAS_COMPLETED_RECIPES.apply(teamConfig.styledName(), completed, teamTasks.size())); if (completed == teamTasks.size()) { game.statistics().global().set(StatisticKey.WINNING_TEAM, team); - game.invoker(GameLogicEvents.WIN_TRIGGERED).onWinTriggered(teamName); + game.invoker(GameLogicEvents.WIN_TRIGGERED).onWinTriggered(teamConfig.name()); + game.invoker(GameLogicEvents.GAME_OVER).onGameOver(); + + done = true; game.allPlayers().forEach(ServerPlayer::closeContainer); - game.schedule(1.5f, () -> game.allPlayers().sendMessage(MinigameTexts.TEAM_WON.apply(teamName).withStyle(ChatFormatting.GREEN), true)); + game.schedule(1.5f, () -> game.allPlayers().sendMessage(MinigameTexts.TEAM_WON.apply(teamConfig.styledName()).withStyle(ChatFormatting.GREEN), true)); game.schedule(5, () -> game.requestStop(GameStopReason.finished())); } } + private InteractionResult useBlock(ServerPlayer player, ServerLevel world, BlockPos pos, InteractionHand hand, BlockHitResult traceResult) { + // don't allow players to use the crafting table after the game was won + if (world.getBlockState(pos).is(Blocks.CRAFTING_TABLE) && done) { + return InteractionResult.FAIL; + } + return InteractionResult.PASS; + } + private void sync(GameTeamKey team) { teams.getPlayersForTeam(team).forEach(this::sync); } diff --git a/src/main/java/com/lovetropics/minigames/common/content/river_race/behaviour/VictoryPointsBehavior.java b/src/main/java/com/lovetropics/minigames/common/content/river_race/behaviour/VictoryPointsBehavior.java index 7a5cf9520..bf5a2d29c 100644 --- a/src/main/java/com/lovetropics/minigames/common/content/river_race/behaviour/VictoryPointsBehavior.java +++ b/src/main/java/com/lovetropics/minigames/common/content/river_race/behaviour/VictoryPointsBehavior.java @@ -9,6 +9,9 @@ import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar; import com.lovetropics.minigames.common.core.game.behavior.event.GameLogicEvents; import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents; +import com.lovetropics.minigames.common.core.game.state.team.GameTeam; +import com.lovetropics.minigames.common.core.game.state.team.GameTeamKey; +import com.lovetropics.minigames.common.core.game.state.team.TeamState; import com.mojang.serialization.Codec; import com.mojang.serialization.MapCodec; import com.mojang.serialization.codecs.RecordCodecBuilder; @@ -59,6 +62,15 @@ private void onWinTriggered(Component component) { if (Objects.equals(player.getDisplayName(), component)) { tryAddPoints(player, pointsPerGameWon); player.displayClientMessage(Component.literal("YOU WIN!!!! Victory points for team: " + getPoints(player)), false); + return; + } + } + + var teams = game.instanceState().getOrNull(TeamState.KEY); + if (teams == null) return; + for (final GameTeam team : teams) { + if (Objects.equals(team.config().name(), component)) { + tryAddPoints(team.key(), pointsPerGameWon); } } } @@ -70,6 +82,13 @@ private void tryAddPoints(final ServerPlayer player, final int points) { } } + private void tryAddPoints(final GameTeamKey team, final int points) { + final VictoryPointsGameState pointState = state(); + if (pointState != null) { + pointState.addPointsToTeam(team, points); + } + } + private int getPoints(final ServerPlayer player) { final VictoryPointsGameState gameState = state(); if (gameState != null) {