001    /*
002     * The FML Forge Mod Loader suite.
003     * Copyright (C) 2012 cpw
004     *
005     * 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
006     * Software Foundation; either version 2.1 of the License, or any later version.
007     *
008     * 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
009     * A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
010     *
011     * 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
012     * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
013     */
014    
015    package cpw.mods.fml.common.modloader;
016    
017    import java.util.EnumSet;
018    import java.util.HashMap;
019    import java.util.Map;
020    import java.util.concurrent.Callable;
021    
022    import com.google.common.collect.ArrayListMultimap;
023    import com.google.common.collect.ListMultimap;
024    import com.google.common.collect.Maps;
025    
026    import net.minecraft.src.BaseMod;
027    import net.minecraft.src.Container;
028    import net.minecraft.src.Entity;
029    import net.minecraft.src.EntityDragon;
030    import net.minecraft.src.EntityPlayer;
031    import net.minecraft.src.IAnimals;
032    import net.minecraft.src.ICommand;
033    import net.minecraft.src.TradeEntry;
034    import cpw.mods.fml.common.FMLCommonHandler;
035    import cpw.mods.fml.common.ICraftingHandler;
036    import cpw.mods.fml.common.IDispenseHandler;
037    import cpw.mods.fml.common.IFuelHandler;
038    import cpw.mods.fml.common.IPickupNotifier;
039    import cpw.mods.fml.common.IWorldGenerator;
040    import cpw.mods.fml.common.Loader;
041    import cpw.mods.fml.common.TickType;
042    import cpw.mods.fml.common.network.IConnectionHandler;
043    import cpw.mods.fml.common.network.IGuiHandler;
044    import cpw.mods.fml.common.network.IPacketHandler;
045    import cpw.mods.fml.common.network.NetworkRegistry;
046    import cpw.mods.fml.common.registry.EntityRegistry;
047    import cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration;
048    import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
049    import cpw.mods.fml.common.registry.VillagerRegistry;
050    
051    /**
052     * @author cpw
053     *
054     */
055    @SuppressWarnings("deprecation")
056    public class ModLoaderHelper
057    {
058        public static IModLoaderSidedHelper sidedHelper;
059    
060        private static Map<Integer, ModLoaderGuiHelper> guiHelpers = Maps.newHashMap();
061    
062        public static void updateStandardTicks(BaseModProxy mod, boolean enable, boolean useClock)
063        {
064            ModLoaderModContainer mlmc = (ModLoaderModContainer) Loader.instance().activeModContainer();
065            BaseModTicker ticker = mlmc.getGameTickHandler();
066            EnumSet<TickType> ticks = ticker.ticks();
067            // If we're enabled we get render ticks
068            if (enable && !useClock) {
069                ticks.add(TickType.RENDER);
070            } else {
071                ticks.remove(TickType.RENDER);
072            }
073            // If we're enabled but we want clock ticks, or we're server side we get game ticks
074            if (enable && (useClock || FMLCommonHandler.instance().getSide().isServer())) {
075                ticks.add(TickType.CLIENT);
076                ticks.add(TickType.WORLDLOAD);
077            } else {
078                ticks.remove(TickType.CLIENT);
079                ticks.remove(TickType.WORLDLOAD);
080            }
081        }
082    
083        public static void updateGUITicks(BaseModProxy mod, boolean enable, boolean useClock)
084        {
085            ModLoaderModContainer mlmc = (ModLoaderModContainer) Loader.instance().activeModContainer();
086            EnumSet<TickType> ticks = mlmc.getGUITickHandler().ticks();
087            // If we're enabled and we don't want clock ticks we get render ticks
088            if (enable && !useClock) {
089                ticks.add(TickType.RENDER);
090            } else {
091                ticks.remove(TickType.RENDER);
092            }
093            // If we're enabled but we want clock ticks, or we're server side we get world ticks
094            if (enable && useClock) {
095                ticks.add(TickType.CLIENT);
096                ticks.add(TickType.WORLDLOAD);
097            } else {
098                ticks.remove(TickType.CLIENT);
099                ticks.remove(TickType.WORLDLOAD);
100            }
101        }
102    
103        public static IPacketHandler buildPacketHandlerFor(BaseModProxy mod)
104        {
105            return new ModLoaderPacketHandler(mod);
106        }
107    
108        public static IWorldGenerator buildWorldGenHelper(BaseModProxy mod)
109        {
110            return new ModLoaderWorldGenerator(mod);
111        }
112    
113        public static IFuelHandler buildFuelHelper(BaseModProxy mod)
114        {
115            return new ModLoaderFuelHelper(mod);
116        }
117    
118        public static ICraftingHandler buildCraftingHelper(BaseModProxy mod)
119        {
120            return new ModLoaderCraftingHelper(mod);
121        }
122    
123        public static void finishModLoading(ModLoaderModContainer mc)
124        {
125            if (sidedHelper != null)
126            {
127                sidedHelper.finishModLoading(mc);
128            }
129        }
130    
131        public static IConnectionHandler buildConnectionHelper(BaseModProxy mod)
132        {
133            return new ModLoaderConnectionHandler(mod);
134        }
135    
136        public static IPickupNotifier buildPickupHelper(BaseModProxy mod)
137        {
138            return new ModLoaderPickupNotifier(mod);
139        }
140    
141        public static void buildGuiHelper(BaseModProxy mod, int id)
142        {
143            ModLoaderGuiHelper handler = new ModLoaderGuiHelper(mod, id);
144            guiHelpers.put(id, handler);
145            NetworkRegistry.instance().registerGuiHandler(mod, handler);
146        }
147    
148        public static void openGui(int id, EntityPlayer player, Container container, int x, int y, int z)
149        {
150            ModLoaderGuiHelper helper = guiHelpers.get(id);
151            helper.injectContainer(container);
152            player.openGui(helper.getMod(), id, player.worldObj, x, y, z);
153        }
154    
155        public static Object getClientSideGui(BaseModProxy mod, EntityPlayer player, int ID, int x, int y, int z)
156        {
157            if (sidedHelper != null)
158            {
159                return sidedHelper.getClientGui(mod, player, ID, x, y, z);
160            }
161            return null;
162        }
163    
164        public static IDispenseHandler buildDispenseHelper(BaseModProxy mod)
165        {
166            return new ModLoaderDispenseHelper(mod);
167        }
168    
169    
170        public static void buildEntityTracker(BaseModProxy mod, Class<? extends Entity> entityClass, int entityTypeId, int updateRange, int updateInterval,
171                boolean sendVelocityInfo)
172        {
173            EntityRegistration er = EntityRegistry.registerModLoaderEntity(mod, entityClass, entityTypeId, updateRange, updateInterval, sendVelocityInfo);
174            er.setCustomSpawning(new ModLoaderEntitySpawnCallback(mod, er), EntityDragon.class.isAssignableFrom(entityClass) || IAnimals.class.isAssignableFrom(entityClass));
175        }
176    
177        private static ModLoaderVillageTradeHandler[] tradeHelpers = new ModLoaderVillageTradeHandler[6];
178    
179        public static void registerTrade(int profession, TradeEntry entry)
180        {
181            assert profession < tradeHelpers.length : "The profession is out of bounds";
182            if (tradeHelpers[profession] == null)
183            {
184                tradeHelpers[profession] = new ModLoaderVillageTradeHandler();
185                VillagerRegistry.instance().registerVillageTradeHandler(profession, tradeHelpers[profession]);
186            }
187    
188            tradeHelpers[profession].addTrade(entry);
189        }
190    
191        public static void addCommand(ICommand command)
192        {
193            ModLoaderModContainer mlmc = (ModLoaderModContainer) Loader.instance().activeModContainer();
194            if (mlmc!=null)
195            {
196                mlmc.addServerCommand(command);
197            }
198        }
199    }