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
112
113    @Deprecated //Moved to ChestGenHooks
114    public static void setDungeonLootTries(int number)
115    {
116        ChestGenHooks.getInfo(DUNGEON_CHEST).setMax(number);
117        ChestGenHooks.getInfo(DUNGEON_CHEST).setMin(number);
118    }
119    @Deprecated //Moved to ChestGenHooks
120    public static int getDungeonLootTries() { return ChestGenHooks.getInfo(DUNGEON_CHEST).getMax(); }
121    @Deprecated //Moved to ChestGenHooks
122    public void addDungeonLoot(DungeonLoot loot){ ChestGenHooks.getInfo(DUNGEON_CHEST).addItem(loot); }
123    @Deprecated //Moved to ChestGenHooks
124    public boolean removeDungeonLoot(DungeonLoot loot){ return ChestGenHooks.getInfo(DUNGEON_CHEST).contents.remove(loot); }
125    @Deprecated //Moved to ChestGenHooks
126    public static void addDungeonLoot(ItemStack item, int rarity){ addDungeonLoot(item, rarity, 1, 1); }
127    @Deprecated //Moved to ChestGenHooks
128    public static float addDungeonLoot(ItemStack item, int rarity, int minCount, int maxCount)
129    {
130        ChestGenHooks.addDungeonLoot(ChestGenHooks.getInfo(DUNGEON_CHEST), item, rarity, minCount, maxCount);
131        return rarity;
132    }
133    @Deprecated //Moved to ChestGenHooks
134    public static void removeDungeonLoot(ItemStack item){ ChestGenHooks.removeItem(DUNGEON_CHEST, item); }
135    @Deprecated //Moved to ChestGenHooks
136    public static void removeDungeonLoot(ItemStack item, int minCount, int maxCount){ ChestGenHooks.removeItem(DUNGEON_CHEST, item); }
137    @Deprecated //Moved to ChestGenHooks
138    public static ItemStack getRandomDungeonLoot(Random rand){ return ChestGenHooks.getOneItem(DUNGEON_CHEST, rand); }
139
140    @Deprecated //Moved to ChestGenHooks
141    public static class DungeonLoot extends WeightedRandomChestContent
142    {
143        @Deprecated
144        public DungeonLoot(int weight, ItemStack item, int min, int max)
145        {
146            super(item, weight, min, max);
147        }
148
149        @Deprecated
150        public ItemStack generateStack(Random rand)
151        {
152            int min = theMinimumChanceToGenerateItem;
153            int max = theMaximumChanceToGenerateItem;
154
155            ItemStack ret = this.theItemId.copy();
156            ret.stackSize = min + (rand.nextInt(max - min + 1));
157            return ret;
158        }
159
160        @Override
161        protected final ItemStack[] generateChestContent(Random random, IInventory newInventory)
162        {
163            FMLLog.warning("Some mod is still using DungeonHooks.DungonLoot, tell them to stop! %s", this);
164            return new ItemStack[] { generateStack(random) };
165        }
166        public boolean equals(ItemStack item, int min, int max)
167        {
168            int minCount = theMinimumChanceToGenerateItem;
169            int maxCount = theMaximumChanceToGenerateItem;
170            return (min == minCount && max == maxCount && item.isItemEqual(theItemId) && ItemStack.areItemStackTagsEqual(item, theItemId));
171        }
172        public boolean equals(ItemStack item){ return item.isItemEqual(theItemId) && ItemStack.areItemStackTagsEqual(item, theItemId); }
173    }
174}