Add updated tag context to AddReloadListenerEvent (#10421)

Fixes using custom tags in GlobalLootModifiers.
This commit is contained in:
Jonathing authored and GitHub committed 2025-02-17 18:28:07 -08:00
1 parent 774048154f
commit 784693b7e5
7 files changed
+51 -12

No files matched your search

@@ -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()
@@ -101,7 +101,7 @@ public class ForgeInternalHandler {
@SubscribeEvent
public void onResourceReload(AddReloadListenerEvent event) {
INSTANCE = new LootModifierManager(event.getRegistryAccess());
INSTANCE = new LootModifierManager(event.getRegistries());
event.addListener(INSTANCE);
}
@@ -9,6 +9,7 @@ import java.io.IOException;
import java.util.*;
import com.mojang.serialization.JsonOps;
import net.minecraft.core.HolderLookup;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
@@ -31,10 +32,16 @@ public class LootModifierManager extends SimpleJsonResourceReloadListener {
private static final Gson GSON = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
private static final String FOLDER = "loot_modifiers";
private final RegistryAccess registries;
private final HolderLookup.Provider registries;
private Map<ResourceLocation, IGlobalLootModifier> modifiers = ImmutableMap.of();
/** @deprecated Use {@link #LootModifierManager(HolderLookup.Provider)} */
@Deprecated(forRemoval = true, since = "1.21.1")
public LootModifierManager(RegistryAccess registries) {
this((HolderLookup.Provider) registries);
}
public LootModifierManager(HolderLookup.Provider registries) {
super(GSON, FOLDER);
this.registries = registries;
}
@@ -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<PreparableReloadListener> 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,26 @@ 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<Void> reload(final PreparationBarrier stage, final ResourceManager resourceManager, final ProfilerFiller preparationsProfiler, final ProfilerFiller reloadProfiler, final Executor backgroundExecutor, final Executor gameExecutor) {
if (ModLoader.isLoadingStateValid())
@@ -86,5 +105,10 @@ public class AddReloadListenerEvent extends Event {
else
return CompletableFuture.completedFuture(null);
}
@Override
public String getName() {
return wrapped.getName();
}
}
}
@@ -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;
@@ -722,10 +723,15 @@ public final class ForgeEventFactory {
return fire(new SleepFinishedTimeEvent(level, newTime, minTime)).getNewTime();
}
@Deprecated(forRemoval = true, since = "1.21.4")
public static List<PreparableReloadListener> onResourceReload(ReloadableServerResources serverResources, RegistryAccess registryAccess) {
return fire(new AddReloadListenerEvent(serverResources, registryAccess)).getListeners();
}
public static List<PreparableReloadListener> 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<CommandSourceStack> dispatcher, Commands.CommandSelection environment, CommandBuildContext context) {
post(new RegisterCommandsEvent(dispatcher, environment, context));
}
@@ -4,7 +4,7 @@
{
"condition": "minecraft:match_tool",
"predicate": {
"items": "minecraft:shears"
"items": "#c:tools/shear"
}
},
{
@@ -53,6 +53,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.DatapackBuiltinEntriesProvider;
import net.minecraftforge.common.data.GlobalLootModifierProvider;
@@ -231,7 +232,8 @@ public class GlobalLootModifiersTest extends BaseTestMod {
// This has no game test because it relies on random number generation and I can't be bothered to try and force that right now.
add("wheat_harvest", new WheatSeedsConverterModifier(
new LootItemCondition[] {
MatchTool.toolMatches(ItemPredicate.Builder.item().of(Items.SHEARS)).build(),
// the #c:tools_shear tag is used here to test parsing of tags in LootModiferManager
MatchTool.toolMatches(ItemPredicate.Builder.item().of(Tags.Items.TOOLS_SHEAR)).build(),
LootItemBlockStatePropertyCondition.hasBlockStateProperties(Blocks.WHEAT).build()
},
3, Items.WHEAT_SEEDS, Items.WHEAT)