Files
MinecraftForge/src/main/java/net/minecraftforge/fml/LifecycleEventProvider.java
T

135 lines
4.8 KiB
Java
Raw Normal View History

2018-06-10 21:12:46 -04:00
/*
* Minecraft Forge
2019-02-10 17:57:03 -05:00
* Copyright (c) 2016-2019.
2018-06-10 21:12:46 -04:00
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation version 2.1
* of the License.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
package net.minecraftforge.fml;
2019-01-19 23:26:25 -05:00
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;
2018-06-10 21:12:46 -04:00
2018-09-28 21:07:46 -04:00
import java.util.List;
2019-01-19 23:26:25 -05:00
import java.util.function.BiConsumer;
2018-09-28 21:07:46 -04:00
import java.util.function.Consumer;
2019-01-19 23:26:25 -05:00
import java.util.function.Function;
2018-06-10 21:12:46 -04:00
import java.util.function.Supplier;
public enum LifecycleEventProvider
{
CONSTRUCT(()->new LifecycleEvent(ModLoadingStage.CONSTRUCT)),
2019-01-19 23:26:25 -05:00
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));
2018-06-10 21:12:46 -04:00
private final Supplier<? extends LifecycleEvent> event;
2019-01-19 23:26:25 -05:00
private final BiConsumer<LifecycleEvent, Consumer<List<ModLoadingException>>> eventDispatcher;
private Supplier<Event> customEventSupplier;
private LifecycleEvent.Progression progression = LifecycleEvent.Progression.NEXT;
2018-06-10 21:12:46 -04:00
LifecycleEventProvider(Supplier<? extends LifecycleEvent> e)
{
2019-01-19 23:26:25 -05:00
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);
2018-06-10 21:12:46 -04:00
}
public static class LifecycleEvent implements ILifecycleEvent<LifecycleEvent> {
2018-06-10 21:12:46 -04:00
private final ModLoadingStage stage;
2019-01-19 23:26:25 -05:00
private Supplier<Event> customEventSupplier;
private Progression progression;
LifecycleEvent(final ModLoadingStage stage)
2018-06-10 21:12:46 -04:00
{
2019-01-19 23:26:25 -05:00
this(stage, Progression.NEXT);
}
LifecycleEvent(final ModLoadingStage stage, final Progression progression) {
2018-06-10 21:12:46 -04:00
this.stage = stage;
2019-01-19 23:26:25 -05:00
this.progression = progression;
2018-06-10 21:12:46 -04:00
}
public ModLoadingStage fromStage()
{
return this.stage;
}
public ModLoadingStage toStage()
{
2019-01-19 23:26:25 -05:00
return progression.apply(this.stage);
}
public void setCustomEventSupplier(Supplier<Event> customEventSupplier) {
this.customEventSupplier = customEventSupplier;
2018-06-10 21:12:46 -04:00
}
2019-01-19 23:26:25 -05:00
public void changeProgression(Progression p) {
this.progression = p;
}
public Event getOrBuildEvent(ModContainer modContainer)
{
2019-01-19 23:26:25 -05:00
if (customEventSupplier!=null) return customEventSupplier.get();
return stage.getModEvent(modContainer);
}
2019-01-19 23:26:25 -05:00
@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);
}
}
2018-06-10 21:12:46 -04:00
}
}