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.
This commit is contained in:
Stephen Seo 2024-10-22 20:10:50 +09:00
parent 02f7b179a5
commit 8e469f1a8a
3 changed files with 76 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.PacketBattleInfo;
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattleMessage; import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattleMessage;
import com.burnedkirby.TurnBasedMinecraft.common.networking.PacketBattlePing;
import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceKey;
import net.minecraft.server.level.ServerPlayer; import net.minecraft.server.level.ServerPlayer;
import net.minecraft.world.InteractionHand; import net.minecraft.world.InteractionHand;
@ -49,7 +50,7 @@ public class Battle {
private ResourceKey<Level> dimension; private ResourceKey<Level> dimension;
private long infoNanos; private long pingTimerNanos;
public enum State { public enum State {
DECISION(0), DECISION(0),
@ -124,7 +125,7 @@ public class Battle {
undecidedCount = new AtomicInteger(0); undecidedCount = new AtomicInteger(0);
random = new Random(); random = new Random();
this.dimension = dimension; this.dimension = dimension;
infoNanos = 0; pingTimerNanos = 0;
if (sideA != null) { if (sideA != null) {
for (Entity e : sideA) { for (Entity e : sideA) {
EntityInfo entityInfo; 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) { protected void sendMessageToAllPlayers(PacketBattleMessage.MessageType type, int from, int to, int amount) {
sendMessageToAllPlayers(type, from, to, amount, new String()); sendMessageToAllPlayers(type, from, to, amount, new String());
} }
@ -654,10 +665,10 @@ public class Battle {
} }
private boolean update(final long dt) { private boolean update(final long dt) {
infoNanos += dt; pingTimerNanos += dt;
if (infoNanos >= 4000000000L) { if (pingTimerNanos >= 4000000000L) {
infoNanos = 0; pingTimerNanos = 0;
notifyPlayersBattleInfo(); notifyPlayersBattlePing();
} }
if (battleEnded) { if (battleEnded) {
Collection<Combatant> combatants = new ArrayList<Combatant>(); Collection<Combatant> combatants = new ArrayList<Combatant>();

View file

@ -39,7 +39,7 @@ import org.apache.logging.log4j.Logger;
public class TurnBasedMinecraftMod { public class TurnBasedMinecraftMod {
public static final String MODID = "com_burnedkirby_turnbasedminecraft"; public static final String MODID = "com_burnedkirby_turnbasedminecraft";
public static final String NAME = "Turn Based Minecraft Mod"; 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 CONFIG_FILENAME = "TBM_Config.toml";
public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml"; public static final String DEFAULT_CONFIG_FILENAME = "TBM_Config_DEFAULT.toml";
public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/"; public static final String CONFIG_DIRECTORY = "config/TurnBasedMinecraft/";
@ -122,6 +122,11 @@ public class TurnBasedMinecraftMod {
.decoder(new PacketClientGui.Decoder()) .decoder(new PacketClientGui.Decoder())
.consumerNetworkThread(new PacketClientGui.Consumer()) .consumerNetworkThread(new PacketClientGui.Consumer())
.add(); .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) // register event handler(s)
MinecraftForge.EVENT_BUS.register(new AttackEventHandler()); MinecraftForge.EVENT_BUS.register(new AttackEventHandler());

View file

@ -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<PacketBattlePing, RegistryFriendlyByteBuf> {
public Encoder() {}
@Override
public void accept(PacketBattlePing pkt, RegistryFriendlyByteBuf buf) {
buf.writeInt(pkt.battleID);
}
}
public static class Decoder implements Function<RegistryFriendlyByteBuf, PacketBattlePing> {
public Decoder() {}
@Override
public PacketBattlePing apply(RegistryFriendlyByteBuf buf) {
return new PacketBattlePing(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();
});
ctx.setPacketHandled(true);
}
}
}