diff --git a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/Battle.java index 607436e..1a031bd 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()) { + TurnBasedMinecraftMod.getHandler().send(pingPacket, PacketDistributor.PLAYER.with((ServerPlayer)p.entity)); + } + } + 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 6310282..71e0539 100644 --- a/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/TurnBasedMinecraftMod.java @@ -39,7 +39,7 @@ import org.apache.logging.log4j.Logger; public class TurnBasedMinecraftMod { public static final String MODID = "com_burnedkirby_turnbasedminecraft"; public static final String NAME = "Turn Based Minecraft Mod"; - public static final String VERSION = "1.26.0"; + public static final String VERSION = "1.26.1"; public static final String CONFIG_FILENAME = "TBM_Config.toml"; public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; @@ -122,6 +122,11 @@ public class TurnBasedMinecraftMod { .decoder(new PacketClientGui.Decoder()) .consumerNetworkThread(new PacketClientGui.Consumer()) .add(); + HANDLER.messageBuilder(PacketBattlePing.class, NetworkDirection.PLAY_TO_CLIENT) + .encoder(new PacketBattlePing.Encoder()) + .decoder(new PacketBattlePing.Decoder()) + .consumerNetworkThread(new PacketBattlePing.Consumer()) + .add(); // register event handler(s) MinecraftForge.EVENT_BUS.register(new AttackEventHandler()); 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..942c7ee --- /dev/null +++ b/src/main/java/com/burnedkirby/TurnBasedMinecraft/common/networking/PacketBattlePing.java @@ -0,0 +1,53 @@ +package com.burnedkirby.TurnBasedMinecraft.common.networking; + +import com.burnedkirby.TurnBasedMinecraft.common.TurnBasedMinecraftMod; +import net.minecraft.network.RegistryFriendlyByteBuf; +import net.minecraftforge.event.network.CustomPayloadEvent; + +import java.util.function.BiConsumer; +import java.util.function.Function; + +public class PacketBattlePing { + private int battleID; + + public PacketBattlePing() { + battleID = 0; + } + + public PacketBattlePing(int battleID) { + this.battleID = battleID; + } + + public static class Encoder implements BiConsumer { + public Encoder() {} + + @Override + public void accept(PacketBattlePing pkt, RegistryFriendlyByteBuf buf) { + buf.writeInt(pkt.battleID); + } + } + + public static class Decoder implements Function { + public Decoder() {} + + @Override + public PacketBattlePing apply(RegistryFriendlyByteBuf buf) { + return new PacketBattlePing(buf.readInt()); + } + } + + public static class Consumer implements BiConsumer { + public Consumer() {} + + @Override + public void accept(PacketBattlePing pkt, CustomPayloadEvent.Context ctx) { + ctx.enqueueWork(() -> { + if (TurnBasedMinecraftMod.proxy.getLocalBattle() == null) { + TurnBasedMinecraftMod.proxy.createLocalBattle(pkt.battleID); + } + TurnBasedMinecraftMod.proxy.setBattleGuiAsGui(); + }); + ctx.setPacketHandled(true); + } + } +} \ No newline at end of file