Skip to content

Commit

Permalink
Apply copious amounts of hacks to make microgames work consistently
Browse files Browse the repository at this point in the history
  • Loading branch information
Gegy committed Nov 14, 2024
1 parent 0fd6038 commit cbb73df
Show file tree
Hide file tree
Showing 11 changed files with 126 additions and 115 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ private ClientLobbyPlayer(UUID uuid, @Nullable PlayerRole playingRole) {
}

public static ClientLobbyPlayer from(IGameLobby lobby, ServerPlayer player) {
IGamePhase currentPhase = lobby.getCurrentPhase();
IGamePhase currentPhase = lobby.getActivePhase();
PlayerRole playingRole = currentPhase != null ? currentPhase.getRoleFor(player) : null;
return new ClientLobbyPlayer(player.getUUID(), playingRole);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.lovetropics.minigames.common.core.game.impl;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.lovetropics.minigames.client.lobby.state.ClientCurrentGame;
import com.lovetropics.minigames.client.lobby.state.message.JoinedLobbyMessage;
import com.lovetropics.minigames.client.lobby.state.message.LeftLobbyMessage;
Expand All @@ -21,18 +20,15 @@
import com.lovetropics.minigames.common.core.game.player.PlayerIterable;
import com.lovetropics.minigames.common.core.game.player.PlayerRoleSelections;
import com.lovetropics.minigames.common.core.game.rewards.GameRewardsMap;
import com.lovetropics.minigames.common.core.game.state.IGameState;
import com.lovetropics.minigames.common.core.game.util.GameTexts;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.network.chat.Component;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.util.Unit;
import net.neoforged.neoforge.network.PacketDistributor;

import javax.annotation.Nullable;
import java.util.Map;

// TODO: do we want a different game lobby implementation for something like carnival games?
/**
Expand Down Expand Up @@ -95,8 +91,9 @@ public LobbyGameQueue getGameQueue() {

@Nullable
@Override
public IGamePhase getCurrentPhase() {
return state.getPhase();
public IGamePhase getActivePhase() {
GamePhase phase = state.getTopPhase();
return phase != null ? phase.getActivePhase() : null;
}

@Nullable
Expand Down Expand Up @@ -242,16 +239,16 @@ void onPlayerRegister(ServerPlayer player) {

stateListener.onPlayerJoin(this, player);

GamePhase phase = state.getPhase();
GamePhase phase = state.getTopPhase();
if (phase != null) {
phase.onPlayerJoin(player);
phase.onPlayerJoin(player, false);
}

management.onPlayersChanged();
}

void onPlayerLeave(ServerPlayer player, boolean loggingOut) {
GamePhase phase = state.getPhase();
GamePhase phase = state.getTopPhase();
if (phase != null) {
player = phase.onPlayerLeave(player, loggingOut);
}
Expand Down Expand Up @@ -305,15 +302,15 @@ void close(boolean serverStopping) {
static final class ChatNotifyListener implements LobbyStateListener {
@Override
public void onPlayerJoin(IGameLobby lobby, ServerPlayer player) {
IGamePhase currentPhase = lobby.getCurrentPhase();
IGamePhase currentPhase = lobby.getActivePhase();
if (currentPhase != null && currentPhase.phaseType() == GamePhaseType.WAITING) {
onPlayerJoinGame(lobby, currentPhase);
}
}

@Override
public void onPlayerLeave(IGameLobby lobby, ServerPlayer player) {
IGamePhase currentPhase = lobby.getCurrentPhase();
IGamePhase currentPhase = lobby.getActivePhase();
if (currentPhase != null && currentPhase.phaseType() == GamePhaseType.WAITING) {
onPlayerLeaveGame(lobby, currentPhase);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,12 +255,14 @@ private void onSetPlayerRole(ServerPlayer player, @Nullable PlayerRole role, @Nu
}
}

void onPlayerJoin(ServerPlayer player) {
ServerPlayer onPlayerJoin(ServerPlayer player, boolean savePlayerDataToMemory) {
try {
ServerPlayer newPlayer = addAndSpawnPlayer(player, null, false);
ServerPlayer newPlayer = addAndSpawnPlayer(player, getRoleFor(player), savePlayerDataToMemory);
invoker(GamePlayerEvents.JOIN).onAdd(newPlayer);
return newPlayer;
} catch (Exception e) {
LoveTropics.LOGGER.warn("Failed to dispatch player join event", e);
return player;
}
}

Expand All @@ -284,6 +286,18 @@ ServerPlayer onPlayerLeave(ServerPlayer player, boolean loggingOut) {
return PlayerIsolation.INSTANCE.restore(player);
}

void removePlayer(ServerPlayer player) {
addedPlayers.remove(player.getUUID());
for (PlayerRole role : PlayerRole.ROLES) {
roles.get(role).remove(player);
}
try {
invoker(GamePlayerEvents.REMOVE).onRemove(player);
} catch (Exception e) {
LoveTropics.LOGGER.warn("Failed to dispatch player leave event", e);
}
}

public void cancelWithError(Exception exception) {
LoveTropics.LOGGER.warn("Game canceled due to exception", exception);
requestStop(GameStopReason.errored(Component.literal("Game stopped due to exception: " + exception)));
Expand Down Expand Up @@ -359,4 +373,8 @@ public GameScheduler scheduler() {
public long ticks() {
return level().getGameTime() - startTime;
}

public IGamePhase getActivePhase() {
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public GameInstance getGame() {
}

@Nullable
public GamePhase getPhase() {
public GamePhase getTopPhase() {
return state.phase;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public GameLobby getLobbyFor(Player player) {
@Override
public IGamePhase getGamePhaseFor(Player player) {
GameLobby lobby = getLobbyFor(player);
return lobby != null ? lobby.getCurrentPhase() : null;
return lobby != null ? lobby.getActivePhase() : null;
}

@Nullable
Expand Down Expand Up @@ -297,7 +297,12 @@ public static void onPlayerTryChangeDimension(EntityTravelToDimensionEvent event
}

private static boolean canTravelBetweenPhases(@Nullable IGamePhase from, @Nullable IGamePhase to) {
return to == null || from == to;
if (to == null) {
return true;
} else if (from == null) {
return false;
}
return from.lobby() == to.lobby();
}

@SubscribeEvent
Expand Down
Loading

0 comments on commit cbb73df

Please sign in to comment.