Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correctly support EventPriority for non-parallel mod bus events #8

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ allprojects {
version = gradleutils.getTagOffsetVersion()

repositories {
mavenLocal()
mavenCentral()

maven {
Expand Down
8 changes: 8 additions & 0 deletions core/src/main/java/net/minecraftforge/fml/ModContainer.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package net.minecraftforge.fml;

import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.fml.config.IConfigEvent;
import net.minecraftforge.fml.config.ModConfig;
import net.minecraftforge.fml.event.IModBusEvent;
Expand Down Expand Up @@ -171,4 +172,11 @@ public void dispatchConfigEvent(IConfigEvent event) {
* @param e Event to accept
*/
protected <T extends Event & IModBusEvent> void acceptEvent(T e) {}

/**
* Accept an arbitrary event for processing by the mod, only for a specific phase.
* Probably posted to an event bus in the lower level container.
* @param e Event to accept
*/
protected <T extends Event & IModBusEvent> void acceptEvent(EventPriority phase, T e) {}
}
25 changes: 14 additions & 11 deletions core/src/main/java/net/minecraftforge/fml/ModLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import com.google.common.collect.ImmutableList;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.fml.event.IModBusEvent;
import net.minecraftforge.fml.loading.FMLEnvironment;
import net.minecraftforge.fml.loading.FMLLoader;
Expand Down Expand Up @@ -323,14 +324,13 @@ public <T extends Event & IModBusEvent> void postEvent(T e) {
LOGGER.error("Cowardly refusing to send event {} to a broken mod state", e.getClass().getName());
return;
}
ModList.get().forEachModInOrder(mc -> mc.acceptEvent(e));
for (EventPriority phase : EventPriority.values()) {
e.setPhase(phase);
ModList.get().forEachModInOrder(mc -> mc.acceptEvent(phase, e));
}
}
public <T extends Event & IModBusEvent> T postEventWithReturn(T e) {
if (!loadingStateValid) {
LOGGER.error("Cowardly refusing to send event {} to a broken mod state", e.getClass().getName());
return e;
}
ModList.get().forEachModInOrder(mc -> mc.acceptEvent(e));
postEvent(e);
return e;
}
public <T extends Event & IModBusEvent> void postEventWrapContainerInModOrder(T event) {
Expand All @@ -341,11 +341,14 @@ public <T extends Event & IModBusEvent> void postEventWithWrapInModOrder(T e, Bi
LOGGER.error("Cowardly refusing to send event {} to a broken mod state", e.getClass().getName());
return;
}
ModList.get().forEachModInOrder(mc -> {
pre.accept(mc, e);
mc.acceptEvent(e);
post.accept(mc, e);
});
for (EventPriority phase : EventPriority.values()) {
e.setPhase(phase);
ModList.get().forEachModInOrder(mc -> {
pre.accept(mc, e);
mc.acceptEvent(phase, e);
post.accept(mc, e);
});
}
}

public List<ModLoadingWarning> getWarnings()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import net.minecraftforge.eventbus.EventBusErrorMessage;
import net.minecraftforge.eventbus.api.BusBuilder;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.eventbus.api.EventPriority;
import net.minecraftforge.eventbus.api.IEventBus;
import net.minecraftforge.eventbus.api.IEventListener;
import net.minecraftforge.fml.ModContainer;
Expand Down Expand Up @@ -111,4 +112,16 @@ protected <T extends Event & IModBusEvent> void acceptEvent(final T e) {
throw new ModLoadingException(modInfo, modLoadingStage, "fml.modloading.errorduringevent", t);
}
}

@Override
protected <T extends Event & IModBusEvent> void acceptEvent(EventPriority phase, final T e) {
try {
LOGGER.trace(LOADING, "Firing event for modid {} : {} (event phase {})", this.getModId(), e, phase);
this.eventBus.postPhase(phase, e);
LOGGER.trace(LOADING, "Fired event for modid {} : {} (event phase {})", this.getModId(), e, phase);
} catch (Throwable t) {
LOGGER.error(LOADING,"Caught exception during event {} dispatch for modid {} (event phase {})", e, this.getModId(), phase, t);
throw new ModLoadingException(modInfo, modLoadingStage, "fml.modloading.errorduringevent", t);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,12 @@
package net.minecraftforge.fml.lowcodemod;

import com.mojang.logging.LogUtils;
import net.minecraftforge.eventbus.api.Event;
import net.minecraftforge.fml.IExtensionPoint;
import net.minecraftforge.fml.ModContainer;
import net.minecraftforge.fml.event.IModBusEvent;
import net.minecraftforge.forgespi.language.IModInfo;
import net.minecraftforge.forgespi.language.ModFileScanData;
import org.slf4j.Logger;

import java.util.Objects;

import static net.minecraftforge.fml.loading.LogMarkers.LOADING;

public class LowCodeModContainer extends ModContainer
Expand Down Expand Up @@ -45,9 +41,4 @@ public Object getMod()
{
return modInstance;
}

@Override
protected <T extends Event & IModBusEvent> void acceptEvent(final T e)
{
}
}