Skip to content

Commit

Permalink
ParticleDatas StreamCodecs
Browse files Browse the repository at this point in the history
  • Loading branch information
Abbie5 authored and artemisSystem committed Nov 12, 2024
1 parent 9ba673b commit 2e4e119
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
*/
package vazkii.botania.client.fx;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import io.netty.buffer.ByteBuf;

public class SparkleParticleData implements ParticleOptions {
public static final Codec<SparkleParticleData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
public static final MapCodec<SparkleParticleData> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Codec.FLOAT.fieldOf("size").forGetter(d -> d.size),
Codec.FLOAT.fieldOf("r").forGetter(d -> d.r),
Codec.FLOAT.fieldOf("g").forGetter(d -> d.g),
Expand All @@ -33,6 +31,28 @@ public class SparkleParticleData implements ParticleOptions {
Codec.BOOL.fieldOf("fake").forGetter(d -> d.fake),
Codec.BOOL.fieldOf("corrupt").forGetter(d -> d.corrupt)
).apply(instance, SparkleParticleData::new));
public static final StreamCodec<ByteBuf, SparkleParticleData> STREAM_CODEC = StreamCodec.of(
(buf, d) -> {
buf.writeFloat(d.size);
buf.writeFloat(d.r);
buf.writeFloat(d.g);
buf.writeFloat(d.b);
buf.writeInt(d.m);
buf.writeBoolean(d.noClip);
buf.writeBoolean(d.fake);
buf.writeBoolean(d.corrupt);
},
buf -> new SparkleParticleData(
buf.readFloat(),
buf.readFloat(),
buf.readFloat(),
buf.readFloat(),
buf.readInt(),
buf.readBoolean(),
buf.readBoolean(),
buf.readBoolean()
)
);
public final float size;
public final float r, g, b;
public final int m;
Expand Down Expand Up @@ -72,53 +92,4 @@ private SparkleParticleData(float size, float r, float g, float b, int m, boolea
public ParticleType<SparkleParticleData> getType() {
return BotaniaParticles.SPARKLE;
}

@Override
public void writeToNetwork(FriendlyByteBuf buf) {
buf.writeFloat(size);
buf.writeFloat(r);
buf.writeFloat(g);
buf.writeFloat(b);
buf.writeInt(m);
buf.writeBoolean(noClip);
buf.writeBoolean(fake);
buf.writeBoolean(corrupt);
}

@NotNull
@Override
public String writeToString() {
return String.format(Locale.ROOT, "%s %.2f %.2f %.2f %.2f %d %s %s %s",
BuiltInRegistries.PARTICLE_TYPE.getKey(this.getType()), this.size, this.r, this.g, this.b, this.m, this.noClip, this.fake, this.corrupt);
}

public static final Deserializer<SparkleParticleData> DESERIALIZER = new Deserializer<>() {
@NotNull
@Override
public SparkleParticleData fromCommand(@NotNull ParticleType<SparkleParticleData> type, @NotNull StringReader reader) throws CommandSyntaxException {
reader.expect(' ');
float size = reader.readFloat();
reader.expect(' ');
float r = reader.readFloat();
reader.expect(' ');
float g = reader.readFloat();
reader.expect(' ');
float b = reader.readFloat();
reader.expect(' ');
int m = reader.readInt();
reader.expect(' ');
boolean noClip = reader.readBoolean();
reader.expect(' ');
boolean fake = reader.readBoolean();
reader.expect(' ');
boolean corrupt = reader.readBoolean();

return new SparkleParticleData(size, r, g, b, m, noClip, fake, corrupt);
}

@Override
public SparkleParticleData fromNetwork(@NotNull ParticleType<SparkleParticleData> type, FriendlyByteBuf buf) {
return new SparkleParticleData(buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readInt(), buf.readBoolean(), buf.readBoolean(), buf.readBoolean());
}
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,34 @@
*/
package vazkii.botania.client.fx;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.particle.ParticleProvider;
import net.minecraft.client.particle.SpriteSet;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

import org.jetbrains.annotations.NotNull;

public class SparkleParticleType extends ParticleType<SparkleParticleData> {
public SparkleParticleType() {
super(false, SparkleParticleData.DESERIALIZER);
super(false);
}

@NotNull
@Override
public Codec<SparkleParticleData> codec() {
public MapCodec<SparkleParticleData> codec() {
return SparkleParticleData.CODEC;
}

@Override
public StreamCodec<? super RegistryFriendlyByteBuf, SparkleParticleData> streamCodec() {
return SparkleParticleData.STREAM_CODEC;
}

public static class Factory implements ParticleProvider<SparkleParticleData> {
private final SpriteSet sprite;

Expand Down
81 changes: 24 additions & 57 deletions Xplat/src/main/java/vazkii/botania/client/fx/WispParticleData.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,20 @@
*/
package vazkii.botania.client.fx;

import com.mojang.brigadier.StringReader;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;
import com.mojang.serialization.codecs.RecordCodecBuilder;

import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.network.FriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

import org.jetbrains.annotations.NotNull;

import java.util.Locale;
import io.netty.buffer.ByteBuf;

public class WispParticleData implements ParticleOptions {
public static final Codec<WispParticleData> CODEC = RecordCodecBuilder.create(instance -> instance.group(
public static final MapCodec<WispParticleData> CODEC = RecordCodecBuilder.mapCodec(instance -> instance.group(
Codec.FLOAT.fieldOf("size").forGetter(d -> d.size),
Codec.FLOAT.fieldOf("r").forGetter(d -> d.r),
Codec.FLOAT.fieldOf("g").forGetter(d -> d.g),
Expand All @@ -33,6 +31,26 @@ public class WispParticleData implements ParticleOptions {
Codec.BOOL.fieldOf("noClip").forGetter(d -> d.noClip)
)
.apply(instance, WispParticleData::new));
public static final StreamCodec<ByteBuf, WispParticleData> STREAM_CODEC = StreamCodec.of(
(buf, d) -> {
buf.writeFloat(d.size);
buf.writeFloat(d.r);
buf.writeFloat(d.g);
buf.writeFloat(d.b);
buf.writeFloat(d.maxAgeMul);
buf.writeBoolean(d.depthTest);
buf.writeBoolean(d.noClip);
},
buf -> new WispParticleData(
buf.readFloat(),
buf.readFloat(),
buf.readFloat(),
buf.readFloat(),
buf.readFloat(),
buf.readBoolean(),
buf.readBoolean()
)
);
public final float size;
public final float r, g, b;
public final float maxAgeMul;
Expand Down Expand Up @@ -78,55 +96,4 @@ public WispParticleData withNoClip(boolean v) {
public ParticleType<WispParticleData> getType() {
return BotaniaParticles.WISP;
}

@Override
public void writeToNetwork(FriendlyByteBuf buf) {
buf.writeFloat(size);
buf.writeFloat(r);
buf.writeFloat(g);
buf.writeFloat(b);
buf.writeFloat(maxAgeMul);
buf.writeBoolean(depthTest);
buf.writeBoolean(noClip);
}

@NotNull
@Override
public String writeToString() {
return String.format(Locale.ROOT, "%s %.2f %.2f %.2f %.2f %.2f %s",
BuiltInRegistries.PARTICLE_TYPE.getKey(getType()), this.size, this.r, this.g, this.b, this.maxAgeMul, this.depthTest);
}

public static final Deserializer<WispParticleData> DESERIALIZER = new Deserializer<>() {
@NotNull
@Override
public WispParticleData fromCommand(@NotNull ParticleType<WispParticleData> type, @NotNull StringReader reader) throws CommandSyntaxException {
reader.expect(' ');
float size = reader.readFloat();
reader.expect(' ');
float r = reader.readFloat();
reader.expect(' ');
float g = reader.readFloat();
reader.expect(' ');
float b = reader.readFloat();
reader.expect(' ');
float mam = reader.readFloat();
boolean depth = true;
if (reader.canRead()) {
reader.expect(' ');
depth = reader.readBoolean();
}
boolean noClip = false;
if (reader.canRead()) {
reader.expect(' ');
depth = reader.readBoolean();
}
return new WispParticleData(size, r, g, b, mam, depth, noClip);
}

@Override
public WispParticleData fromNetwork(@NotNull ParticleType<WispParticleData> type, FriendlyByteBuf buf) {
return new WispParticleData(buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readFloat(), buf.readBoolean(), buf.readBoolean());
}
};
}
13 changes: 10 additions & 3 deletions Xplat/src/main/java/vazkii/botania/client/fx/WispParticleType.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,27 +8,34 @@
*/
package vazkii.botania.client.fx;

import com.mojang.serialization.Codec;
import com.mojang.serialization.MapCodec;

import net.minecraft.client.multiplayer.ClientLevel;
import net.minecraft.client.particle.Particle;
import net.minecraft.client.particle.ParticleProvider;
import net.minecraft.client.particle.SpriteSet;
import net.minecraft.core.particles.ParticleType;
import net.minecraft.network.RegistryFriendlyByteBuf;
import net.minecraft.network.codec.StreamCodec;

import org.jetbrains.annotations.NotNull;

public class WispParticleType extends ParticleType<WispParticleData> {
public WispParticleType() {
super(false, WispParticleData.DESERIALIZER);
super(false);
}

@NotNull
@Override
public Codec<WispParticleData> codec() {
public MapCodec<WispParticleData> codec() {
return WispParticleData.CODEC;
}

@Override
public StreamCodec<? super RegistryFriendlyByteBuf, WispParticleData> streamCodec() {
return WispParticleData.STREAM_CODEC;
}

public static class Factory implements ParticleProvider<WispParticleData> {
private final SpriteSet sprite;

Expand Down

0 comments on commit 2e4e119

Please sign in to comment.