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