mirror of
https://github.com/MinecraftForge/MinecraftForge.git
synced 2026-11-16 18:45:09 +00:00
Move Registry Events to the mod event bus.
Add infrastructure to allow parallel or synchronous dispatch of mod events, and pass-through of events to the dispatch system. Signed-off-by: cpw <[email protected]>
This commit is contained in:
9 files changed
+162
-88
No files matched your search
@@ -19,40 +19,72 @@
|
||||
|
||||
package net.minecraftforge.fml;
|
||||
|
||||
import net.minecraftforge.eventbus.api.Event;
|
||||
import net.minecraftforge.fml.event.lifecycle.ModLifecycleEvent;
|
||||
import net.minecraftforge.fml.javafmlmod.FMLModContainer;
|
||||
import net.minecraftforge.forgespi.language.ILifecycleEvent;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
public enum LifecycleEventProvider
|
||||
{
|
||||
CONSTRUCT(()->new LifecycleEvent(ModLoadingStage.CONSTRUCT)),
|
||||
CREATE_REGISTRIES(()->new LifecycleEvent(ModLoadingStage.CREATE_REGISTRIES), ModList.inlineDispatcher),
|
||||
LOAD_REGISTRIES(()->new LifecycleEvent(ModLoadingStage.LOAD_REGISTRIES, LifecycleEvent.Progression.STAY), ModList.inlineDispatcher),
|
||||
SETUP(()->new LifecycleEvent(ModLoadingStage.COMMON_SETUP)),
|
||||
SIDED_SETUP(()->new LifecycleEvent(ModLoadingStage.SIDED_SETUP)),
|
||||
ENQUEUE_IMC(()->new LifecycleEvent(ModLoadingStage.ENQUEUE_IMC)),
|
||||
PROCESS_IMC(()->new LifecycleEvent(ModLoadingStage.PROCESS_IMC)),
|
||||
COMPLETE(()->new LifecycleEvent(ModLoadingStage.COMPLETE));
|
||||
|
||||
public void dispatch(Consumer<List<ModLoadingException>> errorHandler) {
|
||||
ModList.get().dispatchLifeCycleEvent(this.event.get(), errorHandler);
|
||||
}
|
||||
private final Supplier<? extends LifecycleEvent> event;
|
||||
private final BiConsumer<LifecycleEvent, Consumer<List<ModLoadingException>>> eventDispatcher;
|
||||
private Supplier<Event> customEventSupplier;
|
||||
private LifecycleEvent.Progression progression = LifecycleEvent.Progression.NEXT;
|
||||
|
||||
LifecycleEventProvider(Supplier<? extends LifecycleEvent> e)
|
||||
{
|
||||
event = e;
|
||||
this(e, ModList.parallelDispatcher);
|
||||
}
|
||||
|
||||
LifecycleEventProvider(Supplier<? extends LifecycleEvent> e, BiConsumer<LifecycleEvent, Consumer<List<ModLoadingException>>> eventDispatcher)
|
||||
{
|
||||
this.event = e;
|
||||
this.eventDispatcher = eventDispatcher;
|
||||
}
|
||||
|
||||
public void setCustomEventSupplier(Supplier<Event> eventSupplier) {
|
||||
this.customEventSupplier = eventSupplier;
|
||||
}
|
||||
|
||||
public void changeProgression(LifecycleEvent.Progression progression) {
|
||||
this.progression = progression;
|
||||
}
|
||||
|
||||
public void dispatch(Consumer<List<ModLoadingException>> errorHandler) {
|
||||
final LifecycleEvent lifecycleEvent = this.event.get();
|
||||
lifecycleEvent.setCustomEventSupplier(this.customEventSupplier);
|
||||
lifecycleEvent.changeProgression(this.progression);
|
||||
this.eventDispatcher.accept(lifecycleEvent, errorHandler);
|
||||
}
|
||||
|
||||
|
||||
public static class LifecycleEvent implements ILifecycleEvent<LifecycleEvent> {
|
||||
private final ModLoadingStage stage;
|
||||
|
||||
public LifecycleEvent(ModLoadingStage stage)
|
||||
private Supplier<Event> customEventSupplier;
|
||||
private Progression progression;
|
||||
LifecycleEvent(final ModLoadingStage stage)
|
||||
{
|
||||
this(stage, Progression.NEXT);
|
||||
}
|
||||
|
||||
LifecycleEvent(final ModLoadingStage stage, final Progression progression) {
|
||||
this.stage = stage;
|
||||
this.progression = progression;
|
||||
}
|
||||
|
||||
public ModLoadingStage fromStage()
|
||||
@@ -62,12 +94,42 @@ public enum LifecycleEventProvider
|
||||
|
||||
public ModLoadingStage toStage()
|
||||
{
|
||||
return ModLoadingStage.values()[this.stage.ordinal()+1];
|
||||
return progression.apply(this.stage);
|
||||
}
|
||||
|
||||
public ModLifecycleEvent buildModEvent(FMLModContainer fmlModContainer)
|
||||
public void setCustomEventSupplier(Supplier<Event> customEventSupplier) {
|
||||
this.customEventSupplier = customEventSupplier;
|
||||
}
|
||||
|
||||
public void changeProgression(Progression p) {
|
||||
this.progression = p;
|
||||
}
|
||||
|
||||
public Event getOrBuildEvent(FMLModContainer fmlModContainer)
|
||||
{
|
||||
if (customEventSupplier!=null) return customEventSupplier.get();
|
||||
|
||||
return stage.getModEvent(fmlModContainer);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "LifecycleEvent:"+stage;
|
||||
}
|
||||
|
||||
public enum Progression {
|
||||
NEXT((current)-> ModLoadingStage.values()[current.ordinal()+1]),
|
||||
STAY(Function.identity());
|
||||
|
||||
private final Function<ModLoadingStage, ModLoadingStage> edge;
|
||||
|
||||
Progression(Function<ModLoadingStage, ModLoadingStage> edge) {
|
||||
this.edge = edge;
|
||||
}
|
||||
|
||||
public ModLoadingStage apply(ModLoadingStage in) {
|
||||
return this.edge.apply(in);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user