TamingManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. package com.gmail.nossr50.skills.taming;
  2. import java.util.ArrayList;
  3. import java.util.HashMap;
  4. import java.util.List;
  5. import org.bukkit.Location;
  6. import org.bukkit.Sound;
  7. import org.bukkit.entity.Entity;
  8. import org.bukkit.entity.EntityType;
  9. import org.bukkit.entity.Horse;
  10. import org.bukkit.entity.LivingEntity;
  11. import org.bukkit.entity.Ocelot;
  12. import org.bukkit.entity.Player;
  13. import org.bukkit.entity.Tameable;
  14. import org.bukkit.entity.Wolf;
  15. import org.bukkit.inventory.ItemStack;
  16. import com.gmail.nossr50.mcMMO;
  17. import com.gmail.nossr50.config.AdvancedConfig;
  18. import com.gmail.nossr50.config.Config;
  19. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  20. import com.gmail.nossr50.datatypes.skills.SecondaryAbility;
  21. import com.gmail.nossr50.datatypes.skills.SkillType;
  22. import com.gmail.nossr50.datatypes.skills.XPGainReason;
  23. import com.gmail.nossr50.events.fake.FakeEntityTameEvent;
  24. import com.gmail.nossr50.events.skills.secondaryabilities.SecondaryAbilityWeightedActivationCheckEvent;
  25. import com.gmail.nossr50.locale.LocaleLoader;
  26. import com.gmail.nossr50.runnables.skills.BleedTimerTask;
  27. import com.gmail.nossr50.skills.SkillManager;
  28. import com.gmail.nossr50.util.Misc;
  29. import com.gmail.nossr50.util.Permissions;
  30. import com.gmail.nossr50.util.StringUtils;
  31. import com.gmail.nossr50.util.player.UserManager;
  32. import com.gmail.nossr50.util.skills.ParticleEffectUtils;
  33. import com.gmail.nossr50.util.skills.SkillUtils;
  34. public class TamingManager extends SkillManager {
  35. public TamingManager(McMMOPlayer mcMMOPlayer) {
  36. super(mcMMOPlayer, SkillType.TAMING);
  37. }
  38. private static HashMap<EntityType, List<TrackedTamingEntity>> summonedEntities = new HashMap<EntityType, List<TrackedTamingEntity>>();
  39. public boolean canUseThickFur() {
  40. return getSkillLevel() >= Taming.thickFurUnlockLevel && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.THICK_FUR);
  41. }
  42. public boolean canUseEnvironmentallyAware() {
  43. return getSkillLevel() >= Taming.environmentallyAwareUnlockLevel && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.ENVIROMENTALLY_AWARE);
  44. }
  45. public boolean canUseShockProof() {
  46. return getSkillLevel() >= Taming.shockProofUnlockLevel && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.SHOCK_PROOF);
  47. }
  48. public boolean canUseHolyHound() {
  49. return getSkillLevel() >= Taming.holyHoundUnlockLevel && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.HOLY_HOUND);
  50. }
  51. public boolean canUseFastFoodService() {
  52. return getSkillLevel() >= Taming.fastFoodServiceUnlockLevel && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.FAST_FOOD);
  53. }
  54. public boolean canUseSharpenedClaws() {
  55. return getSkillLevel() >= Taming.sharpenedClawsUnlockLevel && Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.SHARPENED_CLAWS);
  56. }
  57. public boolean canUseGore() {
  58. return Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.GORE);
  59. }
  60. public boolean canUseBeastLore() {
  61. return Permissions.secondaryAbilityEnabled(getPlayer(), SecondaryAbility.BEAST_LORE);
  62. }
  63. /**
  64. * Award XP for taming.
  65. *
  66. * @param entity The LivingEntity to award XP for
  67. */
  68. public void awardTamingXP(LivingEntity entity) {
  69. switch (entity.getType()) {
  70. case HORSE:
  71. applyXpGain(Taming.horseXp, XPGainReason.PVE);
  72. return;
  73. case WOLF:
  74. applyXpGain(Taming.wolfXp, XPGainReason.PVE);
  75. return;
  76. case OCELOT:
  77. applyXpGain(Taming.ocelotXp, XPGainReason.PVE);
  78. return;
  79. default:
  80. return;
  81. }
  82. }
  83. /**
  84. * Apply the Fast Food Service ability.
  85. *
  86. * @param wolf The wolf using the ability
  87. * @param damage The damage being absorbed by the wolf
  88. */
  89. public void fastFoodService(Wolf wolf, double damage) {
  90. if (!SkillUtils.activationSuccessful(SecondaryAbility.FAST_FOOD, getPlayer(), Taming.fastFoodServiceActivationChance, activationChance)) {
  91. return;
  92. }
  93. double health = wolf.getHealth();
  94. double maxHealth = wolf.getMaxHealth();
  95. if (health < maxHealth) {
  96. double newHealth = health + damage;
  97. wolf.setHealth(Math.min(newHealth, maxHealth));
  98. }
  99. }
  100. /**
  101. * Apply the Gore ability.
  102. *
  103. * @param target The LivingEntity to apply Gore on
  104. * @param damage The initial damage
  105. */
  106. public double gore(LivingEntity target, double damage) {
  107. if (!SkillUtils.activationSuccessful(SecondaryAbility.GORE, getPlayer(), getSkillLevel(), activationChance)) {
  108. return 0;
  109. }
  110. BleedTimerTask.add(target, Taming.goreBleedTicks);
  111. if (target instanceof Player) {
  112. ((Player) target).sendMessage(LocaleLoader.getString("Combat.StruckByGore"));
  113. }
  114. getPlayer().sendMessage(LocaleLoader.getString("Combat.Gore"));
  115. damage = (damage * Taming.goreModifier) - damage;
  116. return damage;
  117. }
  118. public double sharpenedClaws() {
  119. return Taming.sharpenedClawsBonusDamage;
  120. }
  121. /**
  122. * Summon an ocelot to your side.
  123. */
  124. public void summonOcelot() {
  125. if (!Permissions.callOfTheWild(getPlayer(), EntityType.OCELOT)) {
  126. return;
  127. }
  128. callOfTheWild(EntityType.OCELOT, Config.getInstance().getTamingCOTWCost(EntityType.OCELOT));
  129. }
  130. /**
  131. * Summon a wolf to your side.
  132. */
  133. public void summonWolf() {
  134. if (!Permissions.callOfTheWild(getPlayer(), EntityType.WOLF)) {
  135. return;
  136. }
  137. callOfTheWild(EntityType.WOLF, Config.getInstance().getTamingCOTWCost(EntityType.WOLF));
  138. }
  139. /**
  140. * Summon a horse to your side.
  141. */
  142. public void summonHorse() {
  143. if (!Permissions.callOfTheWild(getPlayer(), EntityType.HORSE)) {
  144. return;
  145. }
  146. callOfTheWild(EntityType.HORSE, Config.getInstance().getTamingCOTWCost(EntityType.HORSE));
  147. }
  148. /**
  149. * Handle the Beast Lore ability.
  150. *
  151. * @param target The entity to examine
  152. */
  153. public void beastLore(LivingEntity target) {
  154. Player player = getPlayer();
  155. Tameable beast = (Tameable) target;
  156. String message = LocaleLoader.getString("Combat.BeastLore") + " ";
  157. if (beast.isTamed() && beast.getOwner() != null) {
  158. message = message.concat(LocaleLoader.getString("Combat.BeastLoreOwner", beast.getOwner().getName()) + " ");
  159. }
  160. message = message.concat(LocaleLoader.getString("Combat.BeastLoreHealth", target.getHealth(), target.getMaxHealth()));
  161. player.sendMessage(message);
  162. }
  163. public void processEnvironmentallyAware(Wolf wolf, double damage) {
  164. if (damage > wolf.getHealth()) {
  165. return;
  166. }
  167. Player owner = getPlayer();
  168. wolf.teleport(owner);
  169. owner.sendMessage(LocaleLoader.getString("Taming.Listener.Wolf"));
  170. }
  171. public void pummel(LivingEntity target, Wolf wolf) {
  172. double chance = 10 / activationChance;
  173. SecondaryAbilityWeightedActivationCheckEvent event = new SecondaryAbilityWeightedActivationCheckEvent(getPlayer(), SecondaryAbility.PUMMEL, chance);
  174. mcMMO.p.getServer().getPluginManager().callEvent(event);
  175. if ((event.getChance() * activationChance) <= Misc.getRandom().nextInt(activationChance)) {
  176. return;
  177. }
  178. ParticleEffectUtils.playGreaterImpactEffect(target);
  179. target.setVelocity(wolf.getLocation().getDirection().normalize().multiply(1.5D));
  180. if (target instanceof Player) {
  181. Player defender = (Player) target;
  182. if (UserManager.getPlayer(defender).useChatNotifications()) {
  183. defender.sendMessage("Wolf pummeled at you");
  184. }
  185. }
  186. }
  187. public void attackTarget(LivingEntity target) {
  188. double range = 5;
  189. Player player = getPlayer();
  190. for (Entity entity : player.getNearbyEntities(range, range, range)) {
  191. if (entity.getType() != EntityType.WOLF) {
  192. continue;
  193. }
  194. Wolf wolf = (Wolf) entity;
  195. if (!wolf.isTamed() || (wolf.getOwner() != player) || wolf.isSitting()) {
  196. continue;
  197. }
  198. wolf.setTarget(target);
  199. }
  200. }
  201. /**
  202. * Handle the Call of the Wild ability.
  203. *
  204. * @param type The type of entity to summon.
  205. * @param summonAmount The amount of material needed to summon the entity
  206. */
  207. private void callOfTheWild(EntityType type, int summonAmount) {
  208. Player player = getPlayer();
  209. ItemStack heldItem = player.getItemInHand();
  210. int heldItemAmount = heldItem.getAmount();
  211. Location location = player.getLocation();
  212. if (heldItemAmount < summonAmount) {
  213. player.sendMessage(LocaleLoader.getString("Skills.NeedMore", StringUtils.getPrettyItemString(heldItem.getType())));
  214. return;
  215. }
  216. if (!rangeCheck(type)) {
  217. return;
  218. }
  219. int amount = Config.getInstance().getTamingCOTWAmount(type);
  220. int tamingCOTWLength = Config.getInstance().getTamingCOTWLength(type);
  221. for (int i = 0; i < amount; i++) {
  222. if (!summonAmountCheck(type)) {
  223. return;
  224. }
  225. location = Misc.getLocationOffset(location, 1);
  226. LivingEntity entity = (LivingEntity) player.getWorld().spawnEntity(location, type);
  227. FakeEntityTameEvent event = new FakeEntityTameEvent(entity, player);
  228. mcMMO.p.getServer().getPluginManager().callEvent(event);
  229. if (event.isCancelled()) {
  230. continue;
  231. }
  232. entity.setMetadata(mcMMO.entityMetadataKey, mcMMO.metadataValue);
  233. ((Tameable) entity).setOwner(player);
  234. entity.setRemoveWhenFarAway(false);
  235. addToTracker(entity);
  236. switch (type) {
  237. case OCELOT:
  238. ((Ocelot) entity).setCatType(Ocelot.Type.values()[1 + Misc.getRandom().nextInt(3)]);
  239. break;
  240. case WOLF:
  241. entity.setMaxHealth(20.0);
  242. entity.setHealth(entity.getMaxHealth());
  243. break;
  244. case HORSE:
  245. Horse horse = (Horse) entity;
  246. entity.setMaxHealth(15.0 + (Misc.getRandom().nextDouble() * 15));
  247. entity.setHealth(entity.getMaxHealth());
  248. horse.setColor(Horse.Color.values()[Misc.getRandom().nextInt(Horse.Color.values().length)]);
  249. horse.setStyle(Horse.Style.values()[Misc.getRandom().nextInt(Horse.Style.values().length)]);
  250. horse.setJumpStrength(Math.max(AdvancedConfig.getInstance().getMinHorseJumpStrength(), Math.min(Math.min(Misc.getRandom().nextDouble(), Misc.getRandom().nextDouble()) * 2, AdvancedConfig.getInstance().getMaxHorseJumpStrength())));
  251. //TODO: setSpeed, once available
  252. break;
  253. default:
  254. break;
  255. }
  256. if (Permissions.renamePets(player)) {
  257. entity.setCustomName(LocaleLoader.getString("Taming.Summon.Name.Format", player.getName(), StringUtils.getPrettyEntityTypeString(type)));
  258. }
  259. ParticleEffectUtils.playCallOfTheWildEffect(entity);
  260. }
  261. player.setItemInHand(heldItemAmount == summonAmount ? null : new ItemStack(heldItem.getType(), heldItemAmount - summonAmount));
  262. String lifeSpan = "";
  263. if (tamingCOTWLength > 0) {
  264. lifeSpan = LocaleLoader.getString("Taming.Summon.Lifespan", tamingCOTWLength);
  265. }
  266. player.sendMessage(LocaleLoader.getString("Taming.Summon.Complete") + lifeSpan);
  267. player.playSound(location, Sound.ENTITY_FIREWORK_BLAST_FAR, 1F, 0.5F);
  268. }
  269. private boolean rangeCheck(EntityType type) {
  270. double range = Config.getInstance().getTamingCOTWRange();
  271. Player player = getPlayer();
  272. if (range == 0) {
  273. return true;
  274. }
  275. for (Entity entity : player.getNearbyEntities(range, range, range)) {
  276. if (entity.getType() == type) {
  277. player.sendMessage(Taming.getCallOfTheWildFailureMessage(type));
  278. return false;
  279. }
  280. }
  281. return true;
  282. }
  283. private boolean summonAmountCheck(EntityType entityType) {
  284. Player player = getPlayer();
  285. int maxAmountSummons = Config.getInstance().getTamingCOTWMaxAmount(entityType);
  286. if (maxAmountSummons <= 0) {
  287. return true;
  288. }
  289. List<TrackedTamingEntity> trackedEntities = getTrackedEntities(entityType);
  290. int summonAmount = trackedEntities == null ? 0 : trackedEntities.size();
  291. if (summonAmount >= maxAmountSummons) {
  292. player.sendMessage(LocaleLoader.getString("Taming.Summon.Fail.TooMany", maxAmountSummons));
  293. return false;
  294. }
  295. return true;
  296. }
  297. protected static void addToTracker(LivingEntity livingEntity) {
  298. TrackedTamingEntity trackedEntity = new TrackedTamingEntity(livingEntity);
  299. if (!summonedEntities.containsKey(livingEntity.getType())) {
  300. summonedEntities.put(livingEntity.getType(), new ArrayList<TrackedTamingEntity>());
  301. }
  302. summonedEntities.get(livingEntity.getType()).add(trackedEntity);
  303. }
  304. protected static List<TrackedTamingEntity> getTrackedEntities(EntityType entityType) {
  305. return summonedEntities.get(entityType);
  306. }
  307. protected static void removeFromTracker(TrackedTamingEntity trackedEntity) {
  308. summonedEntities.get(trackedEntity.getLivingEntity().getType()).remove(trackedEntity);
  309. }
  310. }