aboutsummaryrefslogtreecommitdiff
path: root/src/main
diff options
context:
space:
mode:
authorLéo Kosman <leo.kosman@proton.me>2024-08-24 22:58:35 +0200
committerLéo Kosman <leo.kosman@proton.me>2024-08-24 22:58:35 +0200
commit275604ac67e4fa3b20035f3756add1701e879c13 (patch)
tree0aa50872cec5bd834061e7f48c17117ff4c53ed9 /src/main
parent06027e9cd2b71a9606fb38dbde261cc629a68cc1 (diff)
parentf9702feda784bfe7fef3cb8b8d0ddb420010ea5b (diff)
Merge branch 'refs/heads/main' into feat/world-borders
# Conflicts: # src/main/java/world/anhgelus/molehunt/game/Game.java
Diffstat (limited to 'src/main')
-rw-r--r--src/main/java/world/anhgelus/molehunt/Molehunt.java17
-rw-r--r--src/main/java/world/anhgelus/molehunt/game/Game.java (renamed from src/main/java/world/anhgelus/molehunt/Game.java)14
-rw-r--r--src/main/java/world/anhgelus/molehunt/game/GamePayload.java23
3 files changed, 47 insertions, 7 deletions
diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java
index 7df62fd..450666a 100644
--- a/src/main/java/world/anhgelus/molehunt/Molehunt.java
+++ b/src/main/java/world/anhgelus/molehunt/Molehunt.java
@@ -24,6 +24,8 @@ import org.slf4j.LoggerFactory;
import world.anhgelus.molehunt.config.Config;
import world.anhgelus.molehunt.config.ConfigPayload;
import world.anhgelus.molehunt.config.SimpleConfig;
+import world.anhgelus.molehunt.game.Game;
+import world.anhgelus.molehunt.game.GamePayload;
import java.util.HashMap;
@@ -165,11 +167,18 @@ public class Molehunt implements ModInitializer {
newPlayer.changeGameMode(GameMode.SPECTATOR);
});
- ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> ServerPlayNetworking.send(
- handler.player,
- new ConfigPayload(CONFIG.areNametagsEnabled(), CONFIG.areSkinsEnabled(), CONFIG.isTabEnabled())
- ));
+ ServerPlayConnectionEvents.JOIN.register((handler, sender, server) -> {
+ ServerPlayNetworking.send(
+ handler.player,
+ new ConfigPayload(CONFIG.areNametagsEnabled(), CONFIG.areSkinsEnabled(), CONFIG.isTabEnabled())
+ );
+ ServerPlayNetworking.send(
+ handler.player,
+ new GamePayload(game != null && game.hasStarted())
+ );
+ });
PayloadTypeRegistry.playS2C().register(ConfigPayload.ID, ConfigPayload.CODEC);
+ PayloadTypeRegistry.playS2C().register(GamePayload.ID, GamePayload.CODEC);
}
}
diff --git a/src/main/java/world/anhgelus/molehunt/Game.java b/src/main/java/world/anhgelus/molehunt/game/Game.java
index 300b4c2..8670f7e 100644
--- a/src/main/java/world/anhgelus/molehunt/Game.java
+++ b/src/main/java/world/anhgelus/molehunt/game/Game.java
@@ -1,5 +1,6 @@
-package world.anhgelus.molehunt;
+package world.anhgelus.molehunt.game;
+import net.fabricmc.fabric.api.networking.v1.ServerPlayNetworking;
import net.minecraft.network.packet.s2c.play.OverlayMessageS2CPacket;
import net.minecraft.network.packet.s2c.play.SubtitleS2CPacket;
import net.minecraft.network.packet.s2c.play.TitleFadeS2CPacket;
@@ -9,6 +10,7 @@ import net.minecraft.server.network.ServerPlayerEntity;
import net.minecraft.text.Text;
import net.minecraft.world.GameMode;
import net.minecraft.world.GameRules;
+import world.anhgelus.molehunt.Molehunt;
import world.anhgelus.molehunt.utils.TimeUtils;
import java.util.*;
@@ -104,7 +106,7 @@ public class Game {
// reset time and weather
server.getOverworld().setTimeOfDay(0);
server.getOverworld().resetWeather();
- started = true;
+ changeState(true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
@@ -136,7 +138,7 @@ public class Game {
// Stops the border shrinking.
worldBorder.setSize(worldBorder.getSize());
- started = false;
+ changeState(false);
final var pm = server.getPlayerManager();
final var winnerSuspense = new TitleS2CPacket(Text.translatable("molehunt.game.end.suspense.title"));
pm.getPlayerList().forEach(p -> {
@@ -195,4 +197,10 @@ public class Game {
public boolean hasStarted() {
return started;
}
+
+ private void changeState(boolean hasStarted) {
+ started = hasStarted;
+ final var payload = new GamePayload(hasStarted);
+ server.getPlayerManager().getPlayerList().forEach(p -> ServerPlayNetworking.send(p, payload));
+ }
}
diff --git a/src/main/java/world/anhgelus/molehunt/game/GamePayload.java b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
new file mode 100644
index 0000000..4f7b8ce
--- /dev/null
+++ b/src/main/java/world/anhgelus/molehunt/game/GamePayload.java
@@ -0,0 +1,23 @@
+package world.anhgelus.molehunt.game;
+
+import net.minecraft.network.RegistryByteBuf;
+import net.minecraft.network.codec.PacketCodec;
+import net.minecraft.network.codec.PacketCodecs;
+import net.minecraft.network.packet.CustomPayload;
+import net.minecraft.util.Identifier;
+import world.anhgelus.molehunt.Molehunt;
+
+public record GamePayload(boolean gameLaunched) implements CustomPayload {
+ public static final Identifier GAME_PACKET_ID = Identifier.of(Molehunt.MOD_ID, "game");
+
+ public static final CustomPayload.Id<GamePayload> ID = new CustomPayload.Id<>(GAME_PACKET_ID);
+ public static final PacketCodec<RegistryByteBuf, GamePayload> CODEC = PacketCodec.tuple(
+ PacketCodecs.BOOL, GamePayload::gameLaunched,
+ GamePayload::new
+ );
+
+ @Override
+ public Id<? extends CustomPayload> getId() {
+ return ID;
+ }
+}