Skip to content

Commit

Permalink
Setup streamcodecs for the data classes, clean up payloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrbysco committed Nov 28, 2024
1 parent a60e9df commit 58d7771
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,10 @@ public ArmorStand getArmorStand() {
return armorStand;
}

public boolean isLocked() {
return locked;
}

public void renderPose(GuiGraphics guiGraphics, int xPos, int yPos, float size) {
if (armorStand != null) {
InventoryScreen.renderEntityInInventory(guiGraphics, xPos, yPos, size,
Expand Down
17 changes: 9 additions & 8 deletions common/src/main/java/com/mrbysco/armorposer/data/RenameData.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
package com.mrbysco.armorposer.data;

import net.minecraft.core.UUIDUtil;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.chat.Component;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.entity.decoration.ArmorStand;
import net.minecraft.world.entity.player.Player;

import java.util.UUID;

public record RenameData(UUID entityUUID, String name) {
public void encode(FriendlyByteBuf buf) {
buf.writeUUID(entityUUID);
buf.writeUtf(name);
}

public static RenameData decode(final FriendlyByteBuf packetBuffer) {
return new RenameData(packetBuffer.readUUID(), packetBuffer.readUtf());
}
public static final StreamCodec<FriendlyByteBuf, RenameData> STREAM_CODEC = StreamCodec.composite(
UUIDUtil.STREAM_CODEC,
RenameData::entityUUID,
ByteBufCodecs.STRING_UTF8,
RenameData::name,
RenameData::new);

public void handleData(ArmorStand armorStand, Player player) {
if (!name.isEmpty() && (player.experienceLevel >= 1 || player.getAbilities().instabuild)) {
Expand Down
15 changes: 10 additions & 5 deletions common/src/main/java/com/mrbysco/armorposer/data/SwapData.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.mrbysco.armorposer.data;

import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.entity.EquipmentSlot;
import net.minecraft.world.entity.decoration.ArmorStand;
Expand All @@ -9,13 +10,17 @@
import java.util.UUID;

public record SwapData(UUID entityUUID, Action action) {
public void write(FriendlyByteBuf buf) {
buf.writeUUID(entityUUID);
buf.writeEnum(action);
public static final StreamCodec<FriendlyByteBuf, SwapData> STREAM_CODEC = StreamCodec.of(
SwapData::write,
SwapData::new);

public SwapData(final FriendlyByteBuf packetBuffer) {
this(packetBuffer.readUUID(), packetBuffer.readEnum(Action.class));
}

public static SwapData read(final FriendlyByteBuf packetBuffer) {
return new SwapData(packetBuffer.readUUID(), packetBuffer.readEnum(Action.class));
private static void write(FriendlyByteBuf friendlyByteBuf, SwapData swapData) {
friendlyByteBuf.writeUUID(swapData.entityUUID);
friendlyByteBuf.writeEnum(swapData.action);
}

public void handleData(ArmorStand armorStand) {
Expand Down
17 changes: 9 additions & 8 deletions common/src/main/java/com/mrbysco/armorposer/data/SyncData.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.mrbysco.armorposer.data;

import com.mrbysco.armorposer.Reference;
import net.minecraft.core.UUIDUtil;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.world.entity.ai.attributes.AttributeInstance;
import net.minecraft.world.entity.ai.attributes.Attributes;
import net.minecraft.world.entity.decoration.ArmorStand;
Expand All @@ -13,14 +16,12 @@
import java.util.UUID;

public record SyncData(UUID entityUUID, CompoundTag tag) {
public void encode(FriendlyByteBuf buf) {
buf.writeUUID(entityUUID);
buf.writeNbt(tag);
}

public static SyncData decode(final FriendlyByteBuf packetBuffer) {
return new SyncData(packetBuffer.readUUID(), packetBuffer.readNbt());
}
public static final StreamCodec<FriendlyByteBuf, SyncData> STREAM_CODEC = StreamCodec.composite(
UUIDUtil.STREAM_CODEC,
SyncData::entityUUID,
ByteBufCodecs.COMPOUND_TAG,
SyncData::tag,
SyncData::new);

public void handleData(ArmorStand armorStand, Player player) {
CompoundTag entityTag = armorStand.saveWithoutId(new CompoundTag());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.mrbysco.armorposer.Reference;
import com.mrbysco.armorposer.data.RenameData;
import com.mrbysco.armorposer.data.SyncData;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;
Expand All @@ -15,11 +14,11 @@ public record ArmorStandRenamePayload(RenameData data) implements CustomPacketPa
public static final Type<ArmorStandRenamePayload> ID = new Type<>(Reference.RENAME_PACKET_ID);

public ArmorStandRenamePayload(final FriendlyByteBuf packetBuffer) {
this(RenameData.decode(packetBuffer));
this(RenameData.STREAM_CODEC.decode(packetBuffer));
}

public void write(FriendlyByteBuf buf) {
data.encode(buf);
RenameData.STREAM_CODEC.encode(buf, data());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,17 @@

import com.mrbysco.armorposer.Reference;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.ByteBufCodecs;
import net.minecraft.network.codec.StreamCodec;
import net.minecraft.network.protocol.common.custom.CustomPacketPayload;

public record ArmorStandScreenPayload(int entityID) implements CustomPacketPayload {
public static final StreamCodec<FriendlyByteBuf, ArmorStandScreenPayload> CODEC = CustomPacketPayload.codec(
ArmorStandScreenPayload::write,
public static final StreamCodec<FriendlyByteBuf, ArmorStandScreenPayload> CODEC = StreamCodec.composite(
ByteBufCodecs.INT,
ArmorStandScreenPayload::entityID,
ArmorStandScreenPayload::new);
public static final Type<ArmorStandScreenPayload> ID = new Type<>(Reference.SCREEN_PACKET_ID);

public ArmorStandScreenPayload(final FriendlyByteBuf packetBuffer) {
this(packetBuffer.readInt());
}

public void write(FriendlyByteBuf buf) {
buf.writeInt(entityID);
}

@Override
public Type<? extends CustomPacketPayload> type() {
return ID;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public record ArmorStandSwapPayload(SwapData data) implements CustomPacketPayloa
public static final Type<ArmorStandSwapPayload> ID = new Type<>(Reference.SWAP_PACKET_ID);

public ArmorStandSwapPayload(final FriendlyByteBuf packetBuffer) {
this(SwapData.read(packetBuffer));
this(SwapData.STREAM_CODEC.decode(packetBuffer));
}

public void write(FriendlyByteBuf buf) {
data.write(buf);
SwapData.STREAM_CODEC.encode(buf, data());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ public record ArmorStandSyncPayload(SyncData data) implements CustomPacketPayloa
public static final Type<ArmorStandSyncPayload> ID = new Type<>(Reference.SYNC_PACKET_ID);

public ArmorStandSyncPayload(final FriendlyByteBuf packetBuffer) {
this(SyncData.decode(packetBuffer));
this(SyncData.STREAM_CODEC.decode(packetBuffer));
}

public void write(FriendlyByteBuf buf) {
data.encode(buf);
SyncData.STREAM_CODEC.encode(buf, data());
}

@Override
Expand Down

0 comments on commit 58d7771

Please sign in to comment.