Skip to content

Commit f499da1

Browse files
committed
Fix: properly clear out player state when leaving from within a microgame
1 parent 4554684 commit f499da1

File tree

4 files changed

+25
-7
lines changed

4 files changed

+25
-7
lines changed

src/main/java/com/lovetropics/minigames/common/core/game/impl/GameLobby.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,10 +228,10 @@ void onPlayerLoggedIn(ServerPlayer player) {
228228
trackingPlayers.onPlayerLoggedIn(player);
229229
}
230230

231-
void onPlayerLoggedOut(ServerPlayer player) {
231+
ServerPlayer onPlayerLoggedOut(ServerPlayer player) {
232232
trackingPlayers.onPlayerLoggedOut(player);
233233

234-
players.remove(player, true);
234+
return players.logOut(player);
235235
}
236236

237237
void onPlayerRegister(ServerPlayer player) {
@@ -247,7 +247,7 @@ void onPlayerRegister(ServerPlayer player) {
247247
management.onPlayersChanged();
248248
}
249249

250-
void onPlayerLeave(ServerPlayer player, boolean loggingOut) {
250+
ServerPlayer onPlayerLeave(ServerPlayer player, boolean loggingOut) {
251251
GamePhase phase = state.getTopPhase();
252252
if (phase != null) {
253253
player = phase.onPlayerLeave(player, loggingOut);
@@ -261,6 +261,8 @@ void onPlayerLeave(ServerPlayer player, boolean loggingOut) {
261261
manager.removePlayerFromLobby(player, this);
262262

263263
rewardsMap.grant(player);
264+
265+
return player;
264266
}
265267

266268
// TODO: better abstract this logic?

src/main/java/com/lovetropics/minigames/common/core/game/impl/LobbyPlayerManager.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,14 @@ public boolean remove(ServerPlayer player, boolean loggingOut) {
8888
return false;
8989
}
9090

91+
public ServerPlayer logOut(ServerPlayer player) {
92+
if (registrations.remove(player.getUUID())) {
93+
player = lobby.onPlayerLeave(player, true);
94+
roleSelections.remove(player);
95+
}
96+
return player;
97+
}
98+
9199
@Override
92100
public boolean forceRole(ServerPlayer player, @Nullable PlayerRole role) {
93101
return registrations.forceRole(player.getUUID(), role);

src/main/java/com/lovetropics/minigames/common/core/game/impl/MultiGameManager.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -267,14 +267,13 @@ public static void onPlayerLoggedIn(PlayerEvent.PlayerLoggedInEvent event) {
267267
* Also if they have registered for a game poll, they will be removed from the
268268
* list of registered players.
269269
*/
270-
@SubscribeEvent
271-
public static void onPlayerLoggedOut(PlayerEvent.PlayerLoggedOutEvent event) {
272-
ServerPlayer player = (ServerPlayer) event.getEntity();
270+
public static ServerPlayer onPlayerLoggedOut(ServerPlayer player) {
273271
if (!PlayerIsolation.INSTANCE.isReloading(player)) {
274272
for (GameLobby lobby : INSTANCE.lobbies) {
275-
lobby.onPlayerLoggedOut(player);
273+
player = lobby.onPlayerLoggedOut(player);
276274
}
277275
}
276+
return player;
278277
}
279278

280279
@SubscribeEvent

src/main/java/com/lovetropics/minigames/mixin/PlayerListMixin.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.lovetropics.minigames.common.core.game.PlayerIsolation;
44
import com.lovetropics.minigames.common.core.game.PlayerListAccess;
5+
import com.lovetropics.minigames.common.core.game.impl.MultiGameManager;
56
import net.minecraft.nbt.CompoundTag;
67
import net.minecraft.server.MinecraftServer;
78
import net.minecraft.server.level.ServerPlayer;
@@ -13,6 +14,7 @@
1314
import org.spongepowered.asm.mixin.Shadow;
1415
import org.spongepowered.asm.mixin.injection.At;
1516
import org.spongepowered.asm.mixin.injection.Inject;
17+
import org.spongepowered.asm.mixin.injection.ModifyVariable;
1618
import org.spongepowered.asm.mixin.injection.callback.CallbackInfo;
1719

1820
import javax.annotation.Nullable;
@@ -81,4 +83,11 @@ private void save(final ServerPlayer player, final CallbackInfo ci) {
8183
public void ltminigames$firePlayerLoading(final ServerPlayer player) {
8284
EventHooks.firePlayerLoadingEvent(player, playerIo.getPlayerDir(), player.getStringUUID());
8385
}
86+
87+
// We need to run the rest of the logic with the new player instance
88+
// FIXME: It would be nice to not need to produce new player instances in the logout process - but microgames need it right now to pull players all the way out
89+
@ModifyVariable(method = "remove", at = @At(value= "HEAD"), argsOnly = true)
90+
private ServerPlayer onPlayerLogOut(ServerPlayer player) {
91+
return MultiGameManager.onPlayerLoggedOut(player);
92+
}
8493
}

0 commit comments

Comments
 (0)