From d0566ebadc9f5a78c5566ae11bdf3ca50447863f Mon Sep 17 00:00:00 2001 From: Ben Staddon Date: Fri, 13 Apr 2018 01:18:43 +0100 Subject: [PATCH] Suppress firing events at load time if an error has already occurred (#4801) --- .../net/minecraftforge/client/model/ModelLoader.java | 2 ++ .../net/minecraftforge/fml/common/LoadController.java | 2 ++ .../fml/common/eventhandler/EventBus.java | 11 ++++++++++- .../debug/mod/ClientLoadingExceptionTest.java | 7 +++++++ 4 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/main/java/net/minecraftforge/client/model/ModelLoader.java b/src/main/java/net/minecraftforge/client/model/ModelLoader.java index e725ef1da..d1707aa88 100644 --- a/src/main/java/net/minecraftforge/client/model/ModelLoader.java +++ b/src/main/java/net/minecraftforge/client/model/ModelLoader.java @@ -952,6 +952,8 @@ public final class ModelLoader extends ModelBakery */ public void onPostBakeEvent(IRegistry modelRegistry) { + if (!isLoading) return; + IBakedModel missingModel = modelRegistry.getObject(MODEL_MISSING); Map modelErrors = Maps.newHashMap(); Set printedBlockStateErrors = Sets.newHashSet(); diff --git a/src/main/java/net/minecraftforge/fml/common/LoadController.java b/src/main/java/net/minecraftforge/fml/common/LoadController.java index c5ed6fb73..681631f06 100644 --- a/src/main/java/net/minecraftforge/fml/common/LoadController.java +++ b/src/main/java/net/minecraftforge/fml/common/LoadController.java @@ -28,6 +28,7 @@ import java.util.Map; import java.util.Map.Entry; import java.util.stream.Collectors; +import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.common.util.TextTable; import net.minecraftforge.fml.common.LoaderState.ModState; import net.minecraftforge.fml.common.ProgressManager.ProgressBar; @@ -185,6 +186,7 @@ public class LoadController if (errors.size() > 0) { FMLLog.log.fatal("Fatal errors were detected during {}. Loading cannot continue.", state); + MinecraftForge.EVENT_BUS.shutdown(); state = state.transition(true); throw throwStoredErrors(); } diff --git a/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java b/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java index 1b2c872bf..11be42f97 100644 --- a/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java +++ b/src/main/java/net/minecraftforge/fml/common/eventhandler/EventBus.java @@ -47,6 +47,7 @@ public class EventBus implements IEventExceptionHandler private Map listenerOwners = new MapMaker().weakKeys().weakValues().makeMap(); private final int busID = maxID++; private IEventExceptionHandler exceptionHandler; + private boolean shutdown; public EventBus() { @@ -170,6 +171,8 @@ public class EventBus implements IEventExceptionHandler public boolean post(Event event) { + if (shutdown) return false; + IEventListener[] listeners = event.getListenerList().getListeners(busID); int index = 0; try @@ -185,7 +188,13 @@ public class EventBus implements IEventExceptionHandler Throwables.throwIfUnchecked(throwable); throw new RuntimeException(throwable); } - return (event.isCancelable() ? event.isCanceled() : false); + return event.isCancelable() && event.isCanceled(); + } + + public void shutdown() + { + FMLLog.log.warn("EventBus {} shutting down - future events will not be posted.", busID); + shutdown = true; } @Override diff --git a/src/test/java/net/minecraftforge/debug/mod/ClientLoadingExceptionTest.java b/src/test/java/net/minecraftforge/debug/mod/ClientLoadingExceptionTest.java index 3a344d7a9..1a01663c0 100644 --- a/src/test/java/net/minecraftforge/debug/mod/ClientLoadingExceptionTest.java +++ b/src/test/java/net/minecraftforge/debug/mod/ClientLoadingExceptionTest.java @@ -22,6 +22,7 @@ package net.minecraftforge.debug.mod; import net.minecraft.client.gui.FontRenderer; import net.minecraft.client.gui.GuiErrorScreen; import net.minecraft.item.Item; +import net.minecraftforge.client.event.ModelBakeEvent; import net.minecraftforge.common.MinecraftForge; import net.minecraftforge.event.RegistryEvent; import net.minecraftforge.fml.client.CustomModLoadingErrorDisplayException; @@ -58,6 +59,12 @@ public class ClientLoadingExceptionTest throw new RuntimeException("This should not be called because the mod threw an exception earlier in Pre-Init and is in a broken state."); } + @SubscribeEvent + public void onModelBake(ModelBakeEvent e) + { + throw new RuntimeException("This should not be called because the mod threw an exception earlier in Pre-Init and is in a broken state."); + } + @Mod.EventHandler public void onInit(FMLInitializationEvent e) {