Skip to content

Commit

Permalink
i'm dumb, proper waiting lobby
Browse files Browse the repository at this point in the history
  • Loading branch information
ellieisjelly committed Jan 3, 2024
1 parent d032e33 commit 5c6590d
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 43 deletions.
20 changes: 7 additions & 13 deletions src/main/java/me/ellieis/Sabotage/game/SabotageConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<SabotageConfig> 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() {
Expand Down
32 changes: 4 additions & 28 deletions src/main/java/me/ellieis/Sabotage/game/phase/SabotageWaiting.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -61,18 +62,12 @@ public static GameOpenProcedure Open(GameOpenContext<SabotageConfig> 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);
});
}

Expand All @@ -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);
});
Expand Down
7 changes: 5 additions & 2 deletions src/main/resources/data/sabotage/games/sabotage.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"type": "sabotage:sabotage",
"map": "sabotage:lobby",
"players_required": 2,
"max_players": 10
"players": {
"min": 1,
"max": 64,
"threshold": 2
}
}

0 comments on commit 5c6590d

Please sign in to comment.