From 89aeb3903f0740320aacce9d117bd79000689e4f Mon Sep 17 00:00:00 2001 From: Sollace Date: Sat, 2 Mar 2024 17:08:17 +0000 Subject: [PATCH] Fix #287 --- .../particle/FollowingParticleEffect.java | 8 +--- .../particle/LightningBoltParticleEffect.java | 4 +- .../particle/ParticleFactoryHelper.java | 41 +++++++++++-------- .../particle/SphereParticleEffect.java | 4 +- .../unicopia/particle/WeakTarget.java | 4 +- .../util/serialization/PacketCodec.java | 20 +++++++++ 6 files changed, 52 insertions(+), 29 deletions(-) create mode 100644 src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java diff --git a/src/main/java/com/minelittlepony/unicopia/particle/FollowingParticleEffect.java b/src/main/java/com/minelittlepony/unicopia/particle/FollowingParticleEffect.java index 4455f7f7..42482d6a 100644 --- a/src/main/java/com/minelittlepony/unicopia/particle/FollowingParticleEffect.java +++ b/src/main/java/com/minelittlepony/unicopia/particle/FollowingParticleEffect.java @@ -36,7 +36,7 @@ public record FollowingParticleEffect ( this(type, new WeakTarget(buf), buf.readFloat(), - buf.readOptional(b -> ParticleFactoryHelper.readEffect(b)) + ParticleFactoryHelper.OPTIONAL_PARTICLE_EFFECT_CODEC.read(buf) ); } @@ -61,11 +61,7 @@ public record FollowingParticleEffect ( public void write(PacketByteBuf buf) { target.write(buf); buf.writeFloat(followSpeed); - buf.writeOptional(childEffect(), (b, child) -> { - b.writeBoolean(true); - b.writeInt(Registries.PARTICLE_TYPE.getRawId(child.getType())); - child.write(buf); - }); + ParticleFactoryHelper.OPTIONAL_PARTICLE_EFFECT_CODEC.write(buf, childEffect()); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/particle/LightningBoltParticleEffect.java b/src/main/java/com/minelittlepony/unicopia/particle/LightningBoltParticleEffect.java index 321b6114..19274a80 100644 --- a/src/main/java/com/minelittlepony/unicopia/particle/LightningBoltParticleEffect.java +++ b/src/main/java/com/minelittlepony/unicopia/particle/LightningBoltParticleEffect.java @@ -32,7 +32,7 @@ public record LightningBoltParticleEffect ( } protected LightningBoltParticleEffect(ParticleType particleType, PacketByteBuf buf) { - this(buf.readBoolean(), buf.readInt(), buf.readInt(), buf.readFloat(), buf.readOptional(ParticleFactoryHelper::readVector)); + this(buf.readBoolean(), buf.readInt(), buf.readInt(), buf.readFloat(), ParticleFactoryHelper.OPTIONAL_VECTOR_CODEC.read(buf)); } @Override @@ -46,7 +46,7 @@ public record LightningBoltParticleEffect ( buffer.writeInt(changeFrequency); buffer.writeInt(maxBranches); buffer.writeFloat(maxDeviation); - buffer.writeOptional(pathEndPoint, ParticleFactoryHelper::writeVector); + ParticleFactoryHelper.OPTIONAL_VECTOR_CODEC.write(buffer, pathEndPoint); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/particle/ParticleFactoryHelper.java b/src/main/java/com/minelittlepony/unicopia/particle/ParticleFactoryHelper.java index 7890f91f..af291232 100644 --- a/src/main/java/com/minelittlepony/unicopia/particle/ParticleFactoryHelper.java +++ b/src/main/java/com/minelittlepony/unicopia/particle/ParticleFactoryHelper.java @@ -1,6 +1,8 @@ package com.minelittlepony.unicopia.particle; import java.util.Optional; + +import com.minelittlepony.unicopia.util.serialization.PacketCodec; import com.mojang.brigadier.StringReader; import com.mojang.brigadier.exceptions.CommandSyntaxException; @@ -12,33 +14,38 @@ import net.minecraft.registry.Registries; import net.minecraft.util.math.Vec3d; public interface ParticleFactoryHelper { + @SuppressWarnings("deprecation") + PacketCodec PARTICLE_EFFECT_CODEC = new PacketCodec<>( + buf -> { + @SuppressWarnings("unchecked") + ParticleType type = (ParticleType)Registries.PARTICLE_TYPE.get(buf.readInt()); + return type.getParametersFactory().read(type, buf); + }, + (buf, effect) -> { + buf.writeInt(Registries.PARTICLE_TYPE.getRawId(effect.getType())); + effect.write(buf); + } + ); + PacketCodec> OPTIONAL_PARTICLE_EFFECT_CODEC = PARTICLE_EFFECT_CODEC.asOptional(); + PacketCodec VECTOR_CODEC = new PacketCodec<>( + buf -> new Vec3d(buf.readDouble(), buf.readDouble(), buf.readDouble()), + (buf, vector) -> { + buf.writeDouble(vector.x); + buf.writeDouble(vector.y); + buf.writeDouble(vector.z); + } + ); + PacketCodec> OPTIONAL_VECTOR_CODEC = VECTOR_CODEC.asOptional(); @SuppressWarnings("unchecked") static T read(StringReader reader) throws CommandSyntaxException { return (T)ParticleEffectArgumentType.readParameters(reader, Registries.PARTICLE_TYPE.getReadOnlyWrapper()); } - @SuppressWarnings("deprecation") - static ParticleEffect readEffect(PacketByteBuf buf) { - @SuppressWarnings("unchecked") - ParticleType type = (ParticleType)Registries.PARTICLE_TYPE.get(buf.readInt()); - return type.getParametersFactory().read(type, buf); - } - static Vec3d readVector(StringReader reader) throws CommandSyntaxException { return new Vec3d(readDouble(reader), readDouble(reader), readDouble(reader)); } - static Vec3d readVector(PacketByteBuf buffer) { - return new Vec3d(buffer.readDouble(), buffer.readDouble(), buffer.readDouble()); - } - - static void writeVector(PacketByteBuf buffer, Vec3d vector) { - buffer.writeDouble(vector.x); - buffer.writeDouble(vector.y); - buffer.writeDouble(vector.z); - } - static boolean readBoolean(StringReader reader) throws CommandSyntaxException { reader.expect(' '); return reader.readBoolean(); diff --git a/src/main/java/com/minelittlepony/unicopia/particle/SphereParticleEffect.java b/src/main/java/com/minelittlepony/unicopia/particle/SphereParticleEffect.java index 702610d4..41f95970 100644 --- a/src/main/java/com/minelittlepony/unicopia/particle/SphereParticleEffect.java +++ b/src/main/java/com/minelittlepony/unicopia/particle/SphereParticleEffect.java @@ -32,7 +32,7 @@ public record SphereParticleEffect ( } protected SphereParticleEffect(ParticleType type, PacketByteBuf buf) { - this(type, buf.readVector3f(), buf.readFloat(), buf.readFloat(), ParticleFactoryHelper.readVector(buf)); + this(type, buf.readVector3f(), buf.readFloat(), buf.readFloat(), ParticleFactoryHelper.VECTOR_CODEC.read(buf)); } public SphereParticleEffect(ParticleType type, int tint, float alpha, float rad) { @@ -61,7 +61,7 @@ public record SphereParticleEffect ( buf.writeVector3f(color); buf.writeFloat(alpha); buf.writeFloat(radius); - ParticleFactoryHelper.writeVector(buf, offset); + ParticleFactoryHelper.VECTOR_CODEC.write(buf, offset); } @Override diff --git a/src/main/java/com/minelittlepony/unicopia/particle/WeakTarget.java b/src/main/java/com/minelittlepony/unicopia/particle/WeakTarget.java index 9dd5ace2..770ea2db 100644 --- a/src/main/java/com/minelittlepony/unicopia/particle/WeakTarget.java +++ b/src/main/java/com/minelittlepony/unicopia/particle/WeakTarget.java @@ -20,7 +20,7 @@ public class WeakTarget { } public WeakTarget(PacketByteBuf buf) { - fixedPosition = ParticleFactoryHelper.readVector(buf); + fixedPosition = ParticleFactoryHelper.VECTOR_CODEC.read(buf); targetId = buf.readInt(); } @@ -49,7 +49,7 @@ public class WeakTarget { } public void write(PacketByteBuf buf) { - ParticleFactoryHelper.writeVector(buf, fixedPosition); + ParticleFactoryHelper.VECTOR_CODEC.write(buf, fixedPosition); buf.writeInt(targetId); } } diff --git a/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java b/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java new file mode 100644 index 00000000..a8f27440 --- /dev/null +++ b/src/main/java/com/minelittlepony/unicopia/util/serialization/PacketCodec.java @@ -0,0 +1,20 @@ +package com.minelittlepony.unicopia.util.serialization; + +import java.util.Optional; + +import net.minecraft.network.PacketByteBuf; + +public record PacketCodec(PacketByteBuf.PacketReader reader, PacketByteBuf.PacketWriter writer) { + + public T read(PacketByteBuf buf) { + return reader().apply(buf); + } + + public void write(PacketByteBuf buf, T value) { + writer().accept(buf, value); + } + + public PacketCodec> asOptional() { + return new PacketCodec<>(buf -> buf.readOptional(reader), (buf, v) -> buf.writeOptional(v, writer)); + } +}