001package cpw.mods.fml.common;
002
003import com.google.common.base.Throwables;
004
005import cpw.mods.fml.common.event.FMLConstructionEvent;
006import cpw.mods.fml.common.event.FMLEvent;
007import cpw.mods.fml.common.event.FMLInitializationEvent;
008import cpw.mods.fml.common.event.FMLLoadCompleteEvent;
009import cpw.mods.fml.common.event.FMLPostInitializationEvent;
010import cpw.mods.fml.common.event.FMLPreInitializationEvent;
011import cpw.mods.fml.common.event.FMLServerStartedEvent;
012import cpw.mods.fml.common.event.FMLServerStartingEvent;
013import cpw.mods.fml.common.event.FMLServerStoppedEvent;
014import cpw.mods.fml.common.event.FMLServerStoppingEvent;
015import cpw.mods.fml.common.event.FMLStateEvent;
016
017/**
018 * The state enum used to help track state progression for the loader
019 * @author cpw
020 *
021 */
022public enum LoaderState
023{
024    NOINIT("Uninitialized",null),
025    LOADING("Loading",null),
026    CONSTRUCTING("Constructing mods",FMLConstructionEvent.class),
027    PREINITIALIZATION("Pre-initializing mods", FMLPreInitializationEvent.class),
028    INITIALIZATION("Initializing mods", FMLInitializationEvent.class),
029    POSTINITIALIZATION("Post-initializing mods", FMLPostInitializationEvent.class),
030    AVAILABLE("Mod loading complete", FMLLoadCompleteEvent.class),
031    SERVER_STARTING("Server starting", FMLServerStartingEvent.class),
032    SERVER_STARTED("Server started", FMLServerStartedEvent.class),
033    SERVER_STOPPING("Server stopping", FMLServerStoppingEvent.class),
034    SERVER_STOPPED("Server stopped", FMLServerStoppedEvent.class),
035    ERRORED("Mod Loading errored",null);
036
037
038    private Class<? extends FMLStateEvent> eventClass;
039    private String name;
040
041    private LoaderState(String name, Class<? extends FMLStateEvent> event)
042    {
043        this.name = name;
044        this.eventClass = event;
045    }
046
047    public LoaderState transition(boolean errored)
048    {
049        if (errored)
050        {
051            return ERRORED;
052        }
053        // stopping -> available
054        if (this == SERVER_STOPPED)
055        {
056            return AVAILABLE;
057        }
058        return values()[ordinal() < values().length ? ordinal()+1 : ordinal()];
059    }
060
061    public boolean hasEvent()
062    {
063        return eventClass != null;
064    }
065
066    public FMLStateEvent getEvent(Object... eventData)
067    {
068        try
069        {
070            return eventClass.getConstructor(Object[].class).newInstance((Object)eventData);
071        }
072        catch (Exception e)
073        {
074            throw Throwables.propagate(e);
075        }
076    }
077    public LoaderState requiredState()
078    {
079        if (this == NOINIT) return NOINIT;
080        return LoaderState.values()[this.ordinal()-1];
081    }
082    public enum ModState
083    {
084        UNLOADED("Unloaded"),
085        LOADED("Loaded"),
086        CONSTRUCTED("Constructed"),
087        PREINITIALIZED("Pre-initialized"),
088        INITIALIZED("Initialized"),
089        POSTINITIALIZED("Post-initialized"),
090        AVAILABLE("Available"),
091        DISABLED("Disabled"),
092        ERRORED("Errored");
093
094        private String label;
095
096        private ModState(String label)
097        {
098            this.label = label;
099        }
100
101        public String toString()
102        {
103            return this.label;
104        }
105    }
106}