Compare commits

..

2 commits

Author SHA1 Message Date
2b4909e1f4 Store remaining turn seconds in PacketBattlePing 2024-10-22 20:35:31 +09:00
d08eaab720 Use "PacketBattlePing" for "pings"
More efficient than using "PacketBattleInfo" every 4 seconds which is
heftier than a single battleID.

Currently "PacketBattlePing" is used to cause the client to show the
BattleGUI if it is not open.
2024-10-22 20:28:06 +09:00
3 changed files with 82 additions and 7 deletions

View file

@ -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<Level> 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(), (int)(timer / 1000000000L));
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<Combatant> combatants = new ArrayList<Combatant>();

View file

@ -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());

View file

@ -0,0 +1,59 @@
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;
private int decisionSeconds;
public PacketBattlePing() {
battleID = 0;
decisionSeconds = 1;
}
public PacketBattlePing(int battleID, int decisionSeconds) {
this.battleID = battleID;
this.decisionSeconds = decisionSeconds;
}
public static class Encoder implements BiConsumer<PacketBattlePing, RegistryFriendlyByteBuf> {
public Encoder() {}
@Override
public void accept(PacketBattlePing pkt, RegistryFriendlyByteBuf buf) {
buf.writeInt(pkt.battleID);
buf.writeInt(pkt.decisionSeconds);
}
}
public static class Decoder implements Function<RegistryFriendlyByteBuf, PacketBattlePing> {
public Decoder() {}
@Override
public PacketBattlePing apply(RegistryFriendlyByteBuf buf) {
return new PacketBattlePing(buf.readInt(), buf.readInt());
}
}
public static class Consumer implements BiConsumer<PacketBattlePing, CustomPayloadEvent.Context> {
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();
TurnBasedMinecraftMod.proxy.setBattleGuiBattleChanged();
TurnBasedMinecraftMod.proxy.setBattleGuiTime(pkt.decisionSeconds);
});
ctx.setPacketHandled(true);
}
}
}