    001package net.minecraftforge.common;
    002
    003import java.util.ArrayList;
    004import java.util.Random;
    005
    006import cpw.mods.fml.common.FMLLog;
    007
    008import net.minecraft.inventory.IInventory;
    009import net.minecraft.item.Item;
    010import net.minecraft.item.ItemStack;
    011import net.minecraft.util.WeightedRandom;
    012import net.minecraft.util.WeightedRandomChestContent;
    013import net.minecraft.util.WeightedRandomItem;
    014
    015import static net.minecraftforge.common.ChestGenHooks.DUNGEON_CHEST;
    016
    017public class DungeonHooks
    018{
    019    private static ArrayList<DungeonMob> dungeonMobs = new ArrayList<DungeonMob>();
    020
    021    /**
    022     * Adds a mob to the possible list of creatures the spawner will create.
    023     * If the mob is already in the spawn list, the rarity will be added to the existing one,
    024     * causing the mob to be more common.
    025     *
    026     * @param name The name of the monster, use the same name used when registering the entity.
    027     * @param rarity The rarity of selecting this mob over others. Must be greater then 0.
    028     *        Vanilla Minecraft has the following mobs:
    029     *        Spider   100
    030     *        Skeleton 100
    031     *        Zombie   200
    032     *        Meaning, Zombies are twice as common as spiders or skeletons.
    033     * @return The new rarity of the monster,
    034     */
    035    public static float addDungeonMob(String name, int rarity)
    036    {
    037        if (rarity <= 0)
    038        {
    039            throw new IllegalArgumentException("Rarity must be greater then zero");
    040        }
    041
    042        for (DungeonMob mob : dungeonMobs)
    043        {
    044            if (name.equals(mob.type))
    045            {
    046                return mob.itemWeight += rarity;
    047            }
    048        }
    049
    050        dungeonMobs.add(new DungeonMob(rarity, name));
    051        return rarity;
    052    }
    053
    054    /**
    055     * Will completely remove a Mob from the dungeon spawn list.
    056     *
    057     * @param name The name of the mob to remove
    058     * @return The rarity of the removed mob, prior to being removed.
    059     */
    060    public static int removeDungeonMob(String name)
    061    {
    062        for (DungeonMob mob : dungeonMobs)
    063        {
    064            if (name.equals(mob.type))
    065            {
    066                dungeonMobs.remove(mob);
    067                return mob.itemWeight;
    068            }
    069        }
    070        return 0;
    071    }
    072
    073    /**
    074     * Gets a random mob name from the list.
    075     * @param rand World generation random number generator
    076     * @return The mob name
    077     */
    078    public static String getRandomDungeonMob(Random rand)
    079    {
    080        DungeonMob mob = (DungeonMob)WeightedRandom.getRandomItem(rand, dungeonMobs);
    081        if (mob == null)
    082        {
    083            return "";
    084        }
    085        return mob.type;
    086    }
    087
    088
    089    public static class DungeonMob extends WeightedRandomItem
    090    {
    091        public String type;
    092        public DungeonMob(int weight, String type)
    093        {
    094            super(weight);
    095            this.type = type;
    096        }
    097
    098        @Override
    099        public boolean equals(Object target)
    100        {
    101            return target instanceof DungeonMob && type.equals(((DungeonMob)target).type);
    102        }
    103    }
    104
    105    static
    106    {
    107        addDungeonMob("Skeleton", 100);
    108        addDungeonMob("Zombie",   200);
    109        addDungeonMob("Spider",   100);
    110    }
    111}