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