001    /*
002     * The FML Forge Mod Loader suite. Copyright (C) 2012 cpw
003     *
004     * This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free
005     * Software Foundation; either version 2.1 of the License, or any later version.
006     *
007     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
008     * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
009     *
010     * You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51
011     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
012     */
013    package cpw.mods.fml.server;
014    
015    import java.util.List;
016    
017    import com.google.common.collect.ImmutableList;
018    import com.google.common.collect.MapDifference;
019    
020    import net.minecraft.entity.Entity;
021    import net.minecraft.network.INetworkManager;
022    import net.minecraft.network.packet.NetHandler;
023    import net.minecraft.network.packet.Packet;
024    import net.minecraft.network.packet.Packet131MapData;
025    import net.minecraft.server.MinecraftServer;
026    import net.minecraft.world.World;
027    import cpw.mods.fml.common.FMLCommonHandler;
028    import cpw.mods.fml.common.IFMLSidedHandler;
029    import cpw.mods.fml.common.Loader;
030    import cpw.mods.fml.common.ObfuscationReflectionHelper;
031    import cpw.mods.fml.common.network.EntitySpawnAdjustmentPacket;
032    import cpw.mods.fml.common.network.EntitySpawnPacket;
033    import cpw.mods.fml.common.network.ModMissingPacket;
034    import cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration;
035    import cpw.mods.fml.common.registry.GameData;
036    import cpw.mods.fml.common.registry.GameRegistry;
037    import cpw.mods.fml.common.registry.ItemData;
038    import cpw.mods.fml.common.registry.LanguageRegistry;
039    import cpw.mods.fml.relauncher.Side;
040    
041    /**
042     * Handles primary communication from hooked code into the system
043     *
044     * The FML entry point is {@link #beginServerLoading(MinecraftServer)} called from
045     * {@link net.minecraft.shared.DedicatedServer}
046     *
047     * Obfuscated code should focus on this class and other members of the "server"
048     * (or "client") code
049     *
050     * The actual mod loading is handled at arms length by {@link Loader}
051     *
052     * It is expected that a similar class will exist for each target environment:
053     * Bukkit and Client side.
054     *
055     * It should not be directly modified.
056     *
057     * @author cpw
058     *
059     */
060    public class FMLServerHandler implements IFMLSidedHandler
061    {
062        /**
063         * The singleton
064         */
065        private static final FMLServerHandler INSTANCE = new FMLServerHandler();
066    
067        /**
068         * A reference to the server itself
069         */
070        private MinecraftServer server;
071    
072        private FMLServerHandler()
073        {
074            FMLCommonHandler.instance().beginLoading(this);
075        }
076        /**
077         * Called to start the whole game off from
078         * {@link MinecraftServer#startServer}
079         *
080         * @param minecraftServer
081         */
082        public void beginServerLoading(MinecraftServer minecraftServer)
083        {
084            server = minecraftServer;
085            ObfuscationReflectionHelper.detectObfuscation(World.class);
086            Loader.instance().loadMods();
087        }
088    
089        /**
090         * Called a bit later on during server initialization to finish loading mods
091         */
092        public void finishServerLoading()
093        {
094            Loader.instance().initializeMods();
095            LanguageRegistry.reloadLanguageTable();
096            GameData.initializeServerGate(1);
097        }
098    
099        @Override
100        public void haltGame(String message, Throwable exception)
101        {
102            throw new RuntimeException(message, exception);
103        }
104    
105        /**
106         * Get the server instance
107         */
108        public MinecraftServer getServer()
109        {
110            return server;
111        }
112    
113        /**
114         * @return the instance
115         */
116        public static FMLServerHandler instance()
117        {
118            return INSTANCE;
119        }
120    
121        /* (non-Javadoc)
122         * @see cpw.mods.fml.common.IFMLSidedHandler#getAdditionalBrandingInformation()
123         */
124        @Override
125        public List<String> getAdditionalBrandingInformation()
126        {
127            return ImmutableList.<String>of();
128        }
129    
130        /* (non-Javadoc)
131         * @see cpw.mods.fml.common.IFMLSidedHandler#getSide()
132         */
133        @Override
134        public Side getSide()
135        {
136            return Side.SERVER;
137        }
138    
139        @Override
140        public void showGuiScreen(Object clientGuiElement)
141        {
142    
143        }
144    
145        @Override
146        public Entity spawnEntityIntoClientWorld(EntityRegistration er, EntitySpawnPacket packet)
147        {
148            // NOOP
149            return null;
150        }
151    
152        @Override
153        public void adjustEntityLocationOnClient(EntitySpawnAdjustmentPacket entitySpawnAdjustmentPacket)
154        {
155            // NOOP
156        }
157        @Override
158        public void sendPacket(Packet packet)
159        {
160            throw new RuntimeException("You cannot send a bare packet without a target on the server!");
161        }
162        @Override
163        public void displayMissingMods(ModMissingPacket modMissingPacket)
164        {
165            // NOOP on server
166        }
167        @Override
168        public void handleTinyPacket(NetHandler handler, Packet131MapData mapData)
169        {
170            // NOOP on server
171        }
172        @Override
173        public void setClientCompatibilityLevel(byte compatibilityLevel)
174        {
175            // NOOP on server
176        }
177        @Override
178        public byte getClientCompatibilityLevel()
179        {
180            return 0;
181        }
182    
183        @Override
184        public boolean shouldServerShouldBeKilledQuietly()
185        {
186            return false;
187        }
188        @Override
189        public void disconnectIDMismatch(MapDifference<Integer, ItemData> s, NetHandler handler, INetworkManager mgr)
190        {
191    
192        }
193    }