mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-13 16:54:22 +00:00
Add updated tag context to AddReloadListenerEvent (#10440)
Fixes using custom tags in GlobalLootModifiers. Fix Global Loot Modifier deserialization not having registry access --------- Co-authored-by: LexManos <LexManos@gmail.com>
This commit is contained in:
1 parent
60904744b9
commit
208adc1109
8 files changed
+66
-14
No files matched your search
@@ -101,7 +101,7 @@ public class ForgeInternalHandler {
|
||||
|
||||
@SubscribeEvent
|
||||
public void onResourceReload(AddReloadListenerEvent event) {
|
||||
INSTANCE = new LootModifierManager();
|
||||
INSTANCE = new LootModifierManager(event.getRegistries());
|
||||
event.addListener(INSTANCE);
|
||||
}
|
||||
|
||||
|
||||
@@ -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<ResourceLocation, IGlobalLootModifier> 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<ResourceLocation, JsonElement> resources, ResourceManager resourceManagerIn, ProfilerFiller profilerIn) {
|
||||
Builder<ResourceLocation, IGlobalLootModifier> 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();
|
||||
}
|
||||
|
||||
@@ -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,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<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 +106,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;
|
||||
@@ -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<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));
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user