diff options
| author | anhgelus <anhgelus.morhtuuzh@proton.me> | 2024-08-24 13:05:11 +0000 |
|---|---|---|
| committer | anhgelus <anhgelus.morhtuuzh@proton.me> | 2024-08-24 13:05:11 +0000 |
| commit | dfbc5bbfd20a22edb2596b4d73248df57a4cce20 (patch) | |
| tree | 6ec4a92c6a2daac254454369650e8a0fe18f0b7c /src | |
| parent | 34dc864a495699f1311651c92111884653b42806 (diff) | |
feat(network): send game state to client and disable mixins when game is not launched
Diffstat (limited to 'src')
| -rw-r--r-- | src/client/java/world/anhgelus/molehunt/client/MolehuntClient.java | 12 | ||||
| -rw-r--r-- | src/client/java/world/anhgelus/molehunt/client/mixin/NoNametags.java | 2 | ||||
| -rw-r--r-- | src/client/java/world/anhgelus/molehunt/client/mixin/NoPlayerListHud.java | 2 | ||||
| -rw-r--r-- | src/client/java/world/anhgelus/molehunt/client/mixin/NoSkin.java | 2 | ||||
| -rw-r--r-- | src/main/java/world/anhgelus/molehunt/Molehunt.java | 17 | ||||
| -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.java | 23 |
7 files changed, 62 insertions, 10 deletions
diff --git a/src/client/java/world/anhgelus/molehunt/client/MolehuntClient.java b/src/client/java/world/anhgelus/molehunt/client/MolehuntClient.java index 1f92573..c8a4d95 100644 --- a/src/client/java/world/anhgelus/molehunt/client/MolehuntClient.java +++ b/src/client/java/world/anhgelus/molehunt/client/MolehuntClient.java @@ -3,6 +3,7 @@ package world.anhgelus.molehunt.client; import net.fabricmc.api.ClientModInitializer; import net.fabricmc.fabric.api.client.networking.v1.ClientPlayNetworking; import world.anhgelus.molehunt.config.ConfigPayload; +import world.anhgelus.molehunt.game.GamePayload; public class MolehuntClient implements ClientModInitializer { @@ -10,6 +11,8 @@ public class MolehuntClient implements ClientModInitializer { private static boolean SHOW_NAMETAGS = false; private static boolean SHOW_TAB = false; + private static boolean GAME_STARTED = false; + @Override public void onInitializeClient() { ClientPlayNetworking.registerGlobalReceiver(ConfigPayload.ID, (payload, context) -> { @@ -19,6 +22,11 @@ public class MolehuntClient implements ClientModInitializer { SHOW_TAB = payload.showTab(); }); }); + ClientPlayNetworking.registerGlobalReceiver(GamePayload.ID, (payload, context) -> { + context.client().execute(() -> { + GAME_STARTED = payload.gameLaunched(); + }); + }); } public static boolean showSkins() { @@ -32,4 +40,8 @@ public class MolehuntClient implements ClientModInitializer { public static boolean showTab() { return SHOW_TAB; } + + public static boolean gameStarted() { + return GAME_STARTED; + } } diff --git a/src/client/java/world/anhgelus/molehunt/client/mixin/NoNametags.java b/src/client/java/world/anhgelus/molehunt/client/mixin/NoNametags.java index 3482abb..7f334a0 100644 --- a/src/client/java/world/anhgelus/molehunt/client/mixin/NoNametags.java +++ b/src/client/java/world/anhgelus/molehunt/client/mixin/NoNametags.java @@ -15,7 +15,7 @@ import world.anhgelus.molehunt.client.MolehuntClient; public class NoNametags<T extends Entity> {
@Inject(at = @At("HEAD"), method = "render", cancellable = true)
private void renderLabelOrNot(T entity, float yaw, float tickDelta, MatrixStack matrices, VertexConsumerProvider vertexConsumers, int light, CallbackInfo ci) {
- if (!(entity instanceof PlayerEntity) || MolehuntClient.showNameTags()) return;
+ if (!(entity instanceof PlayerEntity) || MolehuntClient.showNameTags() || !MolehuntClient.gameStarted()) return;
ci.cancel();
}
}
\ No newline at end of file diff --git a/src/client/java/world/anhgelus/molehunt/client/mixin/NoPlayerListHud.java b/src/client/java/world/anhgelus/molehunt/client/mixin/NoPlayerListHud.java index 00308ea..4473b12 100644 --- a/src/client/java/world/anhgelus/molehunt/client/mixin/NoPlayerListHud.java +++ b/src/client/java/world/anhgelus/molehunt/client/mixin/NoPlayerListHud.java @@ -11,7 +11,7 @@ import world.anhgelus.molehunt.client.MolehuntClient; public class NoPlayerListHud {
@Inject(at = @At("HEAD"), method = "render", cancellable = true)
public void render(CallbackInfo ci) {
- if (MolehuntClient.showTab()) return;
+ if (MolehuntClient.showTab() || !MolehuntClient.gameStarted()) return;
ci.cancel();
}
}
diff --git a/src/client/java/world/anhgelus/molehunt/client/mixin/NoSkin.java b/src/client/java/world/anhgelus/molehunt/client/mixin/NoSkin.java index 775d789..6638c1b 100644 --- a/src/client/java/world/anhgelus/molehunt/client/mixin/NoSkin.java +++ b/src/client/java/world/anhgelus/molehunt/client/mixin/NoSkin.java @@ -14,7 +14,7 @@ import world.anhgelus.molehunt.client.MolehuntClient; public class NoSkin {
@Inject(at = @At("HEAD"), method = "getSkinTextures", cancellable = true)
public void getSkin(CallbackInfoReturnable<SkinTextures> cir) {
- if (MolehuntClient.showSkins()) return;
+ if (MolehuntClient.showSkins() || !MolehuntClient.gameStarted()) return;
cir.setReturnValue(new SkinTextures(
Identifier.of(Molehunt.MOD_ID, "textures/skin.png"),
null,
diff --git a/src/main/java/world/anhgelus/molehunt/Molehunt.java b/src/main/java/world/anhgelus/molehunt/Molehunt.java index d7c0d58..afd6960 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; @@ -150,11 +152,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 dbe7ebd..a98516c 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.*;
@@ -89,7 +91,7 @@ public class Game { // reset time and weather
server.getOverworld().setTimeOfDay(0);
server.getOverworld().resetWeather();
- started = true;
+ changeState(true);
timer.scheduleAtFixedRate(new TimerTask() {
@Override
@@ -116,7 +118,7 @@ public class Game { public void end() {
timer.cancel();
timer = new Timer();
- 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 -> {
@@ -175,4 +177,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;
+ }
+}
|
