Skip to content

Commit

Permalink
pass bolt nbt data along in spawn packet
Browse files Browse the repository at this point in the history
  • Loading branch information
parzivail committed Oct 26, 2024
1 parent ec050e6 commit 6a20d1e
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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);
}

/**
Expand All @@ -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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

0 comments on commit 6a20d1e

Please sign in to comment.