From 1bee47ce6496de03a09ba33cb69b4bc2768e9b1a Mon Sep 17 00:00:00 2001 From: MelanX Date: Tue, 25 Feb 2025 20:07:00 +0100 Subject: [PATCH 1/2] Fix error in encoding data components in hand command --- .../libx/impl/command/common/HandCommand.java | 2 +- .../libx/util/game/ComponentUtil.java | 22 +++++++++---------- 2 files changed, 11 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java b/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java index 02305883..b023cd44 100644 --- a/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java +++ b/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java @@ -48,7 +48,7 @@ public int run(CommandContext ctx) throws CommandSyntaxExcep } if (components.size() != 0) { - Component componentsText = ComponentUtil.toPrettyComponent(Registries.DATA_COMPONENT_TYPE, components); + Component componentsText = ComponentUtil.toPrettyComponent(ctx.getSource().getServer().registryAccess(), components); componentsText = ComponentUtil.withCopyAction(componentsText, componentsText.getString()); message = message.append(Component.literal(" ")).append(componentsText); } diff --git a/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java b/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java index 3fe135aa..37946157 100644 --- a/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java +++ b/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java @@ -7,7 +7,7 @@ import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; import net.minecraft.ChatFormatting; -import net.minecraft.core.Registry; +import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentType; @@ -17,7 +17,7 @@ import net.minecraft.nbt.NbtUtils; import net.minecraft.nbt.Tag; import net.minecraft.network.chat.*; -import net.minecraft.resources.ResourceKey; +import net.minecraft.resources.RegistryOps; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.FormattedCharSequence; @@ -144,26 +144,24 @@ public static Component toPrettyComponent(JsonElement json) { /** * Turns a {@link DataComponentMap} into a {@link Component} with syntax highlighting that can be used for display. */ - public static Component toPrettyComponent(ResourceKey>> registry, DataComponentMap components) { - return toPrettyComponent(registry, components.stream().collect(Collectors.toUnmodifiableMap(TypedDataComponent::type, tc -> Optional.of(tc.value())))); + public static Component toPrettyComponent(HolderLookup.Provider lookupProvider, DataComponentMap components) { + return toPrettyComponent(lookupProvider, components.stream().collect(Collectors.toUnmodifiableMap(TypedDataComponent::type, tc -> Optional.of(tc.value())))); } /** * Turns a {@link DataComponentPatch} into a {@link Component} with syntax highlighting that can be used for display. */ - public static Component toPrettyComponent(ResourceKey>> registry, DataComponentPatch patch) { - return toPrettyComponent(registry, patch.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue))); + public static Component toPrettyComponent(HolderLookup.Provider lookupProvider, DataComponentPatch patch) { + return toPrettyComponent(lookupProvider, patch.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue))); } - private static Component toPrettyComponent(ResourceKey>> registry, Map, Optional> map) { - //noinspection unchecked - Registry> theRegistry = (Registry>) BuiltInRegistries.REGISTRY.get(registry.location()); - if (theRegistry == null) throw new IllegalStateException("Registry not found in builtin registries: " + registry); + private static Component toPrettyComponent(HolderLookup.Provider lookupProvider, Map, Optional> map) { + RegistryOps registryOps = RegistryOps.create(NbtOps.INSTANCE, lookupProvider); Map> typeMap = new HashMap<>(); for (DataComponentType type : map.keySet()) { if (type.isTransient()) continue; - ResourceLocation id = theRegistry.getKey(type); + ResourceLocation id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(type); if (id == null) throw new IllegalStateException("Unregistered data component type: " + type); typeMap.put(id, type); } @@ -184,7 +182,7 @@ private static Component toPrettyComponent(ResourceKey result = ((Codec) type.codecOrThrow()).encode(maybeValue.get(), NbtOps.INSTANCE, NbtOps.INSTANCE.empty()); + DataResult result = ((Codec) type.codecOrThrow()).encodeStart(registryOps, maybeValue.get()); if (result instanceof DataResult.Error) { return Component.literal("encoder error for " + typeId).withStyle(ChatFormatting.RED); } From 607410db317b93a027cd64f8088c5c8a6c8ddf16 Mon Sep 17 00:00:00 2001 From: noeppi_noeppi Date: Wed, 26 Feb 2025 23:49:15 +0100 Subject: [PATCH 2/2] Make the data component toPrettyComponent work with different data component type registries again. --- .../libx/impl/command/common/HandCommand.java | 2 +- .../libx/util/game/ComponentUtil.java | 35 +++++++++++++------ 2 files changed, 25 insertions(+), 12 deletions(-) diff --git a/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java b/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java index b023cd44..b1cf56bc 100644 --- a/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java +++ b/src/main/java/org/moddingx/libx/impl/command/common/HandCommand.java @@ -48,7 +48,7 @@ public int run(CommandContext ctx) throws CommandSyntaxExcep } if (components.size() != 0) { - Component componentsText = ComponentUtil.toPrettyComponent(ctx.getSource().getServer().registryAccess(), components); + Component componentsText = ComponentUtil.toPrettyComponent(Registries.DATA_COMPONENT_TYPE, ctx.getSource().getServer().registryAccess(), components); componentsText = ComponentUtil.withCopyAction(componentsText, componentsText.getString()); message = message.append(Component.literal(" ")).append(componentsText); } diff --git a/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java b/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java index 37946157..95420c76 100644 --- a/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java +++ b/src/main/java/org/moddingx/libx/util/game/ComponentUtil.java @@ -6,8 +6,10 @@ import com.google.gson.JsonPrimitive; import com.mojang.serialization.Codec; import com.mojang.serialization.DataResult; +import com.mojang.serialization.DynamicOps; import net.minecraft.ChatFormatting; -import net.minecraft.core.HolderLookup; +import net.minecraft.core.Registry; +import net.minecraft.core.RegistryAccess; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentType; @@ -18,6 +20,7 @@ import net.minecraft.nbt.Tag; import net.minecraft.network.chat.*; import net.minecraft.resources.RegistryOps; +import net.minecraft.resources.ResourceKey; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.FormattedCharSequence; @@ -143,25 +146,37 @@ public static Component toPrettyComponent(JsonElement json) { /** * Turns a {@link DataComponentMap} into a {@link Component} with syntax highlighting that can be used for display. + * + * @param registry The registry in which the data component types for the {@link DataComponentMap data component map} are registered. + * @param registryAccess A {@link RegistryAccess registry access} to provide access to datapack registries when encoding data component values. */ - public static Component toPrettyComponent(HolderLookup.Provider lookupProvider, DataComponentMap components) { - return toPrettyComponent(lookupProvider, components.stream().collect(Collectors.toUnmodifiableMap(TypedDataComponent::type, tc -> Optional.of(tc.value())))); + public static Component toPrettyComponent(ResourceKey>> registry, RegistryAccess registryAccess, DataComponentMap components) { + return toPrettyComponent(registry, registryAccess, components.stream().collect(Collectors.toUnmodifiableMap(TypedDataComponent::type, tc -> Optional.of(tc.value())))); } /** * Turns a {@link DataComponentPatch} into a {@link Component} with syntax highlighting that can be used for display. + * + * @param registry The registry in which the data component types for the {@link DataComponentPatch data component patch} are registered. + * @param registryAccess A {@link RegistryAccess registry access} to provide access to datapack registries when encoding data component values. */ - public static Component toPrettyComponent(HolderLookup.Provider lookupProvider, DataComponentPatch patch) { - return toPrettyComponent(lookupProvider, patch.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue))); + public static Component toPrettyComponent(ResourceKey>> registry, RegistryAccess registryAccess, DataComponentPatch patch) { + return toPrettyComponent(registry, registryAccess, patch.entrySet().stream().collect(Collectors.toUnmodifiableMap(Map.Entry::getKey, Map.Entry::getValue))); } - private static Component toPrettyComponent(HolderLookup.Provider lookupProvider, Map, Optional> map) { - RegistryOps registryOps = RegistryOps.create(NbtOps.INSTANCE, lookupProvider); + private static Component toPrettyComponent(ResourceKey>> registry, RegistryAccess registryAccess, Map, Optional> map) { + Registry> theRegistry = registryAccess.registry(registry).orElse(null); + if (theRegistry == null) { + //noinspection unchecked + theRegistry = (Registry>) BuiltInRegistries.REGISTRY.get(registry.location()); + } + if (theRegistry == null) throw new IllegalStateException("Registry not found: " + registry); + DynamicOps registryOps = RegistryOps.create(NbtOps.INSTANCE, registryAccess); Map> typeMap = new HashMap<>(); for (DataComponentType type : map.keySet()) { if (type.isTransient()) continue; - ResourceLocation id = BuiltInRegistries.DATA_COMPONENT_TYPE.getKey(type); + ResourceLocation id = theRegistry.getKey(type); if (id == null) throw new IllegalStateException("Unregistered data component type: " + type); typeMap.put(id, type); } @@ -169,9 +184,7 @@ private static Component toPrettyComponent(HolderLookup.Provider lookupProvider, MutableComponent cmp = Component.literal("["); boolean first = true; for (ResourceLocation typeId : typeMap.keySet().stream().sorted().toList()) { - if (!first) { - cmp = cmp.append(", "); - } + if (!first) cmp = cmp.append(", "); first = false; DataComponentType type = typeMap.get(typeId); Optional maybeValue = map.get(type);