diff --git a/patches/server/0420-initial-work-on-native-Adventure-codecs.patch b/patches/server/0420-initial-work-on-native-Adventure-codecs.patch index c0f5b8e3a9..c211c454d5 100644 --- a/patches/server/0420-initial-work-on-native-Adventure-codecs.patch +++ b/patches/server/0420-initial-work-on-native-Adventure-codecs.patch @@ -11,27 +11,35 @@ public net.minecraft.network.chat.contents.TranslatableContents filterAllowedArg diff --git a/src/main/java/io/papermc/paper/adventure/AdventureCodecs.java b/src/main/java/io/papermc/paper/adventure/AdventureCodecs.java new file mode 100644 -index 0000000000000000000000000000000000000000..db13894e2710b933f69f3adaa7e728461e3cf049 +index 0000000000000000000000000000000000000000..1a13bf3b79b2d11e4ce6cc46eff65fe81a05a1ad --- /dev/null +++ b/src/main/java/io/papermc/paper/adventure/AdventureCodecs.java -@@ -0,0 +1,277 @@ +@@ -0,0 +1,365 @@ +package io.papermc.paper.adventure; + ++import com.google.common.base.Suppliers; +import com.mojang.datafixers.util.Either; +import com.mojang.serialization.Codec; +import com.mojang.serialization.DataResult; ++import com.mojang.serialization.DynamicOps; +import com.mojang.serialization.MapCodec; ++import com.mojang.serialization.MapLike; ++import com.mojang.serialization.RecordBuilder; +import com.mojang.serialization.codecs.RecordCodecBuilder; +import java.io.IOException; +import java.util.Collections; +import java.util.List; ++import java.util.Locale; +import java.util.Optional; +import java.util.function.Consumer; +import java.util.function.Function; ++import java.util.function.Supplier; ++import java.util.stream.Stream; +import net.kyori.adventure.key.Key; +import net.kyori.adventure.text.Component; +import net.kyori.adventure.text.KeybindComponent; +import net.kyori.adventure.text.ScoreComponent; ++import net.kyori.adventure.text.SelectorComponent; +import net.kyori.adventure.text.TextComponent; +import net.kyori.adventure.text.TranslatableComponent; +import net.kyori.adventure.text.event.ClickEvent; @@ -40,6 +48,7 @@ index 0000000000000000000000000000000000000000..db13894e2710b933f69f3adaa7e72846 +import net.kyori.adventure.text.format.Style; +import net.kyori.adventure.text.format.TextColor; +import net.kyori.adventure.text.format.TextDecoration; ++import net.kyori.adventure.translation.GlobalTranslator; +import net.minecraft.core.UUIDUtil; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.nbt.CompoundTag; @@ -56,14 +65,16 @@ index 0000000000000000000000000000000000000000..db13894e2710b933f69f3adaa7e72846 +import org.checkerframework.framework.qual.DefaultQualifier; +import org.intellij.lang.annotations.Subst; + -+import static java.util.Objects.requireNonNull; +import static net.kyori.adventure.text.Component.text; +import static net.minecraft.util.ExtraCodecs.strictOptionalField; + +@DefaultQualifier(NonNull.class) +public final class AdventureCodecs { + -+ public static final Codec COMPONENT_CODEC = ExtraCodecs.recursive("adventure Component", AdventureCodecs::createCodec); ++ private static final MapCodec COMPONENT_MAP_CODEC = new RecursiveMapCodec<>("adventure Component", c -> createCodec(c, false)); ++ public static final Codec COMPONENT_CODEC = indirectCodec(COMPONENT_MAP_CODEC.codec()); ++ private static final MapCodec RENDERING_COMPONENT_MAP_CODEC = new RecursiveMapCodec<>("rendering adventure Component", c -> createCodec(c, true)); ++ public static final Codec RENDERING_COMPONENT_CODEC = indirectCodec(RENDERING_COMPONENT_MAP_CODEC.codec()); + + private static final Codec TEXT_COLOR_CODEC = Codec.STRING.comapFlatMap(s -> { + if (s.startsWith("#")) { @@ -96,40 +107,44 @@ index 0000000000000000000000000000000000000000..db13894e2710b933f69f3adaa7e72846 + ).apply(instance, ClickEvent::clickEvent); + }); + -+ private static final Codec SHOW_ENTITY_CODEC = RecordCodecBuilder.create((instance) -> { -+ return instance.group( -+ KEY_CODEC.fieldOf("type").forGetter(HoverEvent.ShowEntity::type), -+ UUIDUtil.LENIENT_CODEC.fieldOf("id").forGetter(HoverEvent.ShowEntity::id), -+ strictOptionalField(COMPONENT_CODEC, "name").forGetter(he -> Optional.ofNullable(he.name())) -+ ).apply(instance, (key, uuid, component) -> { -+ return HoverEvent.ShowEntity.showEntity(key, uuid, component.orElse(null)); ++ private static Codec showEntityCodec(final Codec componentCodec) { ++ return RecordCodecBuilder.create((instance) -> { ++ return instance.group( ++ KEY_CODEC.fieldOf("type").forGetter(HoverEvent.ShowEntity::type), ++ UUIDUtil.LENIENT_CODEC.fieldOf("id").forGetter(HoverEvent.ShowEntity::id), ++ strictOptionalField(componentCodec, "name").forGetter(he -> Optional.ofNullable(he.name())) ++ ).apply(instance, (key, uuid, component) -> { ++ return HoverEvent.ShowEntity.showEntity(key, uuid, component.orElse(null)); ++ }); + }); -+ }); ++ } + -+ private static final Codec SHOW_ITEM_CODEC = net.minecraft.network.chat.HoverEvent.ItemStackInfo.CODEC.xmap(isi -> { -+ @Subst("key") final String typeKey = BuiltInRegistries.ITEM.getKey(isi.item).toString(); -+ return HoverEvent.ShowItem.showItem(Key.key(typeKey), isi.count, PaperAdventure.asBinaryTagHolder(isi.tag.orElse(null))); -+ }, si -> { -+ final Item itemType = BuiltInRegistries.ITEM.get(PaperAdventure.asVanilla(si.item())); -+ final ItemStack stack; -+ try { -+ final @Nullable CompoundTag tag = si.nbt() != null ? si.nbt().get(PaperAdventure.NBT_CODEC) : null; -+ stack = new ItemStack(BuiltInRegistries.ITEM.wrapAsHolder(itemType), si.count(), Optional.ofNullable(tag)); -+ } catch (IOException e) { -+ throw new RuntimeException(e); -+ } -+ return new net.minecraft.network.chat.HoverEvent.ItemStackInfo(stack); -+ }); ++ private static Codec showItemCodec(final Codec componentCodec) { ++ return net.minecraft.network.chat.HoverEvent.ItemStackInfo.CODEC.xmap(isi -> { ++ @Subst("key") final String typeKey = BuiltInRegistries.ITEM.getKey(isi.item).toString(); ++ return HoverEvent.ShowItem.showItem(Key.key(typeKey), isi.count, PaperAdventure.asBinaryTagHolder(isi.tag.orElse(null))); ++ }, si -> { ++ final Item itemType = BuiltInRegistries.ITEM.get(PaperAdventure.asVanilla(si.item())); ++ final ItemStack stack; ++ try { ++ final @Nullable CompoundTag tag = si.nbt() != null ? si.nbt().get(PaperAdventure.NBT_CODEC) : null; ++ stack = new ItemStack(BuiltInRegistries.ITEM.wrapAsHolder(itemType), si.count(), Optional.ofNullable(tag)); ++ } catch (IOException e) { ++ throw new RuntimeException(e); ++ } ++ return new net.minecraft.network.chat.HoverEvent.ItemStackInfo(stack); ++ }); ++ } + + // TODO legacies -+ private static final HoverEventType SHOW_ENTITY_HOVER_EVENT_TYPE = new HoverEventType<>(SHOW_ENTITY_CODEC, HoverEvent.Action.SHOW_ENTITY, "show_entity"); -+ private static final HoverEventType SHOW_ITEM_HOVER_EVENT_TYPE = new HoverEventType<>(SHOW_ITEM_CODEC, HoverEvent.Action.SHOW_ITEM, "show_item"); -+ private static final HoverEventType SHOW_TEXT_HOVER_EVENT_TYPE = new HoverEventType<>(COMPONENT_CODEC, HoverEvent.Action.SHOW_TEXT, "show_text"); ++ private static final HoverEventType SHOW_ENTITY_HOVER_EVENT_TYPE = new HoverEventType<>(AdventureCodecs::showEntityCodec, HoverEvent.Action.SHOW_ENTITY, "show_entity"); ++ private static final HoverEventType SHOW_ITEM_HOVER_EVENT_TYPE = new HoverEventType<>(AdventureCodecs::showItemCodec, HoverEvent.Action.SHOW_ITEM, "show_item"); ++ private static final HoverEventType SHOW_TEXT_HOVER_EVENT_TYPE = new HoverEventType<>(Function.identity(), HoverEvent.Action.SHOW_TEXT, "show_text"); + private static final Codec> HOVER_EVENT_TYPE_CODEC = StringRepresentable.fromValues(() -> new HoverEventType[]{ SHOW_ENTITY_HOVER_EVENT_TYPE, SHOW_ITEM_HOVER_EVENT_TYPE, SHOW_TEXT_HOVER_EVENT_TYPE }); + -+ private record HoverEventType(Codec> codec, String id) implements StringRepresentable { -+ private HoverEventType(final Codec contentCodec, final HoverEvent.Action action, final String id) { -+ this(contentCodec.xmap(v -> { ++ private record HoverEventType(Function, Codec>> codec, String id) implements StringRepresentable { ++ private HoverEventType(final Function, Codec> contentCodec, final HoverEvent.Action action, final String id) { ++ this(cc -> contentCodec.apply(cc).xmap(v -> { + return HoverEvent.hoverEvent(action, v); + }, HoverEvent::value), id); + } @@ -139,30 +154,33 @@ index 0000000000000000000000000000000000000000..db13894e2710b933f69f3adaa7e72846 + } + } + -+ private static final MapCodec> HOVER_EVENT_MAP_CODEC = HOVER_EVENT_TYPE_CODEC.dispatchMap("action", he -> { -+ if (he.action() == HoverEvent.Action.SHOW_ENTITY) { -+ return SHOW_ENTITY_HOVER_EVENT_TYPE; -+ } else if (he.action() == HoverEvent.Action.SHOW_ITEM) { -+ return SHOW_ITEM_HOVER_EVENT_TYPE; -+ } else if (he.action() == HoverEvent.Action.SHOW_TEXT) { -+ return SHOW_TEXT_HOVER_EVENT_TYPE; -+ } else { -+ throw new IllegalStateException(); -+ } -+ }, HoverEventType::codec); ++ private static MapCodec> hoverEventMapCodec(final Codec componentCodec) { ++ return HOVER_EVENT_TYPE_CODEC.dispatchMap("action", he -> { ++ if (he.action() == HoverEvent.Action.SHOW_ENTITY) { ++ return SHOW_ENTITY_HOVER_EVENT_TYPE; ++ } else if (he.action() == HoverEvent.Action.SHOW_ITEM) { ++ return SHOW_ITEM_HOVER_EVENT_TYPE; ++ } else if (he.action() == HoverEvent.Action.SHOW_TEXT) { ++ return SHOW_TEXT_HOVER_EVENT_TYPE; ++ } else { ++ throw new IllegalStateException(); ++ } ++ }, het -> het.codec.apply(componentCodec)); ++ } + -+ public static final MapCodec