From d61de63482f780de40cd3c337fd8e4a874da6b59 Mon Sep 17 00:00:00 2001 From: LexManos Date: Sat, 3 Jan 2026 12:24:53 -0800 Subject: [PATCH] Allow accessing Registry context from TagValueOutput --- .../level/block/entity/BlockEntity.java.patch | 2 +- .../level/storage/TagValueOutput.java.patch | 25 +++++++++++++++++++ .../capabilities/CapabilityProvider.java | 12 +++++++++ 3 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 patches/minecraft/net/minecraft/world/level/storage/TagValueOutput.java.patch diff --git a/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntity.java.patch b/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntity.java.patch index 23b5bde849..f199babc96 100644 --- a/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntity.java.patch +++ b/patches/minecraft/net/minecraft/world/level/block/entity/BlockEntity.java.patch @@ -43,7 +43,7 @@ } protected void saveAdditional(ValueOutput p_407573_) { -+ if (getCapabilities() != null && this.level != null) p_407573_.storeNullable("ForgeCaps", CompoundTag.CODEC, serializeCaps(this.level.registryAccess())); ++ if (getCapabilities() != null) p_407573_.storeNullable("ForgeCaps", CompoundTag.CODEC, serializeCaps(p_407573_)); } public final CompoundTag saveWithFullMetadata(HolderLookup.Provider p_331193_) { diff --git a/patches/minecraft/net/minecraft/world/level/storage/TagValueOutput.java.patch b/patches/minecraft/net/minecraft/world/level/storage/TagValueOutput.java.patch new file mode 100644 index 0000000000..2f00de52bf --- /dev/null +++ b/patches/minecraft/net/minecraft/world/level/storage/TagValueOutput.java.patch @@ -0,0 +1,25 @@ +--- a/net/minecraft/world/level/storage/TagValueOutput.java ++++ b/net/minecraft/world/level/storage/TagValueOutput.java +@@ -17,15 +_,21 @@ + private final ProblemReporter problemReporter; + private final DynamicOps ops; + private final CompoundTag output; ++ public final HolderLookup.@org.jspecify.annotations.Nullable Provider lookup; + + TagValueOutput(ProblemReporter p_407091_, DynamicOps p_407083_, CompoundTag p_409593_) { ++ this(p_407091_, p_407083_, p_409593_, null); ++ } ++ ++ TagValueOutput(ProblemReporter p_407091_, DynamicOps p_407083_, CompoundTag p_409593_, HolderLookup.@org.jspecify.annotations.Nullable Provider lookup) { + this.problemReporter = p_407091_; + this.ops = p_407083_; + this.output = p_409593_; ++ this.lookup = lookup; + } + + public static TagValueOutput createWithContext(ProblemReporter p_409887_, HolderLookup.Provider p_409738_) { +- return new TagValueOutput(p_409887_, p_409738_.createSerializationContext(NbtOps.INSTANCE), new CompoundTag()); ++ return new TagValueOutput(p_409887_, p_409738_.createSerializationContext(NbtOps.INSTANCE), new CompoundTag(), p_409738_); + } + + public static TagValueOutput createWithoutContext(ProblemReporter p_405875_) { diff --git a/src/main/java/net/minecraftforge/common/capabilities/CapabilityProvider.java b/src/main/java/net/minecraftforge/common/capabilities/CapabilityProvider.java index 50a886bf2d..c6d6263720 100644 --- a/src/main/java/net/minecraftforge/common/capabilities/CapabilityProvider.java +++ b/src/main/java/net/minecraftforge/common/capabilities/CapabilityProvider.java @@ -13,6 +13,8 @@ import net.minecraft.world.item.ItemStack; import net.minecraft.world.level.Level; import net.minecraft.world.level.block.entity.BlockEntity; import net.minecraft.world.level.chunk.LevelChunk; +import net.minecraft.world.level.storage.TagValueOutput; +import net.minecraft.world.level.storage.ValueOutput; import net.minecraftforge.common.util.LazyOptional; import net.minecraftforge.event.AttachCapabilitiesEvent; import net.minecraftforge.event.ForgeEventFactory; @@ -176,6 +178,16 @@ public abstract class CapabilityProvider> i return capabilities; } + + protected final @Nullable CompoundTag serializeCaps(ValueOutput output) { + if (isLazy && !initialized) + return lazyData; + var disp = getCapabilities(); + if (disp == null || !(output instanceof TagValueOutput tagOutput) || tagOutput.lookup == null) + return null; + return disp.serializeNBT(tagOutput.lookup); + } + protected final @Nullable CompoundTag serializeCaps(HolderLookup.Provider registryAccess) { if (isLazy && !initialized) return lazyData;