From ff86201523a1f5a2af4559911b3de51358743f82 Mon Sep 17 00:00:00 2001 From: Cyborgmas Date: Mon, 6 Jul 2020 15:41:57 -0400 Subject: [PATCH] New AddReloadListenerEvent that gathers server side data reload handlers. (#6849) --- .../resources/DataPackRegistries.java.patch | 22 ++---------- .../net/minecraftforge/common/ForgeHooks.java | 6 ++-- .../common/ForgeInternalHandler.java | 18 ++++++++++ .../event/AddReloadListenerEvent.java | 34 +++++++++++++++++++ .../event/ForgeEventFactory.java | 8 +++++ 5 files changed, 65 insertions(+), 23 deletions(-) create mode 100644 src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java diff --git a/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch b/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch index 2f3311f00..5b5f5ba78 100644 --- a/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch +++ b/patches/minecraft/net/minecraft/resources/DataPackRegistries.java.patch @@ -1,28 +1,10 @@ --- a/net/minecraft/resources/DataPackRegistries.java +++ b/net/minecraft/resources/DataPackRegistries.java -@@ -22,6 +22,8 @@ - private final AdvancementManager field_240958_h_ = new AdvancementManager(this.field_240956_f_); - private final FunctionReloader field_240959_i_; - -+ private final net.minecraftforge.common.loot.LootModifierManager lootManager = new net.minecraftforge.common.loot.LootModifierManager(); -+ - public DataPackRegistries(Commands.EnvironmentType p_i232598_1_, int p_i232598_2_) { - this.field_240953_c_ = new Commands(p_i232598_1_); - this.field_240959_i_ = new FunctionReloader(p_i232598_2_, this.field_240953_c_.func_197054_a()); -@@ -31,6 +33,7 @@ +@@ -31,6 +31,7 @@ this.field_240952_b_.func_219534_a(this.field_240957_g_); this.field_240952_b_.func_219534_a(this.field_240959_i_); this.field_240952_b_.func_219534_a(this.field_240958_h_); -+ this.field_240952_b_.func_219534_a(this.lootManager); ++ net.minecraftforge.event.ForgeEventFactory.onResourceReload().forEach(field_240952_b_::func_219534_a); } public FunctionReloader func_240960_a_() { -@@ -85,4 +88,8 @@ - public void close() { - this.field_240952_b_.close(); - } -+ -+ public net.minecraftforge.common.loot.LootModifierManager getLootModifierManager() { -+ return lootManager; -+ } - } diff --git a/src/main/java/net/minecraftforge/common/ForgeHooks.java b/src/main/java/net/minecraftforge/common/ForgeHooks.java index 9329f4fc0..31c18332c 100644 --- a/src/main/java/net/minecraftforge/common/ForgeHooks.java +++ b/src/main/java/net/minecraftforge/common/ForgeHooks.java @@ -109,6 +109,8 @@ import net.minecraft.world.IWorld; import net.minecraft.world.IWorldReader; import net.minecraft.world.World; import net.minecraftforge.common.data.IOptionalTagEntry; +import net.minecraftforge.common.loot.IGlobalLootModifier; +import net.minecraftforge.common.loot.LootModifierManager; import net.minecraftforge.common.util.BlockSnapshot; import net.minecraftforge.event.AnvilUpdateEvent; import net.minecraftforge.event.DifficultyChangeEvent; @@ -1165,12 +1167,10 @@ public class ForgeHooks * @return The modified list */ public static List modifyLoot(List list, LootContext context) { - /* - LootModifierManager man = context.getWorld().getServer().getLootModifierManager(); + LootModifierManager man = ForgeInternalHandler.getLootModifierManager(); for(IGlobalLootModifier mod : man.getAllLootMods()) { list = mod.apply(list, context); } - */ return list; } diff --git a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java index b7dceb1f6..edfa54af6 100644 --- a/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java +++ b/src/main/java/net/minecraftforge/common/ForgeInternalHandler.java @@ -25,7 +25,9 @@ import net.minecraft.entity.player.ServerPlayerEntity; import net.minecraft.item.Item; import net.minecraft.item.ItemStack; import net.minecraft.world.server.ServerWorld; +import net.minecraftforge.common.loot.LootModifierManager; import net.minecraftforge.common.util.FakePlayerFactory; +import net.minecraftforge.event.AddReloadListenerEvent; import net.minecraftforge.event.entity.EntityJoinWorldEvent; import net.minecraftforge.event.entity.player.PlayerEvent; import net.minecraftforge.event.world.ChunkEvent; @@ -109,5 +111,21 @@ public class ForgeInternalHandler { ForgeHooks.updateBurns(); } + + private static LootModifierManager INSTANCE; + + @SubscribeEvent + public void onResourceReload(AddReloadListenerEvent event) + { + INSTANCE = new LootModifierManager(); + event.addListener(INSTANCE); + } + + static LootModifierManager getLootModifierManager() + { + if(INSTANCE == null) + throw new IllegalStateException("Can not retrieve LootModifierManager until resources have loaded once."); + return INSTANCE; + } } diff --git a/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java b/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java new file mode 100644 index 000000000..c6941f4e6 --- /dev/null +++ b/src/main/java/net/minecraftforge/event/AddReloadListenerEvent.java @@ -0,0 +1,34 @@ +package net.minecraftforge.event; + +import com.google.common.collect.ImmutableList; +import net.minecraft.resources.DataPackRegistries; +import net.minecraft.resources.IFutureReloadListener; +import net.minecraftforge.common.MinecraftForge; +import net.minecraftforge.eventbus.api.Event; + +import java.util.ArrayList; +import java.util.List; + +/** + * The main ResourceManager is recreated on each reload, through {@link DataPackRegistries}'s creation. + * + * The event is fired on each reload and lets modders add their own ReloadListeners, for server-side resources. + * The event is fired on the {@link MinecraftForge#EVENT_BUS} + */ +public class AddReloadListenerEvent extends Event +{ + private final List listeners = new ArrayList<>(); + + /** + * @param listener the listener to add to the ResourceManager on reload + */ + public void addListener(IFutureReloadListener listener) + { + listeners.add(listener); + } + + public List getListeners() + { + return ImmutableList.copyOf(listeners); + } +} diff --git a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java index 2ef5a1b5d..8060db7ab 100644 --- a/src/main/java/net/minecraftforge/event/ForgeEventFactory.java +++ b/src/main/java/net/minecraftforge/event/ForgeEventFactory.java @@ -51,6 +51,7 @@ import net.minecraft.item.ItemStack; import net.minecraft.item.ItemUseContext; import net.minecraft.loot.LootTable; import net.minecraft.loot.LootTableManager; +import net.minecraft.resources.IFutureReloadListener; import net.minecraft.world.spawner.AbstractSpawner; import net.minecraft.util.ActionResult; import net.minecraft.util.ActionResultType; @@ -722,4 +723,11 @@ public class ForgeEventFactory MinecraftForge.EVENT_BUS.post(event); return event.getNewTime(); } + + public static List onResourceReload() + { + AddReloadListenerEvent event = new AddReloadListenerEvent(); + MinecraftForge.EVENT_BUS.post(event); + return event.getListeners(); + } }