mirror of
https://github.com/Sollace/Unicopia.git
synced 2024-11-27 07:17:58 +01:00
Fix #287
This commit is contained in:
parent
58b134577f
commit
89aeb3903f
6 changed files with 52 additions and 29 deletions
|
@ -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
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue