From 0604fb0c2e203dcb1c1ced31d28bd9968eb3802a Mon Sep 17 00:00:00 2001 From: Up Date: Sat, 21 Sep 2024 04:57:48 +0200 Subject: [PATCH] update packet registration --- .../java/dev/cammiescorner/icarus/Icarus.java | 2 +- .../network/s2c/SyncConfigValuesPacket.java | 77 ++++++++++--------- 2 files changed, 42 insertions(+), 37 deletions(-) diff --git a/Common/src/main/java/dev/cammiescorner/icarus/Icarus.java b/Common/src/main/java/dev/cammiescorner/icarus/Icarus.java index 84de918..aab65d2 100644 --- a/Common/src/main/java/dev/cammiescorner/icarus/Icarus.java +++ b/Common/src/main/java/dev/cammiescorner/icarus/Icarus.java @@ -16,7 +16,7 @@ public class Icarus { public static void init() { CONFIGURATOR.register(IcarusConfig.class); - Network.registerPacket(SyncConfigValuesPacket.ID, SyncConfigValuesPacket.class, SyncConfigValuesPacket::encode, SyncConfigValuesPacket::decode, SyncConfigValuesPacket::handle); + Network.registerPacket(SyncConfigValuesPacket.TYPE, SyncConfigValuesPacket.class, SyncConfigValuesPacket.STREAM_CODEC, SyncConfigValuesPacket::handle); } public static ResourceLocation id(String path) { diff --git a/Common/src/main/java/dev/cammiescorner/icarus/network/s2c/SyncConfigValuesPacket.java b/Common/src/main/java/dev/cammiescorner/icarus/network/s2c/SyncConfigValuesPacket.java index 18add3c..030966c 100644 --- a/Common/src/main/java/dev/cammiescorner/icarus/network/s2c/SyncConfigValuesPacket.java +++ b/Common/src/main/java/dev/cammiescorner/icarus/network/s2c/SyncConfigValuesPacket.java @@ -8,43 +8,48 @@ import dev.cammiescorner.icarus.util.ServerPlayerFallbackValues; import net.minecraft.client.Minecraft; import net.minecraft.network.FriendlyByteBuf; +import net.minecraft.network.codec.StreamCodec; +import net.minecraft.network.protocol.common.custom.CustomPacketPayload; import net.minecraft.resources.ResourceLocation; import net.minecraft.server.level.ServerPlayer; -public record SyncConfigValuesPacket(float wingsSpeed, float maxSlowedMultiplier, boolean armorSlows, boolean canLoopDeLoop, float requiredFoodAmount) { - public static final ResourceLocation ID = Icarus.id("sync_config_values"); - - public void encode(FriendlyByteBuf buf) { - buf.writeFloat(wingsSpeed()); - buf.writeFloat(maxSlowedMultiplier()); - buf.writeBoolean(armorSlows()); - buf.writeBoolean(canLoopDeLoop()); - buf.writeFloat(requiredFoodAmount()); - } - - public static void send(ServerPlayer player) { - var cfg = new ServerPlayerFallbackValues(); - var packet = new SyncConfigValuesPacket(cfg.wingsSpeed(), cfg.maxSlowedMultiplier(), cfg.armorSlows(), cfg.canLoopDeLoop(), cfg.requiredFoodAmount()); - Dispatcher.sendToClient(packet, player); - } - - public static SyncConfigValuesPacket decode(FriendlyByteBuf buf) { - float wingsSpeed = buf.readFloat(); - float maxSlowedMultiplier = buf.readFloat(); - boolean armorSlows = buf.readBoolean(); - boolean canLoopDeLoop = buf.readBoolean(); - float requiredFoodAmount = buf.readFloat(); - - return new SyncConfigValuesPacket(wingsSpeed, maxSlowedMultiplier, armorSlows, canLoopDeLoop, requiredFoodAmount); - } - - public static void handle(PacketContext ctx) { - Minecraft.getInstance().execute(() -> IcarusHelper.fallbackValues = new ClientPlayerFallbackValues( - ctx.message().wingsSpeed(), - ctx.message().maxSlowedMultiplier(), - ctx.message().armorSlows(), - ctx.message().canLoopDeLoop(), - ctx.message().requiredFoodAmount() - )); - } +public record SyncConfigValuesPacket(float wingsSpeed, float maxSlowedMultiplier, boolean armorSlows, + boolean canLoopDeLoop, float requiredFoodAmount) { + public static final ResourceLocation ID = Icarus.id("sync_config_values"); + public static final CustomPacketPayload.Type TYPE = new CustomPacketPayload.Type<>(ID); + public static final StreamCodec STREAM_CODEC = StreamCodec.ofMember(SyncConfigValuesPacket::encode, SyncConfigValuesPacket::decode); + + private void encode(FriendlyByteBuf buf) { + buf.writeFloat(wingsSpeed()); + buf.writeFloat(maxSlowedMultiplier()); + buf.writeBoolean(armorSlows()); + buf.writeBoolean(canLoopDeLoop()); + buf.writeFloat(requiredFoodAmount()); + } + + public static void send(ServerPlayer player) { + var cfg = new ServerPlayerFallbackValues(); + var packet = new SyncConfigValuesPacket(cfg.wingsSpeed(), cfg.maxSlowedMultiplier(), cfg.armorSlows(), cfg.canLoopDeLoop(), cfg.requiredFoodAmount()); + Dispatcher.sendToClient(packet, player); + } + + private static SyncConfigValuesPacket decode(FriendlyByteBuf buf) { + float wingsSpeed = buf.readFloat(); + float maxSlowedMultiplier = buf.readFloat(); + boolean armorSlows = buf.readBoolean(); + boolean canLoopDeLoop = buf.readBoolean(); + float requiredFoodAmount = buf.readFloat(); + + return new SyncConfigValuesPacket(wingsSpeed, maxSlowedMultiplier, armorSlows, canLoopDeLoop, requiredFoodAmount); + } + + public static void handle(PacketContext ctx) { + ClientHandler.handle(ctx); + } + + private static class ClientHandler { + private static void handle(PacketContext ctx) { + Minecraft.getInstance().execute(() -> IcarusHelper.fallbackValues = new ClientPlayerFallbackValues(ctx.message().wingsSpeed(), ctx.message().maxSlowedMultiplier(), ctx.message().armorSlows(), ctx.message().canLoopDeLoop(), ctx.message().requiredFoodAmount())); + } + } }