ShakeMob.java 8.5 KB

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