Skip to content

Commit

Permalink
Track wins correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Oct 26, 2024
1 parent b7877d5 commit 65bfe79
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -55,6 +62,7 @@ public class CraftingBeeBehavior implements IGameBehavior {
private IGamePhase game;

private ListMultimap<GameTeamKey, CraftingTask> tasks;
private volatile boolean done;

public CraftingBeeBehavior(List<RecipeSelector> selectors, List<IngredientDecomposer> decomposers, int allowedHints) {
this.selectors = selectors;
Expand All @@ -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() {
Expand Down Expand Up @@ -131,7 +139,7 @@ private Stream<ItemStack> 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);
Expand All @@ -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);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit 65bfe79

Please sign in to comment.