LexManos

Minecraft Forge Dungeon Hook

Dec 19th, 2011
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.42 KB | None | 0 0
  1.     private static int dungeonLootAttempts = 8;
  2.     private static ArrayList<ObjectPair<Float, String>> dungeonMobs = new ArrayList<ObjectPair<Float, String>>();
  3.     private static ArrayList<ObjectPair<Float, DungeonLoot>> dungeonLoot = new ArrayList<ObjectPair<Float, DungeonLoot>>();
  4.     /**
  5.      * Set the number of item stacks that will be attempted to be added to each Dungeon chest.
  6.      * Note: Due to random number generation, you will not always get this amount per chest.
  7.      * @param number The maximum number of item stacks to add to a chest.
  8.      */
  9.     public static void setDungeonLootTries(int number)
  10.     {
  11.         dungeonLootAttempts = number;
  12.     }
  13.    
  14.     /**
  15.      * @return The max number of item stacks found in each dungeon chest.
  16.      */
  17.     public static int getDungeonLootTries()
  18.     {
  19.         return dungeonLootAttempts;
  20.     }
  21.    
  22.     /**
  23.      * Adds a mob to the possible list of creatures the spawner will create.
  24.      * If the mob is already in the spawn list, the rarity will be added to the existing one,
  25.      * causing the mob to be more common.
  26.      *  
  27.      * @param name The name of the monster, use the same name used when registering the entity.
  28.      * @param rarity The rarity of selecting this mob over others. Must be greater then 0.
  29.      *        Vanilla Minecraft has the following mobs:
  30.      *        Spider   1
  31.      *        Skeleton 1
  32.      *        Zombie   2
  33.      *        Meaning, Zombies are twice as common as spiders or skeletons.
  34.      * @return The new rarity of the monster,        
  35.      */
  36.     public static float addDungeonMob(String name, float rarity)
  37.     {
  38.         if (rarity <= 0)
  39.         {
  40.             throw new IllegalArgumentException("Rarity must be greater then zero");
  41.         }
  42.        
  43.         for(ObjectPair<Float, String> mob : dungeonMobs)
  44.         {
  45.             if (name.equals(mob.getValue2()))
  46.             {
  47.                 mob.setValue1(mob.getValue1() + rarity);
  48.                 return mob.getValue1();
  49.             }
  50.         }
  51.  
  52.         dungeonMobs.add(new ObjectPair<Float, String>(rarity, name));
  53.         return rarity;
  54.     }
  55.    
  56.     /**
  57.      * Will completely remove a Mob from the dungeon spawn list.
  58.      *
  59.      * @param name The name of the mob to remove
  60.      * @return The rarity of the removed mob, prior to being removed.
  61.      */
  62.     public static float removeDungeonMob(String name)
  63.     {
  64.         for (ObjectPair<Float, String> mob : dungeonMobs)
  65.         {
  66.             if (name.equals(name))
  67.             {
  68.                 dungeonMobs.remove(mob);
  69.                 return mob.getValue1();
  70.             }
  71.         }
  72.         return 0;
  73.     }
  74.    
  75.     /**
  76.      * Gets a random mob name from the list.
  77.      * @param rand World generation random number generator
  78.      * @return The mob name
  79.      */
  80.     public static String getRandomDungeonMob(Random rand)
  81.     {
  82.         float maxRarity = 0f;
  83.         for (ObjectPair<Float, String> mob : dungeonMobs)
  84.         {
  85.             maxRarity += mob.getValue1();
  86.         }
  87.        
  88.         float targetRarity = rand.nextFloat() * maxRarity;
  89.         for (ObjectPair<Float, String> mob : dungeonMobs)
  90.         {
  91.             if (targetRarity < mob.getValue1())
  92.             {
  93.                 return mob.getValue2();
  94.             }
  95.             targetRarity -= mob.getValue1();
  96.         }
  97.        
  98.         return "";
  99.     }
  100.    
  101.     /**
  102.      * Adds a item stack to the dungeon loot list with a stack size
  103.      * of 1.
  104.      *
  105.      * @param item The ItemStack to be added to the loot list
  106.      * @param rarity The relative chance that this item will spawn, Vanilla has
  107.      *          most of its items set to 1. Like the saddle, bread, silk, wheat, etc..
  108.      *          Rarer items are set to lower values, EXA: Golden Apple 0.01
  109.      */
  110.     public static void addDungeonLoot(ItemStack item, float rarity)
  111.     {
  112.         addDungeonLoot(item, rarity, 1, 1);
  113.     }
  114.    
  115.     /**
  116.      * Adds a item stack, with a range of sizes, to the dungeon loot list.
  117.      * If a stack matching the same item, and size range, is already in the list
  118.      * the rarities will be added together making the item more common.
  119.      *
  120.      * @param item The ItemStack to be added to the loot list
  121.      * @param rarity The relative chance that this item will spawn, Vanilla has
  122.      *          most of its items set to 1. Like the saddle, bread, silk, wheat, etc..
  123.      *          Rarer items are set to lower values, EXA: Golden Apple 0.01
  124.      * @param minCount When this item does generate, the minimum number that is in the stack
  125.      * @param maxCount When this item does generate, the maximum number that can bein the stack
  126.      * @return The new rarity of the loot.
  127.      */
  128.     public static float addDungeonLoot(ItemStack item, float rarity, int minCount, int maxCount)
  129.     {
  130.         for (ObjectPair<Float, DungeonLoot> loot : dungeonLoot)
  131.         {
  132.             if (loot.getValue2().equals(item, minCount, maxCount))
  133.             {
  134.                 loot.setValue1(loot.getValue1() + rarity);
  135.                 return loot.getValue1();
  136.             }
  137.         }
  138.        
  139.         dungeonLoot.add(new ObjectPair<Float, DungeonLoot>(rarity, new DungeonLoot(item, minCount, maxCount)));
  140.         return rarity;     
  141.     }
  142.     /**
  143.      * Removes a item stack from the dungeon loot list, this will remove all items
  144.      * as long as the item stack matches, it will not care about matching the stack
  145.      * size ranges perfectly.
  146.      *
  147.      * @param item The item stack to remove
  148.      * @return The total rarity of all items removed
  149.      */
  150.     public static float removeDungeonLoot(ItemStack item)
  151.     {
  152.         return removeDungeonLoot(item, -1, 0);
  153.     }
  154.    
  155.     /**
  156.      * Removes a item stack from the dungeon loot list. If 'minCount' parameter
  157.      * is greater then 0, it will only remove loot items that have the same exact
  158.      * stack size range as passed in by parameters.
  159.      *
  160.      * @param item The item stack to remove
  161.      * @param minCount The minimum count for the match check, if less then 0,
  162.      *          the size check is skipped
  163.      * @param maxCount The max count used in match check when 'minCount' is >= 0
  164.      * @return The total rarity of all items removed
  165.      */
  166.     public static float removeDungeonLoot(ItemStack item, int minCount, int maxCount)
  167.     {
  168.         float rarity = 0;
  169.         ArrayList<ObjectPair<Float, DungeonLoot>> lootTmp = (ArrayList<ObjectPair<Float, DungeonLoot>>)dungeonLoot.clone();
  170.         if (minCount < 0)
  171.         {
  172.             for (ObjectPair<Float, DungeonLoot> loot : lootTmp)
  173.             {
  174.                 if (loot.getValue2().equals(item))
  175.                 {
  176.                     dungeonLoot.remove(loot);
  177.                     rarity += loot.getValue1();
  178.                 }
  179.             }
  180.         }
  181.         else
  182.         {
  183.             for (ObjectPair<Float, DungeonLoot> loot : lootTmp)
  184.             {
  185.                 if (loot.getValue2().equals(item, minCount, maxCount))
  186.                 {
  187.                     dungeonLoot.remove(loot);
  188.                     rarity += loot.getValue1();
  189.                 }
  190.             }
  191.         }
  192.        
  193.         return rarity;  
  194.     }
  195.    
  196.  
  197.     /**
  198.      * Gets a random item stack to place in a dungeon chest during world generation
  199.      * @param rand World generation random number generator
  200.      * @return The item stack
  201.      */
  202.     public static ItemStack getRandomDungeonLoot(Random rand)
  203.     {
  204.         float maxRarity = 0f;
  205.         for (ObjectPair<Float, DungeonLoot> loot : dungeonLoot)
  206.         {
  207.             maxRarity += loot.getValue1();
  208.         }
  209.        
  210.         float targetRarity = rand.nextFloat() * maxRarity;
  211.         for (ObjectPair<Float, DungeonLoot> loot : dungeonLoot)
  212.         {
  213.             if (targetRarity < loot.getValue1())
  214.             {
  215.                 return loot.getValue2().generateStack(rand);
  216.             }
  217.             targetRarity -= loot.getValue1();
  218.         }
  219.        
  220.         return null;
  221.     }
  222.        
  223.     static
  224.     {
  225.         addDungeonMob("Skeleton", 1.0f);
  226.         addDungeonMob("Zombie",   2.0f);
  227.         addDungeonMob("Spider",   1.0f);
  228.        
  229.         addDungeonLoot(new ItemStack(Item.saddle),          1.00f      );
  230.         addDungeonLoot(new ItemStack(Item.ingotIron),       1.00f, 1, 4);
  231.         addDungeonLoot(new ItemStack(Item.bread),           1.00f      );
  232.         addDungeonLoot(new ItemStack(Item.wheat),           1.00f, 1, 4);
  233.         addDungeonLoot(new ItemStack(Item.gunpowder),       1.00f, 1, 4);
  234.         addDungeonLoot(new ItemStack(Item.silk),            1.00f, 1, 4);
  235.         addDungeonLoot(new ItemStack(Item.bucketEmpty),     1.00f      );
  236.         addDungeonLoot(new ItemStack(Item.appleGold),       0.01f      );
  237.         addDungeonLoot(new ItemStack(Item.redstone),        0.50f, 1, 4);
  238.         addDungeonLoot(new ItemStack(Item.record13),        0.05f      );
  239.         addDungeonLoot(new ItemStack(Item.recordCat),       0.05f      );
  240.         addDungeonLoot(new ItemStack(Item.dyePowder, 1, 3), 1.00f      );
  241.     }
Advertisement
Add Comment
Please, Sign In to add comment