001    package net.minecraftforge.common;
002    
003    import java.util.ArrayList;
004    import java.util.Random;
005    
006    import net.minecraft.item.Item;
007    import net.minecraft.item.ItemStack;
008    import net.minecraft.util.WeightedRandom;
009    import net.minecraft.util.WeightedRandomChestContent;
010    import net.minecraft.util.WeightedRandomItem;
011    
012    import static net.minecraftforge.common.ChestGenHooks.DUNGEON_CHEST;
013    
014    public class DungeonHooks
015    {
016        private static ArrayList<DungeonMob> dungeonMobs = new ArrayList<DungeonMob>();
017    
018        /**
019         * Adds a mob to the possible list of creatures the spawner will create.
020         * If the mob is already in the spawn list, the rarity will be added to the existing one,
021         * causing the mob to be more common.
022         *
023         * @param name The name of the monster, use the same name used when registering the entity.
024         * @param rarity The rarity of selecting this mob over others. Must be greater then 0.
025         *        Vanilla Minecraft has the following mobs:
026         *        Spider   100
027         *        Skeleton 100
028         *        Zombie   200
029         *        Meaning, Zombies are twice as common as spiders or skeletons.
030         * @return The new rarity of the monster,
031         */
032        public static float addDungeonMob(String name, int rarity)
033        {
034            if (rarity <= 0)
035            {
036                throw new IllegalArgumentException("Rarity must be greater then zero");
037            }
038    
039            for (DungeonMob mob : dungeonMobs)
040            {
041                if (name.equals(mob.type))
042                {
043                    return mob.itemWeight += rarity;
044                }
045            }
046    
047            dungeonMobs.add(new DungeonMob(rarity, name));
048            return rarity;
049        }
050    
051        /**
052         * Will completely remove a Mob from the dungeon spawn list.
053         *
054         * @param name The name of the mob to remove
055         * @return The rarity of the removed mob, prior to being removed.
056         */
057        public static int removeDungeonMob(String name)
058        {
059            for (DungeonMob mob : dungeonMobs)
060            {
061                if (name.equals(mob.type))
062                {
063                    dungeonMobs.remove(mob);
064                    return mob.itemWeight;
065                }
066            }
067            return 0;
068        }
069    
070        /**
071         * Gets a random mob name from the list.
072         * @param rand World generation random number generator
073         * @return The mob name
074         */
075        public static String getRandomDungeonMob(Random rand)
076        {
077            DungeonMob mob = (DungeonMob)WeightedRandom.getRandomItem(rand, dungeonMobs);
078            if (mob == null)
079            {
080                return "";
081            }
082            return mob.type;
083        }
084    
085    
086        public static class DungeonMob extends WeightedRandomItem
087        {
088            public String type;
089            public DungeonMob(int weight, String type)
090            {
091                super(weight);
092                this.type = type;
093            }
094    
095            @Override
096            public boolean equals(Object target)
097            {
098                if (target instanceof DungeonMob)
099                {
100                    return this.type.equals(((DungeonMob)target).type);
101                }
102                return false;
103            }
104        }
105    
106        static
107        {
108            addDungeonMob("Skeleton", 100);
109            addDungeonMob("Zombie",   200);
110            addDungeonMob("Spider",   100);
111        }
112    
113    
114        @Deprecated //Moved to ChestGenHooks
115        public static void setDungeonLootTries(int number)
116        {
117            ChestGenHooks.getInfo(DUNGEON_CHEST).setMax(number);
118            ChestGenHooks.getInfo(DUNGEON_CHEST).setMin(number);
119        }
120        @Deprecated //Moved to ChestGenHooks
121        public static int getDungeonLootTries() { return ChestGenHooks.getInfo(DUNGEON_CHEST).getMax(); }
122        @Deprecated //Moved to ChestGenHooks
123        public void addDungeonLoot(DungeonLoot loot){ ChestGenHooks.getInfo(DUNGEON_CHEST).addItem(loot); }
124        @Deprecated //Moved to ChestGenHooks
125        public boolean removeDungeonLoot(DungeonLoot loot){ return ChestGenHooks.getInfo(DUNGEON_CHEST).contents.remove(loot); }
126        @Deprecated //Moved to ChestGenHooks
127        public static void addDungeonLoot(ItemStack item, int rarity){ addDungeonLoot(item, rarity, 1, 1); }
128        @Deprecated //Moved to ChestGenHooks
129        public static float addDungeonLoot(ItemStack item, int rarity, int minCount, int maxCount)
130        {
131            ChestGenHooks.addDungeonLoot(ChestGenHooks.getInfo(DUNGEON_CHEST), item, rarity, minCount, maxCount);
132            return rarity;
133        }
134        @Deprecated //Moved to ChestGenHooks
135        public static void removeDungeonLoot(ItemStack item){ ChestGenHooks.removeItem(DUNGEON_CHEST, item); }
136        @Deprecated //Moved to ChestGenHooks
137        public static void removeDungeonLoot(ItemStack item, int minCount, int maxCount){ ChestGenHooks.removeItem(DUNGEON_CHEST, item); }
138        @Deprecated //Moved to ChestGenHooks
139        public static ItemStack getRandomDungeonLoot(Random rand){ return ChestGenHooks.getOneItem(DUNGEON_CHEST, rand); }
140    
141        @Deprecated //Moved to ChestGenHooks
142        public static class DungeonLoot extends WeightedRandomChestContent
143        {
144            @Deprecated
145            public DungeonLoot(int weight, ItemStack item, int min, int max)
146            {
147                super(item, weight, min, max);
148            }
149    
150            @Deprecated
151            public ItemStack generateStack(Random rand)
152            {
153                int min = theMinimumChanceToGenerateItem;
154                int max = theMaximumChanceToGenerateItem;
155                
156                ItemStack ret = this.theItemId.copy();
157                ret.stackSize = min + (rand.nextInt(max - min + 1));
158                return ret;
159            }
160    
161            public boolean equals(ItemStack item, int min, int max)
162            {
163                int minCount = theMinimumChanceToGenerateItem;
164                int maxCount = theMaximumChanceToGenerateItem;
165                return (min == minCount && max == maxCount && item.isItemEqual(theItemId) && ItemStack.areItemStackTagsEqual(item, theItemId));
166            }
167            public boolean equals(ItemStack item){ return item.isItemEqual(theItemId) && ItemStack.areItemStackTagsEqual(item, theItemId); }
168        }
169    }