ShakeMob.java 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. package com.gmail.nossr50.skills.fishing;
  2. import java.util.HashMap;
  3. import java.util.Map;
  4. import java.util.Map.Entry;
  5. import org.bukkit.Material;
  6. import org.bukkit.entity.LivingEntity;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.entity.Sheep;
  9. import org.bukkit.entity.Skeleton;
  10. import org.bukkit.entity.Skeleton.SkeletonType;
  11. import org.bukkit.inventory.ItemStack;
  12. import org.bukkit.material.Wool;
  13. import org.bukkit.potion.Potion;
  14. import org.bukkit.potion.PotionType;
  15. import com.gmail.nossr50.skills.fishing.Fishing.Tier;
  16. import com.gmail.nossr50.skills.utilities.CombatTools;
  17. import com.gmail.nossr50.util.Misc;
  18. import com.gmail.nossr50.util.Permissions;
  19. public final class ShakeMob {
  20. private ShakeMob() {}
  21. /**
  22. * Begins Tree Feller
  23. *
  24. * @param player Player using Shake Mob
  25. * @param mob Targeted entity
  26. * @param skillLevel Fishing level of the player
  27. */
  28. public static void process(Player player, LivingEntity mob, int skillLevel) {
  29. int activationChance = Misc.calculateActivationChance(Permissions.luckyFishing(player));
  30. if (getShakeProbability(skillLevel) <= Misc.getRandom().nextInt(activationChance)) {
  31. return;
  32. }
  33. Map<ItemStack, Integer> possibleDrops = new HashMap<ItemStack, Integer>();
  34. findPossibleDrops(mob, possibleDrops);
  35. if (possibleDrops.isEmpty()) {
  36. return;
  37. }
  38. ItemStack drop = chooseDrop(possibleDrops);
  39. // It's possible that chooseDrop returns null if the sum of probability in possibleDrops is inferior than 100
  40. if (drop == null) {
  41. return;
  42. }
  43. // Extra processing depending on the mob and drop type
  44. switch (mob.getType()) {
  45. case SHEEP:
  46. Sheep sheep = (Sheep) mob;
  47. if (drop.getType() == Material.WOOL) {
  48. if (sheep.isSheared()) {
  49. return;
  50. }
  51. // TODO: Find a cleaner way to do this, maybe by using Sheep.getColor().getWoolData() (available since 1.4.7-R0.1)
  52. Wool wool = (Wool) drop.getData();
  53. wool.setColor(sheep.getColor());
  54. drop.setDurability(wool.getData());
  55. sheep.setSheared(true);
  56. }
  57. break;
  58. case SKELETON:
  59. Skeleton skeleton = (Skeleton) mob;
  60. if (skeleton.getSkeletonType() == SkeletonType.WITHER) {
  61. switch (drop.getType()) {
  62. case SKULL_ITEM:
  63. drop.setDurability((short) 1);
  64. break;
  65. case ARROW:
  66. drop.setType(Material.COAL);
  67. break;
  68. default:
  69. break;
  70. }
  71. }
  72. default:
  73. break;
  74. }
  75. Misc.dropItem(mob.getLocation(), drop);
  76. CombatTools.dealDamage(mob, 1); // We may want to base the damage on the entity max health
  77. }
  78. /**
  79. * Finds the possible drops of an entity
  80. *
  81. * @param mob Targeted entity
  82. * @param possibleDrops List of ItemStack that can be dropped
  83. */
  84. private static void findPossibleDrops(LivingEntity mob, Map<ItemStack, Integer> possibleDrops) {
  85. switch (mob.getType()) {
  86. case BLAZE:
  87. possibleDrops.put(new ItemStack(Material.BLAZE_ROD), 100);
  88. break;
  89. case CAVE_SPIDER:
  90. case SPIDER:
  91. possibleDrops.put(new ItemStack(Material.SPIDER_EYE), 50);
  92. possibleDrops.put(new ItemStack(Material.STRING), 50);
  93. break;
  94. case CHICKEN:
  95. possibleDrops.put(new ItemStack(Material.FEATHER), 34);
  96. possibleDrops.put(new ItemStack(Material.RAW_CHICKEN), 33);
  97. possibleDrops.put(new ItemStack(Material.EGG), 33);
  98. break;
  99. case COW:
  100. possibleDrops.put(new ItemStack(Material.MILK_BUCKET), 2);
  101. possibleDrops.put(new ItemStack(Material.LEATHER), 49);
  102. possibleDrops.put(new ItemStack(Material.RAW_BEEF), 49);
  103. break;
  104. case CREEPER:
  105. possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 4), 1);
  106. possibleDrops.put(new ItemStack(Material.SULPHUR), 99);
  107. break;
  108. case ENDERMAN:
  109. possibleDrops.put(new ItemStack(Material.ENDER_PEARL), 100);
  110. break;
  111. case GHAST:
  112. possibleDrops.put(new ItemStack(Material.SULPHUR), 50);
  113. possibleDrops.put(new ItemStack(Material.GHAST_TEAR), 50);
  114. break;
  115. case IRON_GOLEM:
  116. possibleDrops.put(new ItemStack(Material.PUMPKIN), 3);
  117. possibleDrops.put(new ItemStack(Material.IRON_INGOT), 12);
  118. possibleDrops.put(new ItemStack(Material.RED_ROSE), 85);
  119. break;
  120. case MAGMA_CUBE:
  121. possibleDrops.put(new ItemStack(Material.MAGMA_CREAM), 100);
  122. break;
  123. case MUSHROOM_COW:
  124. possibleDrops.put(new ItemStack(Material.MILK_BUCKET), 5);
  125. possibleDrops.put(new ItemStack(Material.MUSHROOM_SOUP), 5);
  126. possibleDrops.put(new ItemStack(Material.LEATHER), 30);
  127. possibleDrops.put(new ItemStack(Material.RAW_BEEF), 30);
  128. possibleDrops.put(new ItemStack(Material.RED_MUSHROOM, Misc.getRandom().nextInt(3) + 1), 30);
  129. break;
  130. case PIG:
  131. possibleDrops.put(new ItemStack(Material.PORK), 100);
  132. break;
  133. case PIG_ZOMBIE:
  134. possibleDrops.put(new ItemStack(Material.ROTTEN_FLESH), 50);
  135. possibleDrops.put(new ItemStack(Material.GOLD_NUGGET), 50);
  136. break;
  137. case SHEEP:
  138. possibleDrops.put(new ItemStack(Material.WOOL, Misc.getRandom().nextInt(6) + 1), 100);
  139. break;
  140. case SKELETON:
  141. possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 0), 2);
  142. possibleDrops.put(new ItemStack(Material.BONE), 49);
  143. possibleDrops.put(new ItemStack(Material.ARROW, Misc.getRandom().nextInt(3) + 1), 49);
  144. break;
  145. case SLIME:
  146. possibleDrops.put(new ItemStack(Material.SLIME_BALL), 100);
  147. break;
  148. case SNOWMAN:
  149. possibleDrops.put(new ItemStack(Material.PUMPKIN), 3);
  150. possibleDrops.put(new ItemStack(Material.SNOW_BALL, Misc.getRandom().nextInt(4) + 1), 97);
  151. break;
  152. case SQUID:
  153. possibleDrops.put(new ItemStack(Material.INK_SACK), 100); // TODO: Add DyeColor.BLACK.getDyeData() to make it more explicit (available since 1.4.7-R0.1)
  154. break;
  155. case WITCH:
  156. possibleDrops.put(new Potion(PotionType.INSTANT_HEAL).toItemStack(1), 1);
  157. possibleDrops.put(new Potion(PotionType.FIRE_RESISTANCE).toItemStack(1), 1);
  158. possibleDrops.put(new Potion(PotionType.SPEED).toItemStack(1), 1);
  159. possibleDrops.put(new ItemStack(Material.GLASS_BOTTLE), 9);
  160. possibleDrops.put(new ItemStack(Material.GLOWSTONE_DUST), 13);
  161. possibleDrops.put(new ItemStack(Material.SULPHUR), 12);
  162. possibleDrops.put(new ItemStack(Material.REDSTONE), 13);
  163. possibleDrops.put(new ItemStack(Material.SPIDER_EYE), 12);
  164. possibleDrops.put(new ItemStack(Material.STICK), 13);
  165. possibleDrops.put(new ItemStack(Material.SUGAR), 12);
  166. possibleDrops.put(new ItemStack(Material.POTION), 13);
  167. break;
  168. case ZOMBIE:
  169. possibleDrops.put(new ItemStack(Material.SKULL_ITEM, 1, (short) 2), 2);
  170. possibleDrops.put(new ItemStack(Material.ROTTEN_FLESH), 98);
  171. break;
  172. default:
  173. return;
  174. }
  175. }
  176. /**
  177. * Randomly chooses a drop among the list
  178. *
  179. * @param possibleDrops List of ItemStack that can be dropped
  180. * @return Chosen ItemStack
  181. */
  182. private static ItemStack chooseDrop(Map<ItemStack, Integer> possibleDrops) {
  183. int dropProbability = Misc.getRandom().nextInt(100);
  184. int cumulatedProbability = 0;
  185. for (Entry<ItemStack, Integer> entry : possibleDrops.entrySet()) {
  186. cumulatedProbability += entry.getValue();
  187. if (dropProbability < cumulatedProbability) {
  188. return entry.getKey();
  189. }
  190. }
  191. return null;
  192. }
  193. /**
  194. * Gets the Shake Mob probability for a given skill level
  195. *
  196. * @param skillLevel Fishing skill level
  197. * @return Shake Mob probability
  198. */
  199. public static int getShakeProbability(int skillLevel) {
  200. for (Tier tier : Tier.values()) {
  201. if (skillLevel >= tier.getLevel()) {
  202. return tier.getShakeChance();
  203. }
  204. }
  205. return 0;
  206. }
  207. }