TamingManager.java 15 KB

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