001package net.minecraftforge.common;
002
003import java.lang.reflect.Constructor;
004import java.util.*;
005
006import cpw.mods.fml.common.FMLCommonHandler;
007import cpw.mods.fml.common.FMLLog;
008import cpw.mods.fml.common.Mod;
009import cpw.mods.fml.common.ModContainer;
010import cpw.mods.fml.relauncher.Side;
011import cpw.mods.fml.relauncher.SideOnly;
012
013import net.minecraft.block.Block;
014import net.minecraft.block.material.Material;
015import net.minecraft.client.renderer.texture.IconRegister;
016import net.minecraft.crash.CrashReport;
017import net.minecraft.entity.monster.EntityEnderman;
018import net.minecraft.item.Item;
019import net.minecraft.item.ItemStack;
020import net.minecraftforge.common.ForgeHooks.GrassEntry;
021import net.minecraftforge.common.ForgeHooks.SeedEntry;
022import net.minecraftforge.event.EventBus;
023import net.minecraftforge.event.ForgeSubscribe;
024import net.minecraftforge.event.entity.EntityEvent;
025import net.minecraftforge.oredict.OreDictionary;
026
027public class MinecraftForge
028{
029    /**
030     * The core Forge EventBusses, all events for Forge will be fired on these,
031     * you should use this to register all your listeners.
032     * This replaces every register*Handler() function in the old version of Forge.
033     * TERRAIN_GEN_BUS for terrain gen events
034     * ORE_GEN_BUS for ore gen events
035     * EVENT_BUS for everything else
036     */
037    public static final EventBus EVENT_BUS = new EventBus();
038    public static final EventBus TERRAIN_GEN_BUS = new EventBus();
039    public static final EventBus ORE_GEN_BUS = new EventBus();
040
041    private static final ForgeInternalHandler INTERNAL_HANDLER = new ForgeInternalHandler();
042
043
044    /** Register a new plant to be planted when bonemeal is used on grass.
045     * @param block The block to place.
046     * @param metadata The metadata to set for the block when being placed.
047     * @param weight The weight of the plant, where red flowers are
048     *               10 and yellow flowers are 20.
049     */
050    public static void addGrassPlant(Block block, int metadata, int weight)
051    {
052        ForgeHooks.grassList.add(new GrassEntry(block, metadata, weight));
053    }
054
055    /**
056     * Register a new seed to be dropped when breaking tall grass.
057     *
058     * @param seed The item to drop as a seed.
059     * @param weight The relative probability of the seeds,
060     *               where wheat seeds are 10.
061     */
062    public static void addGrassSeed(ItemStack seed, int weight)
063    {
064        ForgeHooks.seedList.add(new SeedEntry(seed, weight));
065    }
066
067    /**
068     *
069     * Register a tool as a tool class with a given harvest level.
070     *
071     * @param tool The custom tool to register.
072     * @param toolClass The tool class to register as.  The predefined tool
073     *                  clases are "pickaxe", "shovel", "axe".  You can add
074     *                  others for custom tools.
075     * @param harvestLevel The harvest level of the tool.
076     */
077   public static void setToolClass(Item tool, String toolClass, int harvestLevel)
078   {
079       ForgeHooks.toolClasses.put(tool, Arrays.asList(toolClass, harvestLevel));
080   }
081
082   /**
083    * Register a block to be harvested by a tool class.  This is the metadata
084    * sensitive version, use it if your blocks are using metadata variants.
085    * By default, this sets the block class as effective against that type.
086    *
087    * @param block The block to register.
088    * @param metadata The metadata for the block subtype.
089    * @param toolClass The tool class to register as able to remove this block.
090    *                  You may register the same block multiple times with different tool
091    *                  classes, if multiple tool types can be used to harvest this block.
092    * @param harvestLevel The minimum tool harvest level required to successfully
093    * harvest the block.
094    * @see MinecraftForge#setToolClass for details on tool classes.
095    */
096   public static void setBlockHarvestLevel(Block block, int metadata, String toolClass, int harvestLevel)
097   {
098       List key = Arrays.asList(block, metadata, toolClass);
099       ForgeHooks.toolHarvestLevels.put(key, harvestLevel);
100       ForgeHooks.toolEffectiveness.add(key);
101   }
102
103   /**
104    * Remove a block effectiveness mapping.  Since setBlockHarvestLevel
105    * makes the tool class effective against the block by default, this can be
106    * used to remove that mapping.  This will force a block to be harvested at
107    * the same speed regardless of tool quality, while still requiring a given
108    * harvesting level.
109    *
110    * @param block The block to remove effectiveness from.
111    * @param metadata The metadata for the block subtype.
112    * @param toolClass The tool class to remove the effectiveness mapping from.
113    * @see MinecraftForge#setToolClass for details on tool classes.
114    */
115   public static void removeBlockEffectiveness(Block block, int metadata, String toolClass)
116   {
117       List key = Arrays.asList(block, metadata, toolClass);
118       ForgeHooks.toolEffectiveness.remove(key);
119   }
120
121   /**
122    * Register a block to be harvested by a tool class.
123    * By default, this sets the block class as effective against that type.
124    *
125    * @param block The block to register.
126    * @param toolClass The tool class to register as able to remove this block.
127    *                  You may register the same block multiple times with different tool
128    *                  classes, if multiple tool types can be used to harvest this block.
129    * @param harvestLevel The minimum tool harvest level required to successfully
130    *                     harvest the block.
131    * @see MinecraftForge#setToolClass for details on tool classes.
132    */
133   public static void setBlockHarvestLevel(Block block, String toolClass, int harvestLevel)
134   {
135       for (int metadata = 0; metadata < 16; metadata++)
136       {
137           List key = Arrays.asList(block, metadata, toolClass);
138           ForgeHooks.toolHarvestLevels.put(key, harvestLevel);
139           ForgeHooks.toolEffectiveness.add(key);
140       }
141   }
142
143   /**
144    * Returns the block harvest level for a particular tool class.
145    *
146    * @param block The block to check.
147    * @param metadata The metadata for the block subtype.
148    * @param toolClass The tool class to check as able to remove this block.
149    * @see MinecraftForge#setToolClass for details on tool classes.
150    * @return The harvest level or -1 if no mapping exists.
151    */
152   public static int getBlockHarvestLevel(Block block, int metadata, String toolClass)
153   {
154       ForgeHooks.initTools();
155       List key = Arrays.asList(block, metadata, toolClass);
156       Integer harvestLevel = ForgeHooks.toolHarvestLevels.get(key);
157       return (harvestLevel == null ? -1 : harvestLevel);
158   }
159
160   /**
161    * Remove a block effectiveness mapping.  Since setBlockHarvestLevel
162    * makes the tool class effective against the block by default, this can be
163    * used to remove that mapping.  This will force a block to be harvested at
164    * the same speed regardless of tool quality, while still requiring a given
165    * harvesting level.
166    *
167    * @param block The block to remove effectiveness from.
168    * @param toolClass The tool class to remove the effectiveness mapping from.
169    * @see MinecraftForge#setToolClass for details on tool classes.
170    */
171   public static void removeBlockEffectiveness(Block block, String toolClass)
172   {
173       for (int metadata = 0; metadata < 16; metadata++)
174       {
175           List key = Arrays.asList(block, metadata, toolClass);
176           ForgeHooks.toolEffectiveness.remove(key);
177       }
178   }
179
180   /**
181    * Method invoked by FML before any other mods are loaded.
182    */
183   public static void initialize()
184   {
185       System.out.printf("MinecraftForge v%s Initialized\n", ForgeVersion.getVersion());
186       FMLLog.info("MinecraftForge v%s Initialized", ForgeVersion.getVersion());
187
188       Block filler = new Block(0, Material.air)
189       {
190           @SideOnly(Side.CLIENT) public void func_94332_a(IconRegister register){}
191       };
192       Block.blocksList[0] = null;
193       Block.opaqueCubeLookup[0] = false;
194       Block.lightOpacity[0] = 0;
195       filler.setUnlocalizedName("ForgeFiller");
196
197       for (int x = 256; x < 4096; x++)
198       {
199           if (Item.itemsList[x] != null)
200           {
201               Block.blocksList[x] = filler;
202           }
203       }
204
205       boolean[] temp = new boolean[4096];
206       System.arraycopy(EntityEnderman.carriableBlocks, 0, temp, 0, EntityEnderman.carriableBlocks.length);
207       EntityEnderman.carriableBlocks = temp;
208
209       EVENT_BUS.register(INTERNAL_HANDLER);
210       OreDictionary.getOreName(0);
211
212       //Force these classes to be defined, Should prevent derp error hiding.
213       new CrashReport("ThisIsFake", new Exception("Not real"));
214   }
215
216   public static String getBrandingVersion()
217   {
218       return "Minecraft Forge "+ ForgeVersion.getVersion();
219   }
220}