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                return target instanceof DungeonMob && type.equals(((DungeonMob)target).type);
099            }
100        }
101    
102        static
103        {
104            addDungeonMob("Skeleton", 100);
105            addDungeonMob("Zombie",   200);
106            addDungeonMob("Spider",   100);
107        }
108    
109    
110        @Deprecated //Moved to ChestGenHooks
111        public static void setDungeonLootTries(int number)
112        {
113            ChestGenHooks.getInfo(DUNGEON_CHEST).setMax(number);
114            ChestGenHooks.getInfo(DUNGEON_CHEST).setMin(number);
115        }
116        @Deprecated //Moved to ChestGenHooks
117        public static int getDungeonLootTries() { return ChestGenHooks.getInfo(DUNGEON_CHEST).getMax(); }
118        @Deprecated //Moved to ChestGenHooks
119        public void addDungeonLoot(DungeonLoot loot){ ChestGenHooks.getInfo(DUNGEON_CHEST).addItem(loot); }
120        @Deprecated //Moved to ChestGenHooks
121        public boolean removeDungeonLoot(DungeonLoot loot){ return ChestGenHooks.getInfo(DUNGEON_CHEST).contents.remove(loot); }
122        @Deprecated //Moved to ChestGenHooks
123        public static void addDungeonLoot(ItemStack item, int rarity){ addDungeonLoot(item, rarity, 1, 1); }
124        @Deprecated //Moved to ChestGenHooks
125        public static float addDungeonLoot(ItemStack item, int rarity, int minCount, int maxCount)
126        {
127            ChestGenHooks.addDungeonLoot(ChestGenHooks.getInfo(DUNGEON_CHEST), item, rarity, minCount, maxCount);
128            return rarity;
129        }
130        @Deprecated //Moved to ChestGenHooks
131        public static void removeDungeonLoot(ItemStack item){ ChestGenHooks.removeItem(DUNGEON_CHEST, item); }
132        @Deprecated //Moved to ChestGenHooks
133        public static void removeDungeonLoot(ItemStack item, int minCount, int maxCount){ ChestGenHooks.removeItem(DUNGEON_CHEST, item); }
134        @Deprecated //Moved to ChestGenHooks
135        public static ItemStack getRandomDungeonLoot(Random rand){ return ChestGenHooks.getOneItem(DUNGEON_CHEST, rand); }
136    
137        @Deprecated //Moved to ChestGenHooks
138        public static class DungeonLoot extends WeightedRandomChestContent
139        {
140            @Deprecated
141            public DungeonLoot(int weight, ItemStack item, int min, int max)
142            {
143                super(item, weight, min, max);
144            }
145    
146            @Deprecated
147            public ItemStack generateStack(Random rand)
148            {
149                int min = theMinimumChanceToGenerateItem;
150                int max = theMaximumChanceToGenerateItem;
151                
152                ItemStack ret = this.theItemId.copy();
153                ret.stackSize = min + (rand.nextInt(max - min + 1));
154                return ret;
155            }
156    
157            public boolean equals(ItemStack item, int min, int max)
158            {
159                int minCount = theMinimumChanceToGenerateItem;
160                int maxCount = theMaximumChanceToGenerateItem;
161                return (min == minCount && max == maxCount && item.isItemEqual(theItemId) && ItemStack.areItemStackTagsEqual(item, theItemId));
162            }
163            public boolean equals(ItemStack item){ return item.isItemEqual(theItemId) && ItemStack.areItemStackTagsEqual(item, theItemId); }
164        }
165    }