Skip to content

Commit

Permalink
v1.5 1.20.4 support
Browse files Browse the repository at this point in the history
  • Loading branch information
Owen1212055 committed Dec 27, 2023
1 parent 1398ab0 commit 123b2d7
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 17 deletions.
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.2"
version = "1.5"

repositories {
mavenCentral()
Expand Down
2 changes: 1 addition & 1 deletion nms/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ repositories {
}

dependencies {
paperDevBundle("1.19.4-R0.1-SNAPSHOT")
paperDevBundle("1.20.4-R0.1-SNAPSHOT")
implementation("xyz.jpenilla", "reflection-remapper", "0.1.0-SNAPSHOT")

implementation(project(":api")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,27 @@
import com.mojang.serialization.JsonOps;
import com.owen1212055.biomevisuals.api.types.biome.effect.*;
import net.minecraft.core.Holder;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.particles.ParticleOptions;
import net.minecraft.core.particles.ParticleTypes;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.resources.RegistryOps;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.sounds.SoundEvent;
import net.minecraft.world.level.biome.AmbientAdditionsSettings;
import net.minecraft.world.level.biome.AmbientMoodSettings;
import net.minecraft.world.level.biome.AmbientParticleSettings;
import org.bukkit.Registry;
import org.bukkit.Sound;
import org.bukkit.craftbukkit.v1_19_R3.CraftParticle;
import org.bukkit.craftbukkit.v1_19_R3.CraftSound;
import org.bukkit.craftbukkit.v1_19_R3.util.CraftNamespacedKey;
import org.bukkit.craftbukkit.v1_20_R3.CraftParticle;
import org.bukkit.craftbukkit.v1_20_R3.CraftSound;
import org.bukkit.craftbukkit.v1_20_R3.util.CraftNamespacedKey;

import java.lang.reflect.Field;
import java.util.Objects;

public class ApiEntityConverter {
private static final RegistryOps<JsonElement> BUILTIN_CONTEXT_OPS = RegistryOps.create(JsonOps.INSTANCE, RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));

private static final Field probabilityField;

Expand All @@ -37,7 +41,7 @@ public class ApiEntityConverter {
}

public static JsonElement serialize(AdditionSound sound) {
AmbientAdditionsSettings settings = new AmbientAdditionsSettings(Holder.direct(CraftSound.getSoundEffect(sound.getSoundEvent())), sound.getTickChance());
AmbientAdditionsSettings settings = new AmbientAdditionsSettings(Holder.direct(CraftSound.bukkitToMinecraft(sound.getSoundEvent())), sound.getTickChance());

return encode(AmbientAdditionsSettings.CODEC, settings);
}
Expand Down Expand Up @@ -78,7 +82,7 @@ public static AmbientSound deserializeAmbientSound(JsonElement ambientSound) {
}

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

return encode(AmbientMoodSettings.CODEC, settings);
}
Expand All @@ -90,7 +94,7 @@ public static MoodSound deserializeMoodSound(JsonElement moodSound) {
}

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());
net.minecraft.sounds.Music nmsMusic = new net.minecraft.sounds.Music(Holder.direct(CraftSound.bukkitToMinecraft(music.getSoundEvent())), music.getMinDelay(), music.getMaxDelay(), music.replaceCurrentMusic());

return encode(net.minecraft.sounds.Music.CODEC, nmsMusic);
}
Expand All @@ -108,10 +112,10 @@ private static Sound nmsSoundEventToBukkit(SoundEvent soundEvent) {
}

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

private static <T> T decode(Codec<T> type, JsonElement object) {
return type.decode(JsonOps.INSTANCE, object).map(Pair::getFirst).result().orElseThrow();
return type.decode(BUILTIN_CONTEXT_OPS, object).map(Pair::getFirst).result().orElseThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
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;
import org.bukkit.craftbukkit.v1_20_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();
return (JsonObject) ParticleTypes.CODEC.encode(CraftParticle.createParticleParam(particle, data), JsonOps.INSTANCE, JsonOps.INSTANCE.empty()).get().orThrow();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
import com.owen1212055.biomevisuals.nms.UnsafeUtils;
import net.minecraft.core.RegistryAccess;
import net.minecraft.core.RegistrySynchronization;
import net.minecraft.core.registries.BuiltInRegistries;
import net.minecraft.nbt.NbtOps;
import net.minecraft.nbt.Tag;
import net.minecraft.resources.RegistryOps;
import org.bukkit.Bukkit;
import org.bukkit.NamespacedKey;
import org.slf4j.Logger;
Expand All @@ -28,7 +32,7 @@
import static java.util.Map.Entry;

public class RegistryHook {

private static final RegistryOps<JsonElement> BUILTIN_CONTEXT_OPS = RegistryOps.create(JsonOps.INSTANCE, RegistryAccess.fromRegistryOfRegistries(BuiltInRegistries.REGISTRY));
private static Logger LOGGER;

// Custom codec that wraps around the pre-existing codec.
Expand All @@ -43,7 +47,7 @@ public static void injectCodec(Logger logger) throws Exception {

@Override
public <T> DataResult<T> encode(RegistryAccess input, DynamicOps<T> ops, T prefix) {
DataResult<JsonElement> result = CAPTURED_CODEC.encode(input, JsonOps.INSTANCE, JsonOps.INSTANCE.empty());
DataResult<JsonElement> result = CAPTURED_CODEC.encode(input, BUILTIN_CONTEXT_OPS, BUILTIN_CONTEXT_OPS.empty());
Optional<DataResult.PartialResult<JsonElement>> optionalError = result.error();
if (optionalError.isPresent()) {
LOGGER.warn("Failed to encode to JSON: " + optionalError.get().message());
Expand All @@ -63,7 +67,7 @@ public <T> DataResult<T> encode(RegistryAccess input, DynamicOps<T> ops, T prefi
}

// Decode it back into NMS type from json
DataResult<Pair<RegistryAccess, JsonElement>> dataresult = CAPTURED_CODEC.decode(JsonOps.INSTANCE, mainRegistry);
DataResult<Pair<RegistryAccess, JsonElement>> dataresult = CAPTURED_CODEC.decode(BUILTIN_CONTEXT_OPS, mainRegistry);
// Fail?
if (dataresult.error().isPresent()) {
LOGGER.warn("Failed to encode hooked data: {}", dataresult.error().get().message());
Expand Down
2 changes: 1 addition & 1 deletion plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ tasks {
}

runServer {
minecraftVersion("1.19.4")
minecraftVersion("1.20.4")
}
}

Expand Down
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.2'
version: '1.5'
main: com.owen1212055.biomevisuals.Main
api-version: 1.19
authors: [ Owen1212055 ]

0 comments on commit 123b2d7

Please sign in to comment.