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.client;
014    
015    import java.util.ArrayList;
016    import java.util.Arrays;
017    import java.util.Collections;
018    import java.util.List;
019    import java.util.Map;
020    import java.util.logging.Level;
021    import java.util.logging.Logger;
022    
023    import net.minecraft.client.Minecraft;
024    import net.minecraft.server.MinecraftServer;
025    import net.minecraft.src.CrashReport;
026    import net.minecraft.src.Entity;
027    import net.minecraft.src.EntityLiving;
028    import net.minecraft.src.EntityPlayer;
029    import net.minecraft.src.GuiScreen;
030    import net.minecraft.src.Packet;
031    import net.minecraft.src.Render;
032    import net.minecraft.src.RenderManager;
033    import net.minecraft.src.World;
034    import net.minecraft.src.WorldClient;
035    
036    import com.google.common.base.Throwables;
037    import com.google.common.collect.ImmutableMap;
038    
039    import cpw.mods.fml.client.modloader.ModLoaderClientHelper;
040    import cpw.mods.fml.client.registry.KeyBindingRegistry;
041    import cpw.mods.fml.client.registry.RenderingRegistry;
042    import cpw.mods.fml.common.DummyModContainer;
043    import cpw.mods.fml.common.FMLCommonHandler;
044    import cpw.mods.fml.common.FMLLog;
045    import cpw.mods.fml.common.IFMLSidedHandler;
046    import cpw.mods.fml.common.Loader;
047    import cpw.mods.fml.common.LoaderException;
048    import cpw.mods.fml.common.MetadataCollection;
049    import cpw.mods.fml.common.MissingModsException;
050    import cpw.mods.fml.common.ModContainer;
051    import cpw.mods.fml.common.ModMetadata;
052    import cpw.mods.fml.common.ObfuscationReflectionHelper;
053    import cpw.mods.fml.common.Side;
054    import cpw.mods.fml.common.network.EntitySpawnAdjustmentPacket;
055    import cpw.mods.fml.common.network.EntitySpawnPacket;
056    import cpw.mods.fml.common.network.ModMissingPacket;
057    import cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration;
058    import cpw.mods.fml.common.registry.IEntityAdditionalSpawnData;
059    import cpw.mods.fml.common.registry.IThrowableEntity;
060    import cpw.mods.fml.common.registry.LanguageRegistry;
061    
062    
063    /**
064     * Handles primary communication from hooked code into the system
065     *
066     * The FML entry point is {@link #beginMinecraftLoading(MinecraftServer)} called from
067     * {@link MinecraftServer}
068     *
069     * Obfuscated code should focus on this class and other members of the "server"
070     * (or "client") code
071     *
072     * The actual mod loading is handled at arms length by {@link Loader}
073     *
074     * It is expected that a similar class will exist for each target environment:
075     * Bukkit and Client side.
076     *
077     * It should not be directly modified.
078     *
079     * @author cpw
080     *
081     */
082    public class FMLClientHandler implements IFMLSidedHandler
083    {
084        /**
085         * The singleton
086         */
087        private static final FMLClientHandler INSTANCE = new FMLClientHandler();
088    
089        /**
090         * A reference to the server itself
091         */
092        private Minecraft client;
093    
094        /**
095         * Called to start the whole game off from
096         * {@link MinecraftServer#startServer}
097         *
098         * @param minecraftServer
099         */
100    
101        private DummyModContainer optifineContainer;
102    
103        private boolean guiLoaded;
104    
105        private boolean serverIsRunning;
106    
107        private MissingModsException modsMissing;
108    
109        private boolean loading;
110    
111        public void beginMinecraftLoading(Minecraft minecraft)
112        {
113            if (minecraft.isDemo())
114            {
115                FMLLog.severe("DEMO MODE DETECTED, FML will not work. Finishing now.");
116                haltGame("FML will not run in demo mode", new RuntimeException());
117                return;
118            }
119    
120            loading = true;
121            client = minecraft;
122            ObfuscationReflectionHelper.detectObfuscation(World.class);
123            TextureFXManager.instance().setClient(client);
124            FMLCommonHandler.instance().beginLoading(this);
125            new ModLoaderClientHelper(client);
126            try
127            {
128                Class<?> optifineConfig = Class.forName("Config", false, Loader.instance().getModClassLoader());
129                String optifineVersion = (String) optifineConfig.getField("VERSION").get(null);
130                Map<String,Object> dummyOptifineMeta = ImmutableMap.<String,Object>builder().put("name", "Optifine").put("version", optifineVersion).build();
131                ModMetadata optifineMetadata = MetadataCollection.from(getClass().getResourceAsStream("optifinemod.info"),"optifine").getMetadataForId("optifine", dummyOptifineMeta);
132                optifineContainer = new DummyModContainer(optifineMetadata);
133                FMLLog.info("Forge Mod Loader has detected optifine %s, enabling compatibility features",optifineContainer.getVersion());
134            }
135            catch (Exception e)
136            {
137                optifineContainer = null;
138            }
139            try
140            {
141                Loader.instance().loadMods();
142            }
143            catch (MissingModsException missing)
144            {
145                modsMissing = missing;
146            }
147            catch (LoaderException le)
148            {
149                haltGame("There was a severe problem during mod loading that has caused the game to fail", le);
150                return;
151            }
152        }
153    
154        @Override
155        public void haltGame(String message, Throwable t)
156        {
157            client.displayCrashReport(new CrashReport(message, t));
158            throw Throwables.propagate(t);
159        }
160        /**
161         * Called a bit later on during initialization to finish loading mods
162         * Also initializes key bindings
163         *
164         */
165        @SuppressWarnings("deprecation")
166        public void finishMinecraftLoading()
167        {
168            if (modsMissing != null)
169            {
170                return;
171            }
172            try
173            {
174                Loader.instance().initializeMods();
175            }
176            catch (LoaderException le)
177            {
178                haltGame("There was a severe problem during mod loading that has caused the game to fail", le);
179                return;
180            }
181            LanguageRegistry.reloadLanguageTable();
182            RenderingRegistry.instance().loadEntityRenderers((Map<Class<? extends Entity>, Render>)RenderManager.instance.entityRenderMap);
183    
184            loading = false;
185            KeyBindingRegistry.instance().uploadKeyBindingsToGame(client.gameSettings);
186        }
187    
188        public void onInitializationComplete()
189        {
190            if (modsMissing != null)
191            {
192                client.displayGuiScreen(new GuiModsMissing(modsMissing));
193            }
194            else
195            {
196                TextureFXManager.instance().loadTextures(client.texturePackList.getSelectedTexturePack());
197            }
198        }
199        /**
200         * Get the server instance
201         *
202         * @return
203         */
204        public Minecraft getClient()
205        {
206            return client;
207        }
208    
209        /**
210         * Get a handle to the client's logger instance
211         * The client actually doesn't have one- so we return null
212         */
213        public Logger getMinecraftLogger()
214        {
215            return null;
216        }
217    
218        /**
219         * @return the instance
220         */
221        public static FMLClientHandler instance()
222        {
223            return INSTANCE;
224        }
225    
226        /**
227         * @param player
228         * @param gui
229         */
230        public void displayGuiScreen(EntityPlayer player, GuiScreen gui)
231        {
232            if (client.thePlayer==player && gui != null) {
233                client.displayGuiScreen(gui);
234            }
235        }
236    
237        /**
238         * @param mods
239         */
240        public void addSpecialModEntries(ArrayList<ModContainer> mods)
241        {
242            if (optifineContainer!=null) {
243                mods.add(optifineContainer);
244            }
245        }
246    
247        @Override
248        public List<String> getAdditionalBrandingInformation()
249        {
250            if (optifineContainer!=null)
251            {
252                return Arrays.asList(String.format("Optifine %s",optifineContainer.getVersion()));
253            } else {
254                return Collections.emptyList();
255            }
256        }
257    
258        @Override
259        public Side getSide()
260        {
261            return Side.CLIENT;
262        }
263    
264        public boolean hasOptifine()
265        {
266            return optifineContainer!=null;
267        }
268    
269        @Override
270        public void showGuiScreen(Object clientGuiElement)
271        {
272            GuiScreen gui = (GuiScreen) clientGuiElement;
273            client.displayGuiScreen(gui);
274        }
275    
276        @Override
277        public Entity spawnEntityIntoClientWorld(EntityRegistration er, EntitySpawnPacket packet)
278        {
279            WorldClient wc = client.theWorld;
280    
281            Class<? extends Entity> cls = er.getEntityClass();
282    
283            try
284            {
285                Entity entity;
286                if (er.hasCustomSpawning())
287                {
288                    entity = er.doCustomSpawning(packet);
289                }
290                else
291                {
292                    entity = (Entity)(cls.getConstructor(World.class).newInstance(wc));
293                    entity.entityId = packet.entityId;
294                    entity.setLocationAndAngles(packet.scaledX, packet.scaledY, packet.scaledZ, packet.scaledYaw, packet.scaledPitch);
295                    if (entity instanceof EntityLiving)
296                    {
297                        ((EntityLiving)entity).rotationYawHead = packet.scaledHeadYaw;
298                    }
299    
300                }
301    
302                entity.serverPosX = packet.rawX;
303                entity.serverPosY = packet.rawY;
304                entity.serverPosZ = packet.rawZ;
305    
306                if (entity instanceof IThrowableEntity)
307                {
308                    Entity thrower = client.thePlayer.entityId == packet.throwerId ? client.thePlayer : wc.getEntityByID(packet.throwerId);
309                    ((IThrowableEntity)entity).setThrower(thrower);
310                }
311    
312    
313                Entity parts[] = entity.getParts();
314                if (parts != null)
315                {
316                    int i = packet.entityId - entity.entityId;
317                    for (int j = 0; j < parts.length; j++)
318                    {
319                        parts[j].entityId += i;
320                    }
321                }
322    
323    
324                if (packet.metadata != null)
325                {
326                    entity.getDataWatcher().updateWatchedObjectsFromList((List)packet.metadata);
327                }
328    
329                if (packet.throwerId > 0)
330                {
331                    entity.setVelocity(packet.speedScaledX, packet.speedScaledY, packet.speedScaledZ);
332                }
333    
334                if (entity instanceof IEntityAdditionalSpawnData)
335                {
336                    ((IEntityAdditionalSpawnData)entity).readSpawnData(packet.dataStream);
337                }
338    
339                wc.addEntityToWorld(packet.entityId, entity);
340                return entity;
341            }
342            catch (Exception e)
343            {
344                FMLLog.log(Level.SEVERE, e, "A severe problem occurred during the spawning of an entity");
345                throw Throwables.propagate(e);
346            }
347        }
348    
349        @Override
350        public void adjustEntityLocationOnClient(EntitySpawnAdjustmentPacket packet)
351        {
352            Entity ent = client.theWorld.getEntityByID(packet.entityId);
353            if (ent != null)
354            {
355                ent.serverPosX = packet.serverX;
356                ent.serverPosY = packet.serverY;
357                ent.serverPosZ = packet.serverZ;
358            }
359            else
360            {
361                FMLLog.fine("Attempted to adjust the position of entity %d which is not present on the client", packet.entityId);
362            }
363        }
364    
365        @Override
366        public void beginServerLoading(MinecraftServer server)
367        {
368            // NOOP
369        }
370    
371        @Override
372        public void finishServerLoading()
373        {
374            // NOOP
375        }
376    
377        @Override
378        public MinecraftServer getServer()
379        {
380            return client.getIntegratedServer();
381        }
382    
383        @Override
384        public void sendPacket(Packet packet)
385        {
386            client.thePlayer.sendQueue.addToSendQueue(packet);
387        }
388    
389        @Override
390        public void displayMissingMods(ModMissingPacket modMissingPacket)
391        {
392            client.displayGuiScreen(new GuiModsMissingForServer(modMissingPacket));
393        }
394    
395        /**
396         * If the client is in the midst of loading, we disable saving so that custom settings aren't wiped out
397         * @return
398         */
399        public boolean isLoading()
400        {
401            return loading;
402        }
403    }