diff --git a/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java b/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java index 792b4843..da551289 100644 --- a/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java +++ b/src/main/java/xyz/nucleoid/plasmid/command/GameCommand.java @@ -287,11 +287,9 @@ private static void joinAllPlayersToGame(ServerCommandSource source, GameSpace g .collect(Collectors.toList()); var intent = JoinIntent.ANY; - var screen = gameSpace.getPlayers().screenJoins(players, intent); - if (screen.isOk()) { - gameSpace.getPlayers().offer(players, intent); - } else { - source.sendError(screen.errorCopy().formatted(Formatting.RED)); + var result = gameSpace.getPlayers().offer(players, intent); + if (result.isError()) { + source.sendError(result.errorCopy().formatted(Formatting.RED)); } } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/GameSpacePlayers.java b/src/main/java/xyz/nucleoid/plasmid/game/GameSpacePlayers.java index 81def39d..080312c0 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/GameSpacePlayers.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/GameSpacePlayers.java @@ -17,18 +17,17 @@ */ public interface GameSpacePlayers extends PlayerSet { /** - * Screens a player or group of players and returns whether the collective group should be allowed into the game. + * Simulates offer to join a player or group of players and returns whether they should be allowed into the game. *

- * This logic is controlled through the active {@link GameActivity} through {@link GamePlayerEvents#SCREEN_JOINS}. + * This logic is controlled through the active {@link GameActivity} through {@link GamePlayerEvents#OFFER}. * * @param players the group of players trying to join * @param intent the intent of the players trying to join, such as whether they want to participate or spectate * @return a {@link GameResult} describing whether these players can join this game, or an error if not - * @see GamePlayerEvents#SCREEN_JOINS - * @see GameSpacePlayers#offer(Collection, JoinIntent) + * @see GameSpacePlayers#offer(Collection, JoinIntent) * @see xyz.nucleoid.plasmid.game.player.GamePlayerJoiner */ - GameResult screenJoins(Collection players, JoinIntent intent); + GameResult simulateOffer(Collection players, JoinIntent intent); /** * Offers a player or group of players to join this game. If accepted, they will be teleported into the game, and if not diff --git a/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java b/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java index ecd4acdf..bef63af3 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/common/GameWaitingLobby.java @@ -89,7 +89,6 @@ public static GameWaitingLobby addTo(GameActivity activity, PlayerConfig playerC activity.listen(GameActivityEvents.TICK, lobby::onTick); activity.listen(GameActivityEvents.REQUEST_START, lobby::requestStart); - activity.listen(GamePlayerEvents.SCREEN_JOINS, (players, intent) -> lobby.screenJoins(players)); activity.listen(GamePlayerEvents.OFFER, lobby::offerPlayer); activity.listen(GamePlayerEvents.REMOVE, lobby::onRemovePlayer); @@ -170,17 +169,9 @@ private GameResult requestStart() { } } - private GameResult screenJoins(Collection players) { - int newPlayerCount = this.gameSpace.getPlayers().size() + players.size(); - if (newPlayerCount > this.playerConfig.maxPlayers()) { - return GameResult.error(GameTexts.Join.gameFull()); - } - - return GameResult.ok(); - } - private JoinOfferResult offerPlayer(JoinOffer offer) { - if (this.isFull()) { + int newPlayerCount = this.gameSpace.getPlayers().size() + offer.players().size(); + if (newPlayerCount > this.playerConfig.maxPlayers()) { return offer.reject(GameTexts.Join.gameFull()); } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/event/GamePlayerEvents.java b/src/main/java/xyz/nucleoid/plasmid/game/event/GamePlayerEvents.java index 66856b98..e1ca59c4 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/event/GamePlayerEvents.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/event/GamePlayerEvents.java @@ -101,39 +101,15 @@ public final class GamePlayerEvents { } }); - /** - * Called when a group of players try to join this game. This should be used to reject multiple players as a group, - * such as when a party tries to join but has too many players to fit into the game. - *

- * This is called before {@link GamePlayerEvents#OFFER} which handles specifically bringing a player into the game. - * - * @see GamePlayerEvents#OFFER - */ - public static final StimulusEvent SCREEN_JOINS = StimulusEvent.create(ScreenJoins.class, ctx -> (players, intent) -> { - try { - for (var listener : ctx.getListeners()) { - var result = listener.screenJoins(players, intent); - if (result.isError()) { - return result; - } - } - return GameResult.ok(); - } catch (Throwable throwable) { - ctx.handleException(throwable); - return GameResult.error(GameTexts.Join.unexpectedError()); - } - }); - /** * Called when a single {@link ServerPlayerEntity} tries to join this game. This event is responsible for bringing * the player into the {@link GameSpace} world in the correct location. *

* Games must respond to this event in order for a player to be able to join by returning either - * {@link JoinOffer#accept(ServerWorld, Vec3d)} or {@link JoinOffer#reject(Text)}. + * {@link JoinOffer#accept(ServerWorld)} or {@link JoinOffer#reject(Text)}. * * @see JoinOffer * @see JoinOfferResult - * @see GamePlayerEvents#SCREEN_JOINS * @see GamePlayerEvents#JOIN */ public static final StimulusEvent OFFER = StimulusEvent.create(Offer.class, ctx -> offer -> { @@ -175,10 +151,6 @@ public interface Remove { void onRemovePlayer(ServerPlayerEntity player); } - public interface ScreenJoins { - GameResult screenJoins(Collection players, JoinIntent intent); - } - public interface Offer { JoinOfferResult onOfferPlayer(JoinOffer offer); } diff --git a/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java b/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java index 08e4f519..e0c5b5b6 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpace.java @@ -196,24 +196,6 @@ public GameBehavior getBehavior() { return this.state; } - GameResult screenJoins(Collection players, JoinIntent intent) { - var result = this.attemptScreenJoins(players.stream().map(PlayerEntity::getGameProfile).toList(), intent); - - if (result.isError()) { - this.players.attemptGarbageCollection(); - } - - return result; - } - - private GameResult attemptScreenJoins(Collection players, JoinIntent intent) { - if (this.closed) { - return GameResult.error(GameTexts.Join.gameClosed()); - } - - return this.state.invoker(GamePlayerEvents.SCREEN_JOINS).screenJoins(players, intent); - } - JoinOfferResult offerPlayer(LocalJoinOffer offer) { if (this.closed) { return offer.reject(GameTexts.Join.gameClosed()); diff --git a/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpacePlayers.java b/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpacePlayers.java index fd7540ad..65ebb64d 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpacePlayers.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/manager/ManagedGameSpacePlayers.java @@ -25,8 +25,18 @@ public final class ManagedGameSpacePlayers implements GameSpacePlayers { } @Override - public GameResult screenJoins(Collection players, JoinIntent intent) { - return this.space.screenJoins(players, intent); + public GameResult simulateOffer(Collection players, JoinIntent intent) { + if (players.stream().anyMatch(this.set::contains)) { + return GameResult.error(GameTexts.Join.alreadyJoined()); + } + + var offer = new LocalJoinOffer(players, intent); + + return switch (this.space.offerPlayer(offer)) { + case JoinOfferResult.Accept accept -> GameResult.ok(); + case JoinOfferResult.Reject reject -> GameResult.error(reject.reason()); + default -> GameResult.error(GameTexts.Join.genericError()); + }; } @Override diff --git a/src/main/java/xyz/nucleoid/plasmid/game/player/GamePlayerJoiner.java b/src/main/java/xyz/nucleoid/plasmid/game/player/GamePlayerJoiner.java index fbf89e5c..d2c3f939 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/player/GamePlayerJoiner.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/player/GamePlayerJoiner.java @@ -16,7 +16,7 @@ /** * Utility class for joining players to a {@link GameSpace}. This handles all logic such as collecting all party - * members, screening, and offering players to the {@link GameSpace}. + * members, and offering players to the {@link GameSpace}. */ public final class GamePlayerJoiner { public static Results tryJoin(ServerPlayerEntity player, GameSpace gameSpace, JoinIntent intent) { @@ -40,12 +40,6 @@ private static Set collectPlayersForJoin(ServerPlayerEntity private static Results tryJoinAll(Collection players, GameSpace gameSpace, JoinIntent intent) { var results = new Results(); - var screenResult = gameSpace.getPlayers().screenJoins(players, intent); - if (screenResult.isError()) { - results.globalError = screenResult.error(); - return results; - } - var result = gameSpace.getPlayers().offer(players, intent); if (result.isError()) { for (var player : players) { diff --git a/src/main/java/xyz/nucleoid/plasmid/game/player/JoinIntent.java b/src/main/java/xyz/nucleoid/plasmid/game/player/JoinIntent.java index f2bfe3f5..7cdc94ab 100644 --- a/src/main/java/xyz/nucleoid/plasmid/game/player/JoinIntent.java +++ b/src/main/java/xyz/nucleoid/plasmid/game/player/JoinIntent.java @@ -6,8 +6,7 @@ /** * Represents the "intention" of a player or group of players joining a {@link GameSpace}. * It is up to the game implementation to respect this intent in the way that is appropriate for their game. This may be - * accomplished by handling the {@link GamePlayerEvents#SCREEN_JOINS 'Screen Joins'} and - * {@link GamePlayerEvents#OFFER 'Join Offer'} events. + * accomplished by handling the {@link GamePlayerEvents#OFFER 'Join Offer'} events. */ public enum JoinIntent { /**