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