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..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(Registries.DATA_COMPONENT_TYPE, 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 3fe135aa..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.Registry; +import net.minecraft.core.RegistryAccess; import net.minecraft.core.component.DataComponentMap; import net.minecraft.core.component.DataComponentPatch; import net.minecraft.core.component.DataComponentType; @@ -17,6 +19,7 @@ import net.minecraft.nbt.NbtUtils; 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,22 +146,32 @@ 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(ResourceKey>> registry, DataComponentMap components) { - return toPrettyComponent(registry, 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(ResourceKey>> registry, DataComponentPatch patch) { - return toPrettyComponent(registry, 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(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(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()) { @@ -171,9 +184,7 @@ private static Component toPrettyComponent(ResourceKey type = typeMap.get(typeId); Optional maybeValue = map.get(type); @@ -184,7 +195,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); }