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