Use RegistryOps in the correct places (#10843)

This commit is contained in:
Jake Potrebic 2024-06-02 15:19:17 -07:00 committed by GitHub
parent 5d8e53d78d
commit ac647513b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1157,7 +1157,7 @@ index 0000000000000000000000000000000000000000..2fd6c3e65354071af71c7d8ebb97b559
+} +}
diff --git a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java diff --git a/src/main/java/io/papermc/paper/adventure/PaperAdventure.java b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..fc6e13e4f2408ccbfa645eae2d7ebf4dcfc21908 index 0000000000000000000000000000000000000000..7397918cf747bc2352bf5bb112a71e7f6844e0e0
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java +++ b/src/main/java/io/papermc/paper/adventure/PaperAdventure.java
@@ -0,0 +1,478 @@ @@ -0,0 +1,478 @@
@ -1594,7 +1594,7 @@ index 0000000000000000000000000000000000000000..fc6e13e4f2408ccbfa645eae2d7ebf4d
+ +
+ @Override + @Override
+ public @NotNull BinaryTagHolder asBinaryTag() { + public @NotNull BinaryTagHolder asBinaryTag() {
+ return BinaryTagHolder.encode(this.codec.encodeStart(NbtOps.INSTANCE, this.value).getOrThrow(IllegalArgumentException::new), NBT_CODEC); + return BinaryTagHolder.encode(this.codec.encodeStart(CraftRegistry.getMinecraftRegistry().createSerializationContext(NbtOps.INSTANCE), this.value).getOrThrow(IllegalArgumentException::new), NBT_CODEC);
+ } + }
+ } + }
+ +
@ -1622,7 +1622,7 @@ index 0000000000000000000000000000000000000000..fc6e13e4f2408ccbfa645eae2d7ebf4d
+ // Style + // Style
+ +
+ public static net.minecraft.network.chat.Style asVanilla(final Style style) { + public static net.minecraft.network.chat.Style asVanilla(final Style style) {
+ final RegistryOps<Object> ops = RegistryOps.create(JavaOps.INSTANCE, CraftRegistry.getMinecraftRegistry()); + final RegistryOps<Object> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(JavaOps.INSTANCE);
+ final Object encoded = AdventureCodecs.STYLE_MAP_CODEC.codec() + final Object encoded = AdventureCodecs.STYLE_MAP_CODEC.codec()
+ .encodeStart(ops, style).getOrThrow(IllegalStateException::new); + .encodeStart(ops, style).getOrThrow(IllegalStateException::new);
+ +
@ -1631,7 +1631,7 @@ index 0000000000000000000000000000000000000000..fc6e13e4f2408ccbfa645eae2d7ebf4d
+ } + }
+ +
+ public static Style asAdventure(final net.minecraft.network.chat.Style style) { + public static Style asAdventure(final net.minecraft.network.chat.Style style) {
+ final RegistryOps<Object> ops = RegistryOps.create(JavaOps.INSTANCE, CraftRegistry.getMinecraftRegistry()); + final RegistryOps<Object> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(JavaOps.INSTANCE);
+ final Object encoded = net.minecraft.network.chat.Style.Serializer.CODEC + final Object encoded = net.minecraft.network.chat.Style.Serializer.CODEC
+ .encodeStart(ops, style).getOrThrow(IllegalStateException::new); + .encodeStart(ops, style).getOrThrow(IllegalStateException::new);
+ +
@ -1641,7 +1641,7 @@ index 0000000000000000000000000000000000000000..fc6e13e4f2408ccbfa645eae2d7ebf4d
+} +}
diff --git a/src/main/java/io/papermc/paper/adventure/WrapperAwareSerializer.java b/src/main/java/io/papermc/paper/adventure/WrapperAwareSerializer.java diff --git a/src/main/java/io/papermc/paper/adventure/WrapperAwareSerializer.java b/src/main/java/io/papermc/paper/adventure/WrapperAwareSerializer.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..e58e8ac574eb60205b17e31589f5a7610c60a4e2 index 0000000000000000000000000000000000000000..c786ddf0ef19757011452204fd11d24541c39d9e
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/adventure/WrapperAwareSerializer.java +++ b/src/main/java/io/papermc/paper/adventure/WrapperAwareSerializer.java
@@ -0,0 +1,34 @@ @@ -0,0 +1,34 @@
@ -1661,7 +1661,7 @@ index 0000000000000000000000000000000000000000..e58e8ac574eb60205b17e31589f5a761
+ if (input instanceof AdventureComponent) { + if (input instanceof AdventureComponent) {
+ return ((AdventureComponent) input).adventure; + return ((AdventureComponent) input).adventure;
+ } + }
+ final RegistryOps<Object> ops = RegistryOps.create(JavaOps.INSTANCE, CraftRegistry.getMinecraftRegistry()); + final RegistryOps<Object> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(JavaOps.INSTANCE);
+ final Object obj = ComponentSerialization.CODEC.encodeStart(ops, input) + final Object obj = ComponentSerialization.CODEC.encodeStart(ops, input)
+ .getOrThrow(s -> new RuntimeException("Failed to encode Minecraft Component: " + input + "; " + s)); + .getOrThrow(s -> new RuntimeException("Failed to encode Minecraft Component: " + input + "; " + s));
+ final Pair<Component, Object> converted = AdventureCodecs.COMPONENT_CODEC.decode(ops, obj) + final Pair<Component, Object> converted = AdventureCodecs.COMPONENT_CODEC.decode(ops, obj)
@ -1671,7 +1671,7 @@ index 0000000000000000000000000000000000000000..e58e8ac574eb60205b17e31589f5a761
+ +
+ @Override + @Override
+ public net.minecraft.network.chat.Component serialize(final Component component) { + public net.minecraft.network.chat.Component serialize(final Component component) {
+ final RegistryOps<Object> ops = RegistryOps.create(JavaOps.INSTANCE, CraftRegistry.getMinecraftRegistry()); + final RegistryOps<Object> ops = CraftRegistry.getMinecraftRegistry().createSerializationContext(JavaOps.INSTANCE);
+ final Object obj = AdventureCodecs.COMPONENT_CODEC.encodeStart(ops, component) + final Object obj = AdventureCodecs.COMPONENT_CODEC.encodeStart(ops, component)
+ .getOrThrow(s -> new RuntimeException("Failed to encode adventure Component: " + component + "; " + s)); + .getOrThrow(s -> new RuntimeException("Failed to encode adventure Component: " + component + "; " + s));
+ final Pair<net.minecraft.network.chat.Component, Object> converted = ComponentSerialization.CODEC.decode(ops, obj) + final Pair<net.minecraft.network.chat.Component, Object> converted = ComponentSerialization.CODEC.decode(ops, obj)
@ -1829,14 +1829,15 @@ index 0000000000000000000000000000000000000000..8323f135d6bf2e1f12525e05094ffa3f
+} +}
diff --git a/src/main/java/io/papermc/paper/adventure/providers/DataComponentValueConverterProviderImpl.java b/src/main/java/io/papermc/paper/adventure/providers/DataComponentValueConverterProviderImpl.java diff --git a/src/main/java/io/papermc/paper/adventure/providers/DataComponentValueConverterProviderImpl.java b/src/main/java/io/papermc/paper/adventure/providers/DataComponentValueConverterProviderImpl.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..ef19891a895e699cac638bd9b9f2fa0eaf1371ba index 0000000000000000000000000000000000000000..6b37c0ebda4a0cfcf9c3b2c3483ffababe622555
--- /dev/null --- /dev/null
+++ b/src/main/java/io/papermc/paper/adventure/providers/DataComponentValueConverterProviderImpl.java +++ b/src/main/java/io/papermc/paper/adventure/providers/DataComponentValueConverterProviderImpl.java
@@ -0,0 +1,75 @@ @@ -0,0 +1,82 @@
+package io.papermc.paper.adventure.providers; +package io.papermc.paper.adventure.providers;
+ +
+import com.google.gson.JsonElement; +import com.google.gson.JsonElement;
+import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.brigadier.exceptions.CommandSyntaxException;
+import com.mojang.serialization.DynamicOps;
+import com.mojang.serialization.JsonOps; +import com.mojang.serialization.JsonOps;
+import io.papermc.paper.adventure.PaperAdventure; +import io.papermc.paper.adventure.PaperAdventure;
+import java.util.List; +import java.util.List;
@ -1849,6 +1850,8 @@ index 0000000000000000000000000000000000000000..ef19891a895e699cac638bd9b9f2fa0e
+import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.BuiltInRegistries;
+import net.minecraft.nbt.NbtOps; +import net.minecraft.nbt.NbtOps;
+import net.minecraft.nbt.Tag; +import net.minecraft.nbt.Tag;
+import net.minecraft.resources.RegistryOps;
+import org.bukkit.craftbukkit.CraftRegistry;
+import org.checkerframework.checker.nullness.qual.NonNull; +import org.checkerframework.checker.nullness.qual.NonNull;
+import org.checkerframework.checker.nullness.qual.Nullable; +import org.checkerframework.checker.nullness.qual.Nullable;
+import org.checkerframework.framework.qual.DefaultQualifier; +import org.checkerframework.framework.qual.DefaultQualifier;
@ -1865,6 +1868,10 @@ index 0000000000000000000000000000000000000000..ef19891a895e699cac638bd9b9f2fa0e
+ return ID; + return ID;
+ } + }
+ +
+ private static <T> RegistryOps<T> createOps(final DynamicOps<T> delegate) {
+ return CraftRegistry.getMinecraftRegistry().createSerializationContext(delegate);
+ }
+
+ @SuppressWarnings({"unchecked", "rawtypes"}) + @SuppressWarnings({"unchecked", "rawtypes"})
+ @Override + @Override
+ public Iterable<DataComponentValueConverterRegistry.Conversion<?, ?>> conversions() { + public Iterable<DataComponentValueConverterRegistry.Conversion<?, ?>> conversions() {
@ -1872,7 +1879,7 @@ index 0000000000000000000000000000000000000000..ef19891a895e699cac638bd9b9f2fa0e
+ DataComponentValueConverterRegistry.Conversion.convert( + DataComponentValueConverterRegistry.Conversion.convert(
+ PaperAdventure.DataComponentValueImpl.class, + PaperAdventure.DataComponentValueImpl.class,
+ GsonDataComponentValue.class, + GsonDataComponentValue.class,
+ (key, dataComponentValue) -> gsonDataComponentValue((JsonElement) dataComponentValue.codec().encodeStart(JsonOps.INSTANCE, dataComponentValue.value()).getOrThrow()) + (key, dataComponentValue) -> gsonDataComponentValue((JsonElement) dataComponentValue.codec().encodeStart(createOps(JsonOps.INSTANCE), dataComponentValue.value()).getOrThrow())
+ ), + ),
+ DataComponentValueConverterRegistry.Conversion.convert( + DataComponentValueConverterRegistry.Conversion.convert(
+ GsonDataComponentValue.class, + GsonDataComponentValue.class,
@ -1882,13 +1889,13 @@ index 0000000000000000000000000000000000000000..ef19891a895e699cac638bd9b9f2fa0e
+ if (type == null) { + if (type == null) {
+ throw new IllegalArgumentException("Unknown data component type: " + key); + throw new IllegalArgumentException("Unknown data component type: " + key);
+ } + }
+ return new PaperAdventure.DataComponentValueImpl(type.codecOrThrow(), type.codecOrThrow().parse(JsonOps.INSTANCE, dataComponentValue.element()).getOrThrow(IllegalArgumentException::new)); + return new PaperAdventure.DataComponentValueImpl(type.codecOrThrow(), type.codecOrThrow().parse(createOps(JsonOps.INSTANCE), dataComponentValue.element()).getOrThrow(IllegalArgumentException::new));
+ } + }
+ ), + ),
+ DataComponentValueConverterRegistry.Conversion.convert( + DataComponentValueConverterRegistry.Conversion.convert(
+ PaperAdventure.DataComponentValueImpl.class, + PaperAdventure.DataComponentValueImpl.class,
+ DataComponentValue.TagSerializable.class, + DataComponentValue.TagSerializable.class,
+ (key, dataComponentValue) -> BinaryTagHolder.encode((Tag) dataComponentValue.codec().encodeStart(NbtOps.INSTANCE, dataComponentValue.value()).getOrThrow(), PaperAdventure.NBT_CODEC) + (key, dataComponentValue) -> BinaryTagHolder.encode((Tag) dataComponentValue.codec().encodeStart(createOps(NbtOps.INSTANCE), dataComponentValue.value()).getOrThrow(), PaperAdventure.NBT_CODEC)
+ ), + ),
+ DataComponentValueConverterRegistry.Conversion.convert( + DataComponentValueConverterRegistry.Conversion.convert(
+ DataComponentValue.TagSerializable.class, + DataComponentValue.TagSerializable.class,
@ -1899,7 +1906,7 @@ index 0000000000000000000000000000000000000000..ef19891a895e699cac638bd9b9f2fa0e
+ throw new IllegalArgumentException("Unknown data component type: " + key); + throw new IllegalArgumentException("Unknown data component type: " + key);
+ } + }
+ try { + try {
+ return new PaperAdventure.DataComponentValueImpl(type.codecOrThrow(), type.codecOrThrow().parse(NbtOps.INSTANCE, tagSerializable.asBinaryTag().get(PaperAdventure.NBT_CODEC)).getOrThrow(IllegalArgumentException::new)); + return new PaperAdventure.DataComponentValueImpl(type.codecOrThrow(), type.codecOrThrow().parse(createOps(NbtOps.INSTANCE), tagSerializable.asBinaryTag().get(PaperAdventure.NBT_CODEC)).getOrThrow(IllegalArgumentException::new));
+ } catch (final CommandSyntaxException e) { + } catch (final CommandSyntaxException e) {
+ throw new IllegalArgumentException(e); + throw new IllegalArgumentException(e);
+ } + }
@ -5711,7 +5718,7 @@ index 0000000000000000000000000000000000000000..3aedd0bbc97edacc1ebf71264b310e55
+} +}
diff --git a/src/test/java/io/papermc/paper/adventure/AdventureCodecsTest.java b/src/test/java/io/papermc/paper/adventure/AdventureCodecsTest.java diff --git a/src/test/java/io/papermc/paper/adventure/AdventureCodecsTest.java b/src/test/java/io/papermc/paper/adventure/AdventureCodecsTest.java
new file mode 100644 new file mode 100644
index 0000000000000000000000000000000000000000..0b23e6aab901d359845573e12e939a231ee30ada index 0000000000000000000000000000000000000000..69b3aa957161e75d6344e437a8b4a4533ef00523
--- /dev/null --- /dev/null
+++ b/src/test/java/io/papermc/paper/adventure/AdventureCodecsTest.java +++ b/src/test/java/io/papermc/paper/adventure/AdventureCodecsTest.java
@@ -0,0 +1,403 @@ @@ -0,0 +1,403 @@
@ -5923,7 +5930,7 @@ index 0000000000000000000000000000000000000000..0b23e6aab901d359845573e12e939a23
+ JavaOps.INSTANCE, + JavaOps.INSTANCE,
+ JsonOps.INSTANCE + JsonOps.INSTANCE
+ ) + )
+ .map(ops -> RegistryOps.create(ops, AbstractTestingBase.REGISTRY_CUSTOM)) + .map(ops -> AbstractTestingBase.REGISTRY_CUSTOM.createSerializationContext(ops))
+ .toList(); + .toList();
+ } + }
+ +