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.IDispenserHandler;
038    import cpw.mods.fml.common.IFuelHandler;
039    import cpw.mods.fml.common.IPickupNotifier;
040    import cpw.mods.fml.common.IWorldGenerator;
041    import cpw.mods.fml.common.Loader;
042    import cpw.mods.fml.common.TickType;
043    import cpw.mods.fml.common.network.IConnectionHandler;
044    import cpw.mods.fml.common.network.IGuiHandler;
045    import cpw.mods.fml.common.network.IPacketHandler;
046    import cpw.mods.fml.common.network.NetworkRegistry;
047    import cpw.mods.fml.common.registry.EntityRegistry;
048    import cpw.mods.fml.common.registry.EntityRegistry.EntityRegistration;
049    import cpw.mods.fml.common.registry.VillagerRegistry.IVillageTradeHandler;
050    import cpw.mods.fml.common.registry.VillagerRegistry;
051    
052    /**
053     * @author cpw
054     *
055     */
056    @SuppressWarnings("deprecation")
057    public class ModLoaderHelper
058    {
059        public static IModLoaderSidedHelper sidedHelper;
060    
061        private static Map<Integer, ModLoaderGuiHelper> guiHelpers = Maps.newHashMap();
062    
063        public static void updateStandardTicks(BaseModProxy mod, boolean enable, boolean useClock)
064        {
065            ModLoaderModContainer mlmc = (ModLoaderModContainer) Loader.instance().activeModContainer();
066            BaseModTicker ticker = mlmc.getGameTickHandler();
067            EnumSet<TickType> ticks = ticker.ticks();
068            // If we're enabled we get render ticks
069            if (enable && !useClock) {
070                ticks.add(TickType.RENDER);
071            } else {
072                ticks.remove(TickType.RENDER);
073            }
074            // If we're enabled but we want clock ticks, or we're server side we get game ticks
075            if (enable && (useClock || FMLCommonHandler.instance().getSide().isServer())) {
076                ticks.add(TickType.CLIENT);
077                ticks.add(TickType.WORLDLOAD);
078            } else {
079                ticks.remove(TickType.CLIENT);
080                ticks.remove(TickType.WORLDLOAD);
081            }
082        }
083    
084        public static void updateGUITicks(BaseModProxy mod, boolean enable, boolean useClock)
085        {
086            ModLoaderModContainer mlmc = (ModLoaderModContainer) Loader.instance().activeModContainer();
087            EnumSet<TickType> ticks = mlmc.getGUITickHandler().ticks();
088            // If we're enabled and we don't want clock ticks we get render ticks
089            if (enable && !useClock) {
090                ticks.add(TickType.RENDER);
091            } else {
092                ticks.remove(TickType.RENDER);
093            }
094            // If we're enabled but we want clock ticks, or we're server side we get world ticks
095            if (enable && useClock) {
096                ticks.add(TickType.CLIENT);
097                ticks.add(TickType.WORLDLOAD);
098            } else {
099                ticks.remove(TickType.CLIENT);
100                ticks.remove(TickType.WORLDLOAD);
101            }
102        }
103    
104        public static IPacketHandler buildPacketHandlerFor(BaseModProxy mod)
105        {
106            return new ModLoaderPacketHandler(mod);
107        }
108    
109        public static IWorldGenerator buildWorldGenHelper(BaseModProxy mod)
110        {
111            return new ModLoaderWorldGenerator(mod);
112        }
113    
114        public static IFuelHandler buildFuelHelper(BaseModProxy mod)
115        {
116            return new ModLoaderFuelHelper(mod);
117        }
118    
119        public static ICraftingHandler buildCraftingHelper(BaseModProxy mod)
120        {
121            return new ModLoaderCraftingHelper(mod);
122        }
123    
124        public static void finishModLoading(ModLoaderModContainer mc)
125        {
126            if (sidedHelper != null)
127            {
128                sidedHelper.finishModLoading(mc);
129            }
130        }
131    
132        public static IConnectionHandler buildConnectionHelper(BaseModProxy mod)
133        {
134            return new ModLoaderConnectionHandler(mod);
135        }
136    
137        public static IPickupNotifier buildPickupHelper(BaseModProxy mod)
138        {
139            return new ModLoaderPickupNotifier(mod);
140        }
141    
142        public static void buildGuiHelper(BaseModProxy mod, int id)
143        {
144            ModLoaderGuiHelper handler = new ModLoaderGuiHelper(mod, id);
145            guiHelpers.put(id, handler);
146            NetworkRegistry.instance().registerGuiHandler(mod, handler);
147        }
148    
149        public static void openGui(int id, EntityPlayer player, Container container, int x, int y, int z)
150        {
151            ModLoaderGuiHelper helper = guiHelpers.get(id);
152            helper.injectContainer(container);
153            player.openGui(helper.getMod(), id, player.worldObj, x, y, z);
154        }
155    
156        public static Object getClientSideGui(BaseModProxy mod, EntityPlayer player, int ID, int x, int y, int z)
157        {
158            if (sidedHelper != null)
159            {
160                return sidedHelper.getClientGui(mod, player, ID, x, y, z);
161            }
162            return null;
163        }
164    
165        public static IDispenserHandler buildDispenseHelper(BaseModProxy mod)
166        {
167            return new ModLoaderDispenseHelper(mod);
168        }
169    
170    
171        public static void buildEntityTracker(BaseModProxy mod, Class<? extends Entity> entityClass, int entityTypeId, int updateRange, int updateInterval,
172                boolean sendVelocityInfo)
173        {
174            EntityRegistration er = EntityRegistry.registerModLoaderEntity(mod, entityClass, entityTypeId, updateRange, updateInterval, sendVelocityInfo);
175            er.setCustomSpawning(new ModLoaderEntitySpawnCallback(mod, er), EntityDragon.class.isAssignableFrom(entityClass) || IAnimals.class.isAssignableFrom(entityClass));
176        }
177    
178        private static ModLoaderVillageTradeHandler[] tradeHelpers = new ModLoaderVillageTradeHandler[6];
179    
180        public static void registerTrade(int profession, TradeEntry entry)
181        {
182            assert profession < tradeHelpers.length : "The profession is out of bounds";
183            if (tradeHelpers[profession] == null)
184            {
185                tradeHelpers[profession] = new ModLoaderVillageTradeHandler();
186                VillagerRegistry.instance().registerVillageTradeHandler(profession, tradeHelpers[profession]);
187            }
188    
189            tradeHelpers[profession].addTrade(entry);
190        }
191    
192        public static void addCommand(ICommand command)
193        {
194            ModLoaderModContainer mlmc = (ModLoaderModContainer) Loader.instance().activeModContainer();
195            if (mlmc!=null)
196            {
197                mlmc.addServerCommand(command);
198            }
199        }
200    }