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