Skip to content

Commit

Permalink
Fix AmbientParticle serialization, update to 1.4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed May 13, 2023
1 parent 298768d commit 1398ab0
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,45 +1,42 @@
package com.owen1212055.biomevisuals.api.types.biome.effect;

import com.google.gson.JsonObject;
import org.bukkit.Particle;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

public final class AmbientParticle {

@NotNull
private final Particle particle;
@Nullable
private final Object data;
private final JsonObject particle;
private final float probability;

private AmbientParticle(@NotNull Particle particle, @Nullable Object data, float probability) {
this.particle = particle;
this.data = data;
private AmbientParticle( @NotNull JsonObject particleData, float probability) {
this.particle = particleData;
this.probability = probability;
}

@NotNull
public static AmbientParticle of(@NotNull Particle particle, float probability, @Nullable Object data) {
return new AmbientParticle(particle, data, probability);
public static AmbientParticle of(float probability, @NotNull JsonObject particleData) {
return new AmbientParticle(particleData, probability);
}

@NotNull
public static AmbientParticle of(@NotNull Particle particle, float probability) {
return new AmbientParticle(particle, null, probability);
public static AmbientParticle of(@NotNull Particle particle, float probability, @Nullable Object data) {
return new AmbientParticle(ParticleDataSerializer.of().serialize(particle, data), probability);
}

@NotNull
public Particle getParticle() {
return particle;
}

@Nullable
public Object getData() {
return data;
public static AmbientParticle of(@NotNull Particle particle, float probability) {
return new AmbientParticle(ParticleDataSerializer.of().serialize(particle, null), probability);
}

public float getProbability() {
return probability;
}

@NotNull
public JsonObject getParticle() {
return particle;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.owen1212055.biomevisuals.api.types.biome.effect;

import com.google.gson.JsonObject;
import org.bukkit.Particle;

public interface ParticleDataSerializer {

static ParticleDataSerializer of() {
return ParticleDataSerializerProvider.INSTANCE;
}

JsonObject serialize(Particle particle, Object data);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.owen1212055.biomevisuals.api.types.biome.effect;

import java.util.ServiceLoader;

class ParticleDataSerializerProvider {

static final ParticleDataSerializer INSTANCE = ServiceLoader.load(ParticleDataSerializer.class, ParticleDataSerializer.class.getClassLoader())
.findFirst()
.orElseThrow();
}
2 changes: 1 addition & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group = "com.owen1212055"
version = "1.4.1"
version = "1.4.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package com.owen1212055.biomevisuals.nms;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.mojang.datafixers.util.Pair;
import com.mojang.serialization.Codec;
import com.mojang.serialization.JsonOps;
import com.owen1212055.biomevisuals.api.types.biome.effect.AdditionSound;
import com.owen1212055.biomevisuals.api.types.biome.effect.AmbientParticle;
import com.owen1212055.biomevisuals.api.types.biome.effect.AmbientSound;
import com.owen1212055.biomevisuals.api.types.biome.effect.MoodSound;
import com.owen1212055.biomevisuals.api.types.biome.effect.Music;
import com.owen1212055.biomevisuals.api.types.biome.effect.*;
import net.minecraft.core.Holder;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.biome.AmbientAdditionsSettings;
Expand All @@ -26,6 +26,7 @@
public class ApiEntityConverter {

private static final Field probabilityField;

static {
try {
probabilityField = AmbientParticleSettings.class.getDeclaredField(Mappings.AMBIENT_PARTICLE_PROBABILITY);
Expand All @@ -38,26 +39,27 @@ public class ApiEntityConverter {
public static JsonElement serialize(AdditionSound sound) {
AmbientAdditionsSettings settings = new AmbientAdditionsSettings(Holder.direct(CraftSound.getSoundEffect(sound.getSoundEvent())), sound.getTickChance());

return AmbientAdditionsSettings.CODEC.encode(settings, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
return encode(AmbientAdditionsSettings.CODEC, settings);
}

public static AdditionSound deserializeAdditionSound(JsonElement sound) {
AmbientAdditionsSettings settings = AmbientAdditionsSettings.CODEC.decode(JsonOps.INSTANCE, sound).map(Pair::getFirst).result().orElseThrow();
AmbientAdditionsSettings settings = decode(AmbientAdditionsSettings.CODEC, sound);

return AdditionSound.of(Objects.requireNonNull(nmsSoundEventToBukkit(settings.getSoundEvent().value())), settings.getTickChance());
}

public static JsonElement serialize(AmbientParticle particle) {
AmbientParticleSettings settings = new AmbientParticleSettings(CraftParticle.toNMS(particle.getParticle(), particle.getData()), particle.getProbability());
ParticleOptions options = decode(ParticleTypes.CODEC, particle.getParticle());
AmbientParticleSettings settings = new AmbientParticleSettings(options, particle.getProbability());

return AmbientParticleSettings.CODEC.encode(settings, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
return encode(AmbientParticleSettings.CODEC, settings);
}

public static AmbientParticle deserializeAmbientParticle(JsonElement particle) {
AmbientParticleSettings settings = AmbientParticleSettings.CODEC.decode(JsonOps.INSTANCE, particle).map(Pair::getFirst).result().orElseThrow();
AmbientParticleSettings settings = decode(AmbientParticleSettings.CODEC, particle);

try {
return AmbientParticle.of(CraftParticle.toBukkit(settings.getOptions()), (float) probabilityField.get(settings));
return AmbientParticle.of((float) probabilityField.get(settings), (JsonObject) encode(ParticleTypes.CODEC, settings.getOptions()));
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
Expand All @@ -66,35 +68,35 @@ public static AmbientParticle deserializeAmbientParticle(JsonElement particle) {
public static JsonElement serialize(AmbientSound ambientSound) {
SoundEvent soundEvent = SoundEvent.createVariableRangeEvent(new ResourceLocation(ambientSound.getSoundEvent().getKey().toString()));

return SoundEvent.CODEC.encode(Holder.direct(soundEvent), JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
return encode(SoundEvent.CODEC, Holder.direct(soundEvent));
}

public static AmbientSound deserializeAmbientSound(JsonElement ambientSound) {
Holder<SoundEvent> soundEvent = SoundEvent.CODEC.decode(JsonOps.INSTANCE, ambientSound).map(Pair::getFirst).result().orElseThrow();
Holder<SoundEvent> soundEvent = decode(SoundEvent.CODEC, ambientSound);

return AmbientSound.of(Objects.requireNonNull(nmsSoundEventToBukkit(soundEvent.value())));
}

public static JsonElement serialize(MoodSound moodSound) {
AmbientMoodSettings settings = new AmbientMoodSettings(Holder.direct(CraftSound.getSoundEffect(moodSound.getSoundEvent())), moodSound.getTickDelay(), moodSound.getBlockSearchExtent(), moodSound.getSoundPositionOffset());

return AmbientMoodSettings.CODEC.encode(settings, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
return encode(AmbientMoodSettings.CODEC, settings);
}

public static MoodSound deserializeMoodSound(JsonElement moodSound) {
AmbientMoodSettings settings = AmbientMoodSettings.CODEC.decode(JsonOps.INSTANCE, moodSound).map(Pair::getFirst).result().orElseThrow();
AmbientMoodSettings settings = decode(AmbientMoodSettings.CODEC, moodSound);

return MoodSound.of(Objects.requireNonNull(nmsSoundEventToBukkit(settings.getSoundEvent().value())), settings.getTickDelay(), settings.getBlockSearchExtent(), settings.getSoundPositionOffset());
}

public static JsonElement serialize(Music music) {
net.minecraft.sounds.Music nmsMusic = new net.minecraft.sounds.Music(Holder.direct(CraftSound.getSoundEffect(music.getSoundEvent())), music.getMinDelay(), music.getMaxDelay(), music.replaceCurrentMusic());

return net.minecraft.sounds.Music.CODEC.encode(nmsMusic, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
return encode(net.minecraft.sounds.Music.CODEC, nmsMusic);
}

public static Music deserializeMusic(JsonElement music) {
net.minecraft.sounds.Music nmsMusic = net.minecraft.sounds.Music.CODEC.decode(JsonOps.INSTANCE, music).map(Pair::getFirst).result().orElseThrow();
net.minecraft.sounds.Music nmsMusic = decode(net.minecraft.sounds.Music.CODEC, music);

return Music.of(Objects.requireNonNull(nmsSoundEventToBukkit(nmsMusic.getEvent().value())), nmsMusic.getMinDelay(), nmsMusic.getMaxDelay(), nmsMusic.replaceCurrentMusic());
}
Expand All @@ -104,4 +106,12 @@ public static Music deserializeMusic(JsonElement music) {
private static Sound nmsSoundEventToBukkit(SoundEvent soundEvent) {
return Registry.SOUNDS.get(Objects.requireNonNull(CraftNamespacedKey.fromMinecraft(soundEvent.getLocation())));
}

private static <T> JsonElement encode(Codec<T> type, T object) {
return type.encode(object, JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
}

private static <T> T decode(Codec<T> type, JsonElement object) {
return type.decode(JsonOps.INSTANCE, object).map(Pair::getFirst).result().orElseThrow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.owen1212055.biomevisuals.nms;

import com.google.gson.JsonObject;
import com.mojang.serialization.JsonOps;
import com.owen1212055.biomevisuals.api.types.biome.effect.ParticleDataSerializer;
import net.minecraft.core.particles.ParticleTypes;
import org.bukkit.Particle;
import org.bukkit.craftbukkit.v1_19_R3.CraftParticle;

public class ParticleDataSerializerImpl implements ParticleDataSerializer {
@Override
public JsonObject serialize(Particle particle, Object data) {
return (JsonObject) ParticleTypes.CODEC.encode(CraftParticle.toNMS(particle, data), JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
com.owen1212055.biomevisuals.nms.ParticleDataSerializerImpl
2 changes: 1 addition & 1 deletion plugin/src/main/resources/plugin.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: BiomeVisuals
version: '1.4.1'
version: '1.4.2'
main: com.owen1212055.biomevisuals.Main
api-version: 1.19
authors: [ Owen1212055 ]

0 comments on commit 1398ab0

Please sign in to comment.