Fix Global Loot Modifier deserialization not having registry access. Closes #10042 and #10043

This commit is contained in:
LexManos committed 2024-07-10 11:46:23 -07:00
1 parent e2d1ca7d0e
commit d498ec4a45
4 files changed
+76 -23

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.getRegistryAccess());
event.addListener(INSTANCE);
}
@@ -23,6 +23,7 @@ import net.minecraft.server.packs.resources.SimpleJsonResourceReloadListener;
import net.minecraft.util.profiling.ProfilerFiller;
import net.minecraft.server.packs.resources.ResourceManager;
import net.minecraft.util.GsonHelper;
import net.minecraft.core.RegistryAccess;
import net.minecraft.resources.ResourceLocation;
public class LootModifierManager extends SimpleJsonResourceReloadListener {
@@ -30,10 +31,12 @@ 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 Map<ResourceLocation, IGlobalLootModifier> modifiers = ImmutableMap.of();
public LootModifierManager() {
public LootModifierManager(RegistryAccess registries) {
super(GSON, FOLDER);
this.registries = registries;
}
@Override
@@ -68,12 +71,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 = registries.createSerializationContext(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();
}