Skip to content

Commit

Permalink
Switch microgame queue to an action
Browse files Browse the repository at this point in the history
  • Loading branch information
Matyrobbrt committed Oct 25, 2024
1 parent 2ca9186 commit b1fc9a0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@

import com.lovetropics.minigames.LoveTropics;
import com.lovetropics.minigames.client.CustomItemRenderers;
import com.lovetropics.minigames.client.render.block.TriviaChestRenderer;
import com.lovetropics.minigames.common.content.river_race.behaviour.MicrogamesBehaviour;
import com.lovetropics.minigames.common.content.river_race.behaviour.StartMicrogamesAction;
import com.lovetropics.minigames.common.content.river_race.behaviour.RiverRaceMerchantBehavior;
import com.lovetropics.minigames.common.content.river_race.behaviour.TriviaBehaviour;
import com.lovetropics.minigames.common.content.river_race.behaviour.VictoryPointsBehavior;
Expand All @@ -18,15 +17,12 @@
import com.tterrag.registrate.util.entry.BlockEntry;
import net.minecraft.core.registries.Registries;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.SoundType;
import net.minecraft.world.level.block.state.BlockBehaviour;
import net.minecraft.world.level.block.state.properties.NoteBlockInstrument;
import net.minecraft.world.level.material.MapColor;

public class RiverRace {
private static final LoveTropicsRegistrate REGISTRATE = LoveTropics.registrate();
public static final GameBehaviorEntry<TriviaBehaviour> TRIVIA_BEHAVIOUR = REGISTRATE.object("trivia").behavior(TriviaBehaviour.CODEC).register();
public static final GameBehaviorEntry<MicrogamesBehaviour> MICROGAMES_BEHAVIOUR = REGISTRATE.object("microgames").behavior(MicrogamesBehaviour.CODEC).register();
public static final GameBehaviorEntry<StartMicrogamesAction> START_MICROGAMES_ACTION = REGISTRATE.object("start_microgames").behavior(StartMicrogamesAction.CODEC).register();
public static final GameBehaviorEntry<VictoryPointsBehavior> VICTORY_POINTS_BEHAVIOR = REGISTRATE.object("victory_points").behavior(VictoryPointsBehavior.CODEC).register();
public static final GameBehaviorEntry<RiverRaceMerchantBehavior> RIVER_RACE_MERCHANT_BEHAVIOR = REGISTRATE.object("river_race_merchant").behavior(RiverRaceMerchantBehavior.CODEC).register();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,34 @@
package com.lovetropics.minigames.common.content.river_race.behaviour;

import com.lovetropics.minigames.common.core.game.GameException;
import com.lovetropics.minigames.common.core.game.GamePhaseType;
import com.lovetropics.minigames.common.core.game.IGamePhase;
import com.lovetropics.minigames.common.core.game.behavior.IGameBehavior;
import com.lovetropics.minigames.common.core.game.behavior.event.EventRegistrar;
import com.lovetropics.minigames.common.core.game.behavior.event.GamePlayerEvents;
import com.lovetropics.minigames.common.core.game.config.GameConfig;
import com.lovetropics.minigames.common.core.game.config.GameConfigs;
import com.lovetropics.minigames.common.core.game.impl.GamePhase;
import com.lovetropics.minigames.common.core.game.behavior.event.GameActionEvents;
import com.lovetropics.minigames.common.core.game.impl.MultiGamePhase;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;
import net.minecraft.network.chat.PlayerChatMessage;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.ExtraCodecs;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public record MicrogamesBehaviour(List<ResourceLocation> gameConfigs, int gamesPerRound) implements IGameBehavior {
public record StartMicrogamesAction(List<ResourceLocation> gameConfigs, int gamesPerRound) implements IGameBehavior {

public static final MapCodec<MicrogamesBehaviour> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
ExtraCodecs.nonEmptyList(ResourceLocation.CODEC.listOf()).fieldOf("games").forGetter(MicrogamesBehaviour::gameConfigs),
public static final MapCodec<StartMicrogamesAction> CODEC = RecordCodecBuilder.mapCodec(i -> i.group(
ExtraCodecs.nonEmptyList(ResourceLocation.CODEC.listOf()).fieldOf("games").forGetter(StartMicrogamesAction::gameConfigs),
Codec.INT.optionalFieldOf("games_per_round", 1).forGetter(c -> c.gamesPerRound)
).apply(i, MicrogamesBehaviour::new));
).apply(i, StartMicrogamesAction::new));

@Override
public void register(IGamePhase game, EventRegistrar events) throws GameException {
events.listen(GamePlayerEvents.CHAT, (ServerPlayer player, PlayerChatMessage message) -> {
events.listen(GameActionEvents.APPLY, context -> {
queueMicrogames(game);
//startMicrogame(game);
startQueuedMicrogame(game);
return false;
return true;
});
}

Expand All @@ -54,13 +47,4 @@ public void startQueuedMicrogame(final IGamePhase game) {
multiGamePhase.startNextQueuedMicrogame(true);
}
}

public void startMicrogame(IGamePhase game, String gameName){
if (game instanceof MultiGamePhase multiGamePhase) {
GameConfig gameConfig = GameConfigs.REGISTRY.get(ResourceLocation.parse(gameName));
GamePhase.create(multiGamePhase.game(), gameConfig.getPlayingPhase(), GamePhaseType.PLAYING).thenAccept((result) -> {
multiGamePhase.setActivePhase(result.getOk(), true);
});
}
}
}

0 comments on commit b1fc9a0

Please sign in to comment.