mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-21 12:42:26 +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 <[email protected]>
This commit is contained in:
1 parent
60904744b9
commit
208adc1109
8 files changed
+66
-14
No files matched your search
@@ -39,6 +39,15 @@
|
||||
);
|
||||
return dataresult.map(p_326149_ -> Pair.of((HolderSet<E>)p_326149_, (T)p_326147_.getSecond()));
|
||||
}
|
||||
@@ -90,7 +_,7 @@
|
||||
}
|
||||
|
||||
private <T> DataResult<Pair<HolderSet<E>, T>> decodeWithoutRegistry(DynamicOps<T> 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<Holder.Direct<E>> list = new ArrayList<>();
|
||||
|
||||
for (Holder<E> holder : p_206666_.getFirst()) {
|
||||
@@ -106,6 +_,9 @@
|
||||
}
|
||||
|
||||
|
||||
@@ -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();
|
||||
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));
|
||||
}
|
||||
|
||||
+1
-1
@@ -4,7 +4,7 @@
|
||||
{
|
||||
"condition": "minecraft:match_tool",
|
||||
"predicate": {
|
||||
"items": "minecraft:shears"
|
||||
"items": "#forge:shears"
|
||||
}
|
||||
},
|
||||
{
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user