diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java index bc46e10..67104bb 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java @@ -2,6 +2,7 @@ package com.burnedkirby.TurnBasedMinecraft.common; import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattleInfo; import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattleMessage; +import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattlePing; import net.minecraft.resources.ResourceKey; import net.minecraft.server.level.ServerPlayer; import net.minecraft.world.InteractionHand; @@ -49,7 +50,7 @@ public class Battle { private ResourceKey dimension; - private long infoNanos; + private long pingTimerNanos; public enum State { DECISION(0), @@ -124,7 +125,7 @@ public class Battle { undecidedCount = new AtomicInteger(0); random = new Random(); this.dimension = dimension; - infoNanos = 0; + pingTimerNanos = 0; if (sideA != null) { for (Entity e : sideA) { EntityInfo entityInfo; @@ -456,6 +457,16 @@ public class Battle { } } + protected void notifyPlayersBattlePing() { + if (!isServer) { + return; + } + PacketBattlePing pingPacket = new PacketBattlePing(getId()); + for (Combatant p : players.values()) { + PacketDistributor.sendToPlayer((ServerPlayer)p.entity, pingPacket); + } + } + protected void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount) { sendMessageToAllPlayers(type, from, to, amount, new String()); } @@ -654,10 +665,10 @@ public class Battle { } private boolean update(final long dt) { - infoNanos += dt; - if (infoNanos >= 4000000000L) { - infoNanos = 0; - notifyPlayersBattleInfo(); + pingTimerNanos += dt; + if (pingTimerNanos >= 4000000000L) { + pingTimerNanos = 0; + notifyPlayersBattlePing(); } if (battleEnded) { Collection combatants = new ArrayList(); diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java index 9b8a142..3528add 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java @@ -88,6 +88,8 @@ public class TurnBasedMinecraftMod { registrar.playToClient(PacketClientGUI.TYPE, PacketClientGUI.STREAM_CODEC, new PacketClientGUI.PayloadHandler()); + registrar.playToClient(PacketBattlePing.TYPE, PacketBattlePing.STREAM_CODEC, new PacketBattlePing.PayloadHandler()); + logger.debug("Register packets com_burnedkirby_turnbasedminecraft"); } diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/networking/PacketBattlePing.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/networking/PacketBattlePing.java new file mode 100644 index 0000000..08ed1ff --- /dev/null +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/networking/PacketBattlePing.java @@ -0,0 +1,43 @@ +package com.burnedkirby.TurnBasedMinecraft.common.networking; + +import com.burnedkirby.TurnBasedMinecraft.common.TurnBasedMinecraftMod; +import io.netty.buffer.ByteBuf; +import net.minecraft.network.chat.Component; +import net.minecraft.network.codec.ByteBufCodecs; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; +import net.minecraft.resources.ResourceLocation; +import net.neoforged.neoforge.network.handling.IPayloadContext; +import net.neoforged.neoforge.network.handling.IPayloadHandler; +import org.jetbrains.annotations.NotNull; + +public record PacketBattlePing(int battleID) implements CustomPacketPayload { + public static final CustomPacketPayload.Type TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(TurnBasedMinecraftMod.MODID, "network_packetbattleping")); + + public static final StreamCodec STREAM_CODEC = StreamCodec.composite( + ByteBufCodecs.VAR_INT, + PacketBattlePing::battleID, + PacketBattlePing::new + ); + + @Override + public Type type() { + return TYPE; + } + + public static class PayloadHandler implements IPayloadHandler { + @Override + public void handle(final @NotNull PacketBattlePing pkt, IPayloadContext ctx) { + ctx.enqueueWork(() -> { + if (TurnBasedMinecraftMod.proxy.getLocalBattle() == null) { + TurnBasedMinecraftMod.proxy.createLocalBattle(pkt.battleID); + } + TurnBasedMinecraftMod.proxy.setBattleGuiAsGui(); + TurnBasedMinecraftMod.proxy.setBattleGuiBattleChanged(); + }).exceptionally(e -> { + ctx.disconnect(Component.literal("Exception handling PacketBattlePing! " + e.getMessage())); + return null; + }); + } + } +}