This commit is contained in:
Sollace 2024-03-02 17:08:17 +00:00
parent 58b134577f
commit 89aeb3903f
No known key found for this signature in database
GPG key ID: E52FACE7B5C773DB
6 changed files with 52 additions and 29 deletions

View file

@ -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

View file

@ -32,7 +32,7 @@ public record LightningBoltParticleEffect (
}
protected LightningBoltParticleEffect(ParticleType<LightningBoltParticleEffect> 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

View file

@ -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<ParticleEffect> PARTICLE_EFFECT_CODEC = new PacketCodec<>(
buf -> {
@SuppressWarnings("unchecked")
ParticleType<ParticleEffect> type = (ParticleType<ParticleEffect>)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<ParticleEffect>> OPTIONAL_PARTICLE_EFFECT_CODEC = PARTICLE_EFFECT_CODEC.asOptional();
PacketCodec<Vec3d> 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<Vec3d>> OPTIONAL_VECTOR_CODEC = VECTOR_CODEC.asOptional();
@SuppressWarnings("unchecked")
static <T extends ParticleEffect> T read(StringReader reader) throws CommandSyntaxException {
return (T)ParticleEffectArgumentType.readParameters(reader, Registries.PARTICLE_TYPE.getReadOnlyWrapper());
}
@SuppressWarnings("deprecation")
static <T extends ParticleEffect> ParticleEffect readEffect(PacketByteBuf buf) {
@SuppressWarnings("unchecked")
ParticleType<T> type = (ParticleType<T>)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();

View file

@ -32,7 +32,7 @@ public record SphereParticleEffect (
}
protected SphereParticleEffect(ParticleType<? extends SphereParticleEffect> 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<? extends SphereParticleEffect> 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

View file

@ -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);
}
}

View file

@ -0,0 +1,20 @@
package com.minelittlepony.unicopia.util.serialization;
import java.util.Optional;
import net.minecraft.network.PacketByteBuf;
public record PacketCodec<T>(PacketByteBuf.PacketReader<T> reader, PacketByteBuf.PacketWriter<T> writer) {
public T read(PacketByteBuf buf) {
return reader().apply(buf);
}
public void write(PacketByteBuf buf, T value) {
writer().accept(buf, value);
}
public PacketCodec<Optional<T>> asOptional() {
return new PacketCodec<>(buf -> buf.readOptional(reader), (buf, v) -> buf.writeOptional(v, writer));
}
}