TamingManager.java 14 KB

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