From 5c6590dd1cade1851decf52a69849625f0410aa7 Mon Sep 17 00:00:00 2001 From: ellieisjelly Date: Wed, 3 Jan 2024 17:24:05 -0300 Subject: [PATCH] i'm dumb, proper waiting lobby --- .../ellieis/Sabotage/game/SabotageConfig.java | 20 ++++-------- .../Sabotage/game/phase/SabotageWaiting.java | 32 +++---------------- .../data/sabotage/games/sabotage.json | 7 ++-- 3 files changed, 16 insertions(+), 43 deletions(-) diff --git a/src/main/java/me/ellieis/Sabotage/game/SabotageConfig.java b/src/main/java/me/ellieis/Sabotage/game/SabotageConfig.java index ad5b11d..5556979 100644 --- a/src/main/java/me/ellieis/Sabotage/game/SabotageConfig.java +++ b/src/main/java/me/ellieis/Sabotage/game/SabotageConfig.java @@ -3,30 +3,24 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.codecs.RecordCodecBuilder; import net.minecraft.util.Identifier; +import xyz.nucleoid.plasmid.game.common.config.PlayerConfig; public class SabotageConfig { private final Identifier map; - private final int maxPlayers; - private final int playersRequired; + private final PlayerConfig playerConfig; public static final Codec CODEC = RecordCodecBuilder.create(instance -> { return instance.group( Identifier.CODEC.fieldOf("map").forGetter(SabotageConfig::getMap), - Codec.INT.fieldOf("max_players").forGetter(SabotageConfig::getMaxPlayers), - Codec.INT.fieldOf("players_required").forGetter(SabotageConfig::getPlayersRequired) + PlayerConfig.CODEC.fieldOf("players").forGetter(SabotageConfig::getPlayerConfig) ).apply(instance, SabotageConfig::new); }); - public SabotageConfig(Identifier map, int maxPlayers, int playersRequired) { + public SabotageConfig(Identifier map, PlayerConfig playerConfig) { this.map = map; - this.maxPlayers = maxPlayers; - this.playersRequired = playersRequired; + this.playerConfig = playerConfig; } - public int getPlayersRequired() { - return this.playersRequired; - } - - public int getMaxPlayers() { - return this.maxPlayers; + public PlayerConfig getPlayerConfig() { + return this.playerConfig; } public Identifier getMap() { diff --git a/src/main/java/me/ellieis/Sabotage/game/phase/SabotageWaiting.java b/src/main/java/me/ellieis/Sabotage/game/phase/SabotageWaiting.java index 86678c0..55078fd 100644 --- a/src/main/java/me/ellieis/Sabotage/game/phase/SabotageWaiting.java +++ b/src/main/java/me/ellieis/Sabotage/game/phase/SabotageWaiting.java @@ -13,6 +13,7 @@ import net.minecraft.world.GameMode; import xyz.nucleoid.fantasy.RuntimeWorldConfig; import xyz.nucleoid.plasmid.game.*; +import xyz.nucleoid.plasmid.game.common.GameWaitingLobby; import xyz.nucleoid.plasmid.game.common.GlobalWidgets; import xyz.nucleoid.plasmid.game.common.widget.SidebarWidget; import xyz.nucleoid.plasmid.game.event.GameActivityEvents; @@ -61,18 +62,12 @@ public static GameOpenProcedure Open(GameOpenContext context) { return context.openWithWorld(worldConfig, (activity, world) -> { SabotageWaiting game = new SabotageWaiting(config, activity.getGameSpace(), map, world); - game.widget = GlobalWidgets.addTo(activity).addSidebar(); - game.widget.setTitle( - Text.translatableWithFallback("gameType.sabotage.sabotage", "Sabotage").formatted(Formatting.GOLD) - ); - game.widget.setLine(SidebarLine.create(1, Text.translatableWithFallback("sabotage.waiting", "Waiting for players."))); - // line 0 is reserved for player count in this phase. - // it's only set once a player joins. + GameWaitingLobby.addTo(activity, config.getPlayerConfig()); + rules(activity); activity.listen(GameActivityEvents.TICK, game::onTick); activity.listen(GamePlayerEvents.OFFER, game::onOffer); - activity.listen(GamePlayerEvents.JOIN, game::onJoin); - activity.listen(GamePlayerEvents.LEAVE, game::onLeave); + activity.listen(GameActivityEvents.REQUEST_START, game::requestStart); }); } @@ -86,27 +81,8 @@ private void onTick() { } - private void onJoin(ServerPlayerEntity plr) { - this.map.spawnEntity(this.world, plr); - this.widget.addPlayer(plr); - long playerCount = this.gameSpace.getPlayers().stream().count(); - this.widget.setLine(SidebarLine.create(0, Text.literal(playerCount + "/" + this.config.getMaxPlayers() + " Players" ))); - if (playerCount >= 4) { - //requestStart(); - } else { - } - } - - private void onLeave(ServerPlayerEntity _plr) { - long playerCount = this.gameSpace.getPlayers().stream().count(); - this.widget.setLine(SidebarLine.create(0, Text.literal(playerCount + "/" + this.config.getMaxPlayers() + " Players" ))); - } private PlayerOfferResult onOffer(PlayerOffer offer) { ServerPlayerEntity plr = offer.player(); - long playerCount = this.gameSpace.getPlayers().stream().count(); - if (playerCount >= this.config.getMaxPlayers()) { - return offer.reject(Text.translatableWithFallback("sabotage.full", "Game is full.")); - } return offer.accept(this.world, new Vec3d(0.0, 66.0, 0.0)).and(() -> { plr.changeGameMode(GameMode.ADVENTURE); }); diff --git a/src/main/resources/data/sabotage/games/sabotage.json b/src/main/resources/data/sabotage/games/sabotage.json index 3803066..12139fe 100644 --- a/src/main/resources/data/sabotage/games/sabotage.json +++ b/src/main/resources/data/sabotage/games/sabotage.json @@ -1,6 +1,9 @@ { "type": "sabotage:sabotage", "map": "sabotage:lobby", - "players_required": 2, - "max_players": 10 + "players": { + "min": 1, + "max": 64, + "threshold": 2 + } } \ No newline at end of file