Taming.java 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. package com.gmail.nossr50.skills.taming;
  2. import java.util.Random;
  3. import org.bukkit.ChatColor;
  4. import org.bukkit.Material;
  5. import org.bukkit.entity.AnimalTamer;
  6. import org.bukkit.entity.Entity;
  7. import org.bukkit.entity.EntityType;
  8. import org.bukkit.entity.LivingEntity;
  9. import org.bukkit.entity.Ocelot;
  10. import org.bukkit.entity.Player;
  11. import org.bukkit.entity.Tameable;
  12. import org.bukkit.entity.Wolf;
  13. import org.bukkit.event.entity.EntityDamageByEntityEvent;
  14. import org.bukkit.event.entity.EntityDamageEvent;
  15. import org.bukkit.event.entity.EntityDamageEvent.DamageCause;
  16. import org.bukkit.inventory.ItemStack;
  17. import org.bukkit.metadata.FixedMetadataValue;
  18. import com.gmail.nossr50.mcMMO;
  19. import com.gmail.nossr50.config.Config;
  20. import com.gmail.nossr50.datatypes.SkillType;
  21. import com.gmail.nossr50.locale.LocaleLoader;
  22. import com.gmail.nossr50.util.Misc;
  23. import com.gmail.nossr50.util.Permissions;
  24. import com.gmail.nossr50.util.Users;
  25. public class Taming {
  26. public static final int FAST_FOOD_SERVICE_ACTIVATION_CHANCE = 50;
  27. public static final int FAST_FOOD_SERVICE_ACTIVATION_LEVEL = 50;
  28. public static final int GORE_BLEED_TICKS = 2;
  29. public static final int GORE_MAX_BONUS_LEVEL = 1000;
  30. public static final int GORE_MULTIPLIER = 2;
  31. public static final int SHARPENED_CLAWS_ACTIVATION_LEVEL = 750;
  32. public static final int SHARPENED_CLAWS_BONUS = 2;
  33. private static Random random = new Random();
  34. /**
  35. * Get the name of a tameable animal's owner.
  36. *
  37. * @param beast The animal whose owner's name to get
  38. * @return the name of the animal's owner, or "Offline Master" if the owner is offline
  39. */
  40. private static String getOwnerName(Tameable beast) {
  41. AnimalTamer tamer = beast.getOwner();
  42. if (tamer instanceof Player) {
  43. return ((Player) tamer).getName();
  44. }
  45. else {
  46. return "Offline Master";
  47. }
  48. }
  49. /**
  50. * Prevent damage to wolves based on various skills.
  51. *
  52. * @param event The event to modify
  53. */
  54. public static void preventDamage(EntityDamageEvent event) {
  55. final int ENVIRONMENTALLY_AWARE_LEVEL = 100;
  56. final int THICK_FUR_LEVEL = 250;
  57. final int SHOCK_PROOF_LEVEL = 500;
  58. final int THICK_FUR_MODIFIER = 2;
  59. final int SHOCK_PROOF_MODIFIER = 6;
  60. if (!(event.getEntity() instanceof Wolf)) {
  61. return;
  62. }
  63. DamageCause cause = event.getCause();
  64. Wolf wolf = (Wolf) event.getEntity();
  65. Player master = (Player) wolf.getOwner();
  66. int skillLevel = Users.getProfile(master).getSkillLevel(SkillType.TAMING);
  67. switch (cause) {
  68. /* Environmentally Aware */
  69. case CONTACT:
  70. case LAVA:
  71. case FIRE:
  72. if (Permissions.getInstance().environmentallyAware(master)) {
  73. if (skillLevel >= ENVIRONMENTALLY_AWARE_LEVEL) {
  74. if (event.getDamage() >= wolf.getHealth()) {
  75. return;
  76. }
  77. wolf.teleport(master.getLocation());
  78. master.sendMessage(LocaleLoader.getString("Taming.Listener.Wolf"));
  79. }
  80. }
  81. break;
  82. case FALL:
  83. if (Permissions.getInstance().environmentallyAware(master)) {
  84. if (skillLevel >= ENVIRONMENTALLY_AWARE_LEVEL) {
  85. event.setCancelled(true);
  86. }
  87. }
  88. break;
  89. /* Thick Fur */
  90. case FIRE_TICK:
  91. if (Permissions.getInstance().thickFur(master)) {
  92. if(skillLevel >= THICK_FUR_LEVEL) {
  93. wolf.setFireTicks(0);
  94. }
  95. }
  96. break;
  97. case ENTITY_ATTACK:
  98. case PROJECTILE:
  99. if (Permissions.getInstance().thickFur(master)) {
  100. if (skillLevel >= THICK_FUR_LEVEL) {
  101. event.setDamage(event.getDamage() / THICK_FUR_MODIFIER);
  102. }
  103. }
  104. break;
  105. /* Shock Proof */
  106. case ENTITY_EXPLOSION:
  107. case BLOCK_EXPLOSION:
  108. if (Permissions.getInstance().shockProof(master)) {
  109. if (skillLevel >= SHOCK_PROOF_LEVEL) {
  110. event.setDamage(event.getDamage() / SHOCK_PROOF_MODIFIER);
  111. }
  112. }
  113. break;
  114. default:
  115. break;
  116. }
  117. }
  118. /**
  119. * Summon an animal.
  120. *
  121. * @param type Type of animal to summon
  122. * @param player Player summoning the animal
  123. */
  124. public static void animalSummon(EntityType type, Player player, mcMMO plugin) {
  125. ItemStack item = player.getItemInHand();
  126. Material summonItem = null;
  127. int summonAmount = 0;
  128. switch (type) {
  129. case WOLF:
  130. summonItem = Material.BONE;
  131. summonAmount = Config.getInstance().getTamingCOTWWolfCost();
  132. break;
  133. case OCELOT:
  134. summonItem = Material.RAW_FISH;
  135. summonAmount = Config.getInstance().getTamingCOTWOcelotCost();
  136. break;
  137. default:
  138. break;
  139. }
  140. if (item.getType().equals(summonItem)) {
  141. if (item.getAmount() >= summonAmount) {
  142. for (Entity x : player.getNearbyEntities(40, 40, 40)) {
  143. if (x.getType().equals(type)) {
  144. switch (type) {
  145. case WOLF:
  146. player.sendMessage(LocaleLoader.getString("Taming.Summon.Fail.Wolf"));
  147. return;
  148. case OCELOT:
  149. player.sendMessage(LocaleLoader.getString("Taming.Summon.Fail.Ocelot"));
  150. return;
  151. default:
  152. return;
  153. }
  154. }
  155. }
  156. LivingEntity entity = player.getWorld().spawnCreature(player.getLocation(), type);
  157. entity.setMetadata("mcmmoSummoned", new FixedMetadataValue(plugin, true));
  158. ((Tameable) entity).setOwner(player);
  159. if (entity.getType().equals(EntityType.OCELOT)) {
  160. ((Ocelot) entity).setCatType(Ocelot.Type.getType(1 + random.nextInt(3)));
  161. }
  162. if (entity.getType().equals(EntityType.WOLF)) {
  163. entity.setHealth(entity.getMaxHealth());
  164. }
  165. player.setItemInHand(new ItemStack(summonItem, item.getAmount() - summonAmount));
  166. player.sendMessage(LocaleLoader.getString("Taming.Summon.Complete"));
  167. }
  168. else {
  169. player.sendMessage(LocaleLoader.getString("Skills.NeedMore") + " " + ChatColor.GRAY + Misc.prettyItemString(summonItem.getId()));
  170. }
  171. }
  172. }
  173. /**
  174. * Inspect a tameable animal for details.
  175. *
  176. * @param event Event to modify
  177. * @param target Animal to inspect
  178. * @param inspector Player inspecting the animal
  179. */
  180. public static void beastLore(EntityDamageByEntityEvent event, LivingEntity target, Player inspector) {
  181. if (target instanceof Tameable) {
  182. Tameable beast = (Tameable) target;
  183. String message = LocaleLoader.getString("Combat.BeastLore") + " ";
  184. int health = target.getHealth();
  185. event.setCancelled(true);
  186. if (beast.isTamed()) {
  187. message = message.concat(LocaleLoader.getString("Combat.BeastLoreOwner", new Object[] {getOwnerName(beast)}) + " ");
  188. }
  189. message = message.concat(LocaleLoader.getString("Combat.BeastLoreHealth", new Object[] {health, target.getMaxHealth()}));
  190. inspector.sendMessage(message);
  191. }
  192. }
  193. public static Random getRandom() {
  194. return random;
  195. }
  196. }