From 6a20d1e54140f5e103945da537ddcc50499d3f33 Mon Sep 17 00:00:00 2001
From: parzivail <colby.newman2000@gmail.com>
Date: Sat, 26 Oct 2024 19:09:59 -0400
Subject: [PATCH] pass bolt nbt data along in spawn packet

---
 .../dev/pswg/entity/BlasterBoltEntity.java    | 19 +++++--
 .../GalaxiesEntitySpawnS2CPacket.java         | 50 ++++++++++++++++++-
 .../pswg/networking/GalaxiesNetworking.java   |  2 +-
 3 files changed, 64 insertions(+), 7 deletions(-)

diff --git a/projects/pswg_blasters/src/main/java/dev/pswg/entity/BlasterBoltEntity.java b/projects/pswg_blasters/src/main/java/dev/pswg/entity/BlasterBoltEntity.java
index efb346da3..f3c41d6d1 100644
--- a/projects/pswg_blasters/src/main/java/dev/pswg/entity/BlasterBoltEntity.java
+++ b/projects/pswg_blasters/src/main/java/dev/pswg/entity/BlasterBoltEntity.java
@@ -38,7 +38,7 @@ public void tick()
 	{
 		super.tick();
 
-		this.move(MovementType.SELF, this.getVelocity());
+		move(MovementType.SELF, getVelocity());
 
 		if (this.getWorld() instanceof ServerWorld serverWorld && this.age > 20)
 			kill(serverWorld);
@@ -51,16 +51,27 @@ public void onSpawnPacket(EntitySpawnS2CPacket packet)
 
 		float yaw = packet.getYaw();
 		float pitch = packet.getPitch();
-		this.setAngles(yaw, pitch);
+		setAngles(yaw, pitch);
 
 		if (packet instanceof GalaxiesEntitySpawnS2CPacket precisePacket)
-			this.setVelocity(precisePacket.getVelocity());
+		{
+			setVelocity(precisePacket.getVelocity());
+			readCustomDataFromNbt(precisePacket.getCustomData(this, NbtCompound.CODEC).getOrThrow());
+		}
 	}
 
 	@Override
 	public Packet<ClientPlayPacketListener> createSpawnPacket(EntityTrackerEntry entityTrackerEntry)
 	{
-		return GalaxiesNetworking.createPlayS2CPacket(new GalaxiesEntitySpawnS2CPacket(this, entityTrackerEntry));
+		var nbt = new NbtCompound();
+		writeCustomDataToNbt(nbt);
+
+		return GalaxiesNetworking.createPlayS2CPacket(new GalaxiesEntitySpawnS2CPacket(
+				this,
+				entityTrackerEntry,
+				NbtCompound.CODEC,
+				nbt
+		));
 	}
 
 	@Override
diff --git a/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesEntitySpawnS2CPacket.java b/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesEntitySpawnS2CPacket.java
index 7f2295315..34e7a2942 100644
--- a/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesEntitySpawnS2CPacket.java
+++ b/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesEntitySpawnS2CPacket.java
@@ -1,7 +1,11 @@
 package dev.pswg.networking;
 
+import com.mojang.serialization.Codec;
+import com.mojang.serialization.DataResult;
 import dev.pswg.Galaxies;
 import net.minecraft.entity.Entity;
+import net.minecraft.nbt.NbtCompound;
+import net.minecraft.nbt.NbtOps;
 import net.minecraft.network.RegistryByteBuf;
 import net.minecraft.network.codec.PacketCodec;
 import net.minecraft.network.packet.CustomPayload;
@@ -10,24 +14,47 @@
 import net.minecraft.util.math.Vec3d;
 
 /**
- * A spawn packet that serializes full-precision values for entity velocity and angles
+ * A spawn packet that serializes full-precision values for entity velocity and angles, and
+ * allows for custom payload values
  */
 public class GalaxiesEntitySpawnS2CPacket extends EntitySpawnS2CPacket implements CustomPayload
 {
+	/**
+	 * The name of the NBT element that contains the custom spawn data
+	 */
+	private static final String CUSTOM_DATA_FIELD = "custom_data";
+
 	public static final CustomPayload.Id<GalaxiesEntitySpawnS2CPacket> ID = new Id<>(Galaxies.id("entity_spawn"));
 	public static final PacketCodec<RegistryByteBuf, GalaxiesEntitySpawnS2CPacket> CODEC = PacketCodec.of(GalaxiesEntitySpawnS2CPacket::write, GalaxiesEntitySpawnS2CPacket::new);
 
 	private final Vec3d velocity;
 	private final float yaw;
 	private final float pitch;
+	private final NbtCompound customData;
 
-	public GalaxiesEntitySpawnS2CPacket(Entity entity, EntityTrackerEntry entityTrackerEntry)
+	/**
+	 * Constructs a new {@link GalaxiesEntitySpawnS2CPacket}
+	 *
+	 * @param entity             The entity that generated the packet
+	 * @param entityTrackerEntry The tracker entry for the entity
+	 */
+	public <T> GalaxiesEntitySpawnS2CPacket(Entity entity, EntityTrackerEntry entityTrackerEntry, Codec<T> codec, T value)
 	{
 		super(entity, entityTrackerEntry);
 
 		this.velocity = entity.getVelocity();
 		this.yaw = entity.getYaw();
 		this.pitch = entity.getPitch();
+		customData = (NbtCompound)codec
+				.fieldOf(CUSTOM_DATA_FIELD)
+				.codec()
+				.encode(value, entity.getRegistryManager().getOps(NbtOps.INSTANCE), new NbtCompound())
+				.getOrThrow();
+	}
+
+	public GalaxiesEntitySpawnS2CPacket(Entity entity, EntityTrackerEntry entityTrackerEntry)
+	{
+		this(entity, entityTrackerEntry, Codec.EMPTY.codec(), null);
 	}
 
 	public GalaxiesEntitySpawnS2CPacket(RegistryByteBuf buf)
@@ -37,6 +64,7 @@ public GalaxiesEntitySpawnS2CPacket(RegistryByteBuf buf)
 		this.velocity = buf.readVec3d();
 		this.yaw = buf.readFloat();
 		this.pitch = buf.readFloat();
+		this.customData = buf.readNbt();
 	}
 
 	@Override
@@ -47,6 +75,7 @@ public void write(RegistryByteBuf buf)
 		buf.writeVec3d(this.velocity);
 		buf.writeFloat(this.yaw);
 		buf.writeFloat(this.pitch);
+		buf.writeNbt(this.customData);
 	}
 
 	/**
@@ -57,6 +86,23 @@ public Vec3d getVelocity()
 		return velocity;
 	}
 
+	/**
+	 * A field within the custom payload serialized for this entity
+	 *
+	 * @param entity The entity from which a registry manager will be derived
+	 * @param codec  The map codec that will be used to deserialize the data
+	 * @param <T>    The type of data to deserialize
+	 *
+	 * @return The deserialized data
+	 */
+	public <T> DataResult<T> getCustomData(Entity entity, Codec<T> codec)
+	{
+		return codec
+				.fieldOf(CUSTOM_DATA_FIELD)
+				.codec()
+				.parse(entity.getRegistryManager().getOps(NbtOps.INSTANCE), customData);
+	}
+
 	@Override
 	public float getYaw()
 	{
diff --git a/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesNetworking.java b/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesNetworking.java
index 8aadc9103..c7f975985 100644
--- a/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesNetworking.java
+++ b/projects/pswg_core/src/main/java/dev/pswg/networking/GalaxiesNetworking.java
@@ -35,6 +35,6 @@ public static void sendToTracking(Entity entity, CustomPayload packet)
 	@SuppressWarnings("unchecked")
 	public static <T extends CustomPayload> Packet<ClientPlayPacketListener> createPlayS2CPacket(T packet)
 	{
-		return (Packet<ClientPlayPacketListener>)(Object)ServerPlayNetworking.createS2CPacket(packet);
+		return (Packet<ClientPlayPacketListener>)(Packet<?>)ServerPlayNetworking.createS2CPacket(packet);
 	}
 }