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:
parent
cabc9766aa
commit
cd6106a19c
3 changed files with 62 additions and 6 deletions
|
@ -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()) {
|
||||||
|
PacketDistributor.sendToPlayer((ServerPlayer)p.entity, pingPacket);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
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>();
|
||||||
|
|
|
@ -88,6 +88,8 @@ public class TurnBasedMinecraftMod {
|
||||||
|
|
||||||
registrar.playToClient(PacketClientGUI.TYPE, PacketClientGUI.STREAM_CODEC, new PacketClientGUI.PayloadHandler());
|
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");
|
logger.debug("Register packets com_burnedkirby_turnbasedminecraft");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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<PacketBattlePing> TYPE = new CustomPacketPayload.Type<>(ResourceLocation.fromNamespaceAndPath(TurnBasedMinecraftMod.MODID, "network_packetbattleping"));
|
||||||
|
|
||||||
|
public static final StreamCodec<ByteBuf, PacketBattlePing> STREAM_CODEC = StreamCodec.composite(
|
||||||
|
ByteBufCodecs.VAR_INT,
|
||||||
|
PacketBattlePing::battleID,
|
||||||
|
PacketBattlePing::new
|
||||||
|
);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Type<? extends CustomPacketPayload> type() {
|
||||||
|
return TYPE;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class PayloadHandler implements IPayloadHandler<PacketBattlePing> {
|
||||||
|
@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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue