diff --git a/patches/minecraft/net/minecraft/resources/HolderSetCodec.java.patch b/patches/minecraft/net/minecraft/resources/HolderSetCodec.java.patch index c459573e61..aa7b8bb364 100644 --- a/patches/minecraft/net/minecraft/resources/HolderSetCodec.java.patch +++ b/patches/minecraft/net/minecraft/resources/HolderSetCodec.java.patch @@ -39,6 +39,15 @@ ); return dataresult.map(p_326149_ -> Pair.of((HolderSet)p_326149_, (T)p_326147_.getSecond())); } +@@ -90,7 +_,7 @@ + } + + private DataResult, T>> decodeWithoutRegistry(DynamicOps p_206671_, T p_206672_) { +- return this.elementCodec.listOf().decode(p_206671_, p_206672_).flatMap(p_206666_ -> { ++ return this.homogenousListCodec.decode(p_206671_, p_206672_).flatMap(p_206666_ -> { // Forge: Match encodeWithoutRegistry's use of the homogenousListCodec + List> list = new ArrayList<>(); + + for (Holder holder : p_206666_.getFirst()) { @@ -106,6 +_,9 @@ } diff --git a/patches/minecraft/net/minecraft/server/ReloadableServerResources.java.patch b/patches/minecraft/net/minecraft/server/ReloadableServerResources.java.patch index ce7f05cf3a..0964ca11a3 100644 --- a/patches/minecraft/net/minecraft/server/ReloadableServerResources.java.patch +++ b/patches/minecraft/net/minecraft/server/ReloadableServerResources.java.patch @@ -26,7 +26,7 @@ p_326196_ -> { ReloadableServerResources reloadableserverresources = new ReloadableServerResources(p_326196_.compositeAccess(), p_250212_, p_249301_, p_251126_); + var listeners = new java.util.ArrayList<>(reloadableserverresources.listeners()); -+ listeners.addAll(net.minecraftforge.event.ForgeEventFactory.onResourceReload(reloadableserverresources, p_330376_.compositeAccess())); ++ listeners.addAll(net.minecraftforge.event.ForgeEventFactory.onResourceReload(reloadableserverresources, reloadableserverresources.registryLookup, p_330376_.compositeAccess())); return SimpleReloadInstance.create( - p_248588_, reloadableserverresources.listeners(), p_249136_, p_249601_, DATA_RELOAD_INITIAL_TASK, LOGGER.isDebugEnabled() + p_248588_, listeners, p_249136_, p_249601_, DATA_RELOAD_INITIAL_TASK, LOGGER.isDebugEnabled() diff --git a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java index 89c22a0895..399c49bab8 100644 --- a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java @@ -101,7 +101,7 @@ public class ForgeInternalHandler { @SubscribeEvent public void onResourceReload(AddReloadListenerEvent event) { - INSTANCE = new LootModifierManager(); + INSTANCE = new LootModifierManager(event.getRegistries()); event.addListener(INSTANCE); } diff --git a/src/main/java/net/minecraftforge/common/loot/LootModifierManager.java b/src/main/java/net/minecraftforge/common/loot/LootModifierManager.java index edef6697e1..55a6f0bd91 100644 --- a/src/main/java/net/minecraftforge/common/loot/LootModifierManager.java +++ b/src/main/java/net/minecraftforge/common/loot/LootModifierManager.java @@ -8,7 +8,9 @@ package net.minecraftforge.common.loot; import java.io.IOException; import java.util.*; +import com.mojang.serialization.DynamicOps; import com.mojang.serialization.JsonOps; +import net.minecraft.core.HolderLookup; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @@ -30,10 +32,17 @@ public class LootModifierManager extends SimpleJsonResourceReloadListener { private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create(); private static final String FOLDER = "loot_modifiers"; + private final HolderLookup.Provider registries; private Map modifiers = ImmutableMap.of(); + @Deprecated(forRemoval = true, since = "1.20.6") public LootModifierManager() { + this(null); + } + + public LootModifierManager(HolderLookup.Provider registries) { super(GSON, FOLDER); + this.registries = registries; } @Override @@ -68,12 +77,13 @@ public class LootModifierManager extends SimpleJsonResourceReloadListener { @Override protected void apply(Map resources, ResourceManager resourceManagerIn, ProfilerFiller profilerIn) { Builder builder = ImmutableMap.builder(); + var ops = this.registries != null ? registries.createSerializationContext(JsonOps.INSTANCE) : JsonOps.INSTANCE; resources.forEach((location, json) -> { - IGlobalLootModifier.DIRECT_CODEC.parse(JsonOps.INSTANCE, json) + IGlobalLootModifier.DIRECT_CODEC.parse(ops, json) // log error if parse fails - .resultOrPartial(errorMsg -> LOGGER.warn("Could not decode GlobalLootModifier with json id {} - error: {}", location, errorMsg)) + .ifError(error -> LOGGER.warn("Could not decode GlobalLootModifier with json id {} - error: {}", location, error.message())) // add loot modifier if parse succeeds - .ifPresent(modifier -> builder.put(location, modifier)); + .ifSuccess(modifier -> builder.put(location, modifier)); }); this.modifiers = builder.build(); } diff --git a/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java b/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java index d394fed423..f7877ac755 100644 --- a/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java +++ b/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java @@ -6,6 +6,7 @@ package net.minecraftforge.event; import com.google.common.collect.ImmutableList; +import net.minecraft.core.HolderLookup; import net.minecraft.core.RegistryAccess; import net.minecraft.server.ReloadableServerResources; import net.minecraft.util.profiling.ProfilerFiller; @@ -30,10 +31,24 @@ import java.util.concurrent.Executor; public class AddReloadListenerEvent extends Event { private final List listeners = new ArrayList<>(); private final ReloadableServerResources serverResources; + + private final HolderLookup.Provider registries; + @Deprecated(forRemoval = true, since = "1.21.4") private final RegistryAccess registryAccess; + /** @deprecated Does not provide additional context. Use {@link #AddReloadListenerEvent(ReloadableServerResources, HolderLookup.Provider, RegistryAccess)} instead. */ + @Deprecated(forRemoval = true, since = "1.21.4") public AddReloadListenerEvent(ReloadableServerResources serverResources, RegistryAccess registryAccess) { + this(serverResources, registryAccess, registryAccess); + } + + public AddReloadListenerEvent( + ReloadableServerResources serverResources, + HolderLookup.Provider registries, + @Deprecated(forRemoval = true, since = "1.21.4") RegistryAccess registryAccess + ) { this.serverResources = serverResources; + this.registries = registries; this.registryAccess = registryAccess; } @@ -63,22 +78,27 @@ public class AddReloadListenerEvent extends Event { return serverResources.getConditionContext(); } + /** + * @return A holder lookup provider containing the registries with updated tags. + * + * @see net.minecraft.server.ReloadableServerResources#registryLookup + */ + public HolderLookup.Provider getRegistries() { + return registries; + } + /** * Provides access to the loaded registries associated with these server resources. * All built-in and dynamic registries are loaded and frozen by this point. * @return The RegistryAccess context for the currently active reload. + * @deprecated Does not contain updated tags. Use {@link #getRegistries()} instead. */ + @Deprecated(forRemoval = true, since = "1.21.4") public RegistryAccess getRegistryAccess() { return registryAccess; } - private static class WrappedStateAwareListener implements PreparableReloadListener { - private final PreparableReloadListener wrapped; - - private WrappedStateAwareListener(final PreparableReloadListener wrapped) { - this.wrapped = wrapped; - } - + private record WrappedStateAwareListener(PreparableReloadListener wrapped) implements PreparableReloadListener { @Override public CompletableFuture reload(final PreparationBarrier stage, final ResourceManager resourceManager, final ProfilerFiller preparationsProfiler, final ProfilerFiller reloadProfiler, final Executor backgroundExecutor, final Executor gameExecutor) { if (ModLoader.isLoadingStateValid()) @@ -86,5 +106,10 @@ public class AddReloadListenerEvent extends Event { else return CompletableFuture.completedFuture(null); } + + @Override + public String getName() { + return wrapped.getName(); + } } } diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 3f0106a630..def4ae85e6 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -14,6 +14,7 @@ import java.util.Optional; import java.util.function.BooleanSupplier; import java.util.function.Consumer; +import net.minecraft.core.HolderLookup; import net.minecraft.core.component.DataComponentMap; import net.minecraft.world.item.Item; import net.minecraftforge.common.MinecraftForge; @@ -721,10 +722,15 @@ public final class ForgeEventFactory { return fire(new SleepFinishedTimeEvent(level, newTime, minTime)).getNewTime(); } + @Deprecated(forRemoval = true, since = "1.21.4") public static List onResourceReload(ReloadableServerResources serverResources, RegistryAccess registryAccess) { return fire(new AddReloadListenerEvent(serverResources, registryAccess)).getListeners(); } + public static List onResourceReload(ReloadableServerResources serverResources, HolderLookup.Provider registries, @Deprecated(forRemoval = true, since = "1.21.4") RegistryAccess registryAccess) { + return fire(new AddReloadListenerEvent(serverResources, registries, registryAccess)).getListeners(); + } + public static void onCommandRegister(CommandDispatcher dispatcher, Commands.CommandSelection environment, CommandBuildContext context) { post(new RegisterCommandsEvent(dispatcher, environment, context)); } diff --git a/src/test/generated/global_loot_test/data/global_loot_test/loot_modifiers/wheat_harvest.json b/src/test/generated/global_loot_test/data/global_loot_test/loot_modifiers/wheat_harvest.json index 90e8896d15..9055026a34 100644 --- a/src/test/generated/global_loot_test/data/global_loot_test/loot_modifiers/wheat_harvest.json +++ b/src/test/generated/global_loot_test/data/global_loot_test/loot_modifiers/wheat_harvest.json @@ -4,7 +4,7 @@ { "condition": "minecraft:match_tool", "predicate": { - "items": "minecraft:shears" + "items": "#forge:shears" } }, { diff --git a/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java b/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java index 4ebceeb348..97de5500c8 100644 --- a/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java +++ b/src/test/java/net/minecraftforge/debug/gameplay/loot/GlobalLootModifiersTest.java @@ -47,6 +47,7 @@ import net.minecraft.world.level.storage.loot.parameters.LootContextParams; import net.minecraft.world.level.storage.loot.predicates.LootItemBlockStatePropertyCondition; import net.minecraft.world.level.storage.loot.predicates.LootItemCondition; import net.minecraft.world.level.storage.loot.predicates.MatchTool; +import net.minecraftforge.common.Tags; import net.minecraftforge.common.crafting.conditions.IConditionBuilder; import net.minecraftforge.common.data.GlobalLootModifierProvider; import net.minecraftforge.common.loot.IGlobalLootModifier; @@ -165,7 +166,8 @@ public class GlobalLootModifiersTest extends BaseTestMod { add("wheat_harvest", new WheatSeedsConverterModifier( new LootItemCondition[] { - MatchTool.toolMatches(ItemPredicate.Builder.item().of(Items.SHEARS)).build(), + // the #forge:shears tag is used here to test parsing of tags in LootModiferManager + MatchTool.toolMatches(ItemPredicate.Builder.item().of(Tags.Items.SHEARS)).build(), LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.WHEAT).build() }, 3, Items.WHEAT_SEEDS, Items.WHEAT)