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