Skip to content

Commit

Permalink
update packet registration
Browse files Browse the repository at this point in the history
  • Loading branch information
UpcraftLP committed Sep 21, 2024
1 parent f0ab9d0 commit 0604fb0
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Common/src/main/java/dev/cammiescorner/icarus/Icarus.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<SyncConfigValuesPacket> 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<CustomPacketPayload> TYPE = new CustomPacketPayload.Type<>(ID);
public static final StreamCodec<FriendlyByteBuf, SyncConfigValuesPacket> 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<SyncConfigValuesPacket> ctx) {
ClientHandler.handle(ctx);
}

private static class ClientHandler {
private static void handle(PacketContext<SyncConfigValuesPacket> ctx) {
Minecraft.getInstance().execute(() -> IcarusHelper.fallbackValues = new ClientPlayerFallbackValues(ctx.message().wingsSpeed(), ctx.message().maxSlowedMultiplier(), ctx.message().armorSlows(), ctx.message().canLoopDeLoop(), ctx.message().requiredFoodAmount()));
}
}
}

0 comments on commit 0604fb0

Please sign in to comment.