TamingManager.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  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.experience.XPGainReason;
  6. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  7. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  8. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  9. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  10. import com.gmail.nossr50.datatypes.skills.subskills.taming.CallOfTheWildType;
  11. import com.gmail.nossr50.datatypes.skills.subskills.taming.TamingSummon;
  12. import com.gmail.nossr50.locale.LocaleLoader;
  13. import com.gmail.nossr50.mcMMO;
  14. import com.gmail.nossr50.runnables.skills.BleedTimerTask;
  15. import com.gmail.nossr50.skills.SkillManager;
  16. import com.gmail.nossr50.util.Misc;
  17. import com.gmail.nossr50.util.Permissions;
  18. import com.gmail.nossr50.util.compat.layers.persistentdata.MobMetaFlagType;
  19. import com.gmail.nossr50.util.player.NotificationManager;
  20. import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
  21. import com.gmail.nossr50.util.random.RandomChanceUtil;
  22. import com.gmail.nossr50.util.skills.ParticleEffectUtils;
  23. import com.gmail.nossr50.util.skills.RankUtils;
  24. import com.gmail.nossr50.util.skills.SkillActivationType;
  25. import com.gmail.nossr50.util.sounds.SoundManager;
  26. import com.gmail.nossr50.util.sounds.SoundType;
  27. import com.gmail.nossr50.util.text.StringUtils;
  28. import org.bukkit.Location;
  29. import org.bukkit.Material;
  30. import org.bukkit.attribute.Attribute;
  31. import org.bukkit.entity.*;
  32. import org.bukkit.inventory.ItemStack;
  33. import org.jetbrains.annotations.NotNull;
  34. import java.util.ArrayList;
  35. import java.util.HashMap;
  36. import java.util.List;
  37. public class TamingManager extends SkillManager {
  38. //TODO: Temporary static cache, will be changed in 2.2
  39. private static HashMap<Material, CallOfTheWildType> summoningItems;
  40. private static HashMap<CallOfTheWildType, TamingSummon> cotwSummonDataProperties;
  41. private long lastSummonTimeStamp;
  42. private HashMap<CallOfTheWildType, List<TrackedTamingEntity>> playerSummonedEntities;
  43. public TamingManager(McMMOPlayer mcMMOPlayer) {
  44. super(mcMMOPlayer, PrimarySkillType.TAMING);
  45. init();
  46. }
  47. //TODO: Hacky stuff for 2.1, will be cleaned up in 2.2
  48. private void init() {
  49. //prevents accidentally summoning too many things when holding down left click
  50. lastSummonTimeStamp = 0L;
  51. //Init per-player tracking of summoned entities
  52. initPerPlayerSummonTracking();
  53. //Hacky stuff used as a band-aid
  54. initStaticCaches();
  55. }
  56. private void initPerPlayerSummonTracking() {
  57. playerSummonedEntities = new HashMap<>();
  58. for(CallOfTheWildType callOfTheWildType : CallOfTheWildType.values()) {
  59. playerSummonedEntities.put(callOfTheWildType, new ArrayList<>());
  60. }
  61. }
  62. private void initStaticCaches() {
  63. //TODO: Temporary static cache, will be changed in 2.2
  64. //This is shared between instances of TamingManager
  65. if(summoningItems == null) {
  66. summoningItems = new HashMap<>();
  67. summoningItems.put(Config.getInstance().getTamingCOTWMaterial(CallOfTheWildType.CAT.getConfigEntityTypeEntry()), CallOfTheWildType.CAT);
  68. summoningItems.put(Config.getInstance().getTamingCOTWMaterial(CallOfTheWildType.WOLF.getConfigEntityTypeEntry()), CallOfTheWildType.WOLF);
  69. summoningItems.put(Config.getInstance().getTamingCOTWMaterial(CallOfTheWildType.HORSE.getConfigEntityTypeEntry()), CallOfTheWildType.HORSE);
  70. }
  71. //TODO: Temporary static cache, will be changed in 2.2
  72. //This is shared between instances of TamingManager
  73. if(cotwSummonDataProperties == null) {
  74. cotwSummonDataProperties = new HashMap<>();
  75. for(CallOfTheWildType callOfTheWildType : CallOfTheWildType.values()) {
  76. Material itemSummonMaterial = Config.getInstance().getTamingCOTWMaterial(callOfTheWildType.getConfigEntityTypeEntry());
  77. int itemAmountRequired = Config.getInstance().getTamingCOTWCost(callOfTheWildType.getConfigEntityTypeEntry());
  78. int entitiesSummonedPerCOTW = Config.getInstance().getTamingCOTWAmount(callOfTheWildType.getConfigEntityTypeEntry());
  79. int summonLifespanSeconds = Config.getInstance().getTamingCOTWLength(callOfTheWildType.getConfigEntityTypeEntry());
  80. int perPlayerMaxAmount = Config.getInstance().getTamingCOTWMaxAmount(callOfTheWildType.getConfigEntityTypeEntry());
  81. TamingSummon tamingSummon = new TamingSummon(callOfTheWildType, itemSummonMaterial, itemAmountRequired, entitiesSummonedPerCOTW, summonLifespanSeconds, perPlayerMaxAmount);
  82. cotwSummonDataProperties.put(callOfTheWildType, tamingSummon);
  83. }
  84. }
  85. }
  86. public boolean canUseThickFur() {
  87. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_THICK_FUR)
  88. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_THICK_FUR);
  89. }
  90. public boolean canUseEnvironmentallyAware() {
  91. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_ENVIRONMENTALLY_AWARE)
  92. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_ENVIRONMENTALLY_AWARE);
  93. }
  94. public boolean canUseShockProof() {
  95. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_SHOCK_PROOF)
  96. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_SHOCK_PROOF);
  97. }
  98. public boolean canUseHolyHound() {
  99. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_ENVIRONMENTALLY_AWARE)
  100. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_HOLY_HOUND);
  101. }
  102. public boolean canUseFastFoodService() {
  103. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_FAST_FOOD_SERVICE)
  104. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_FAST_FOOD_SERVICE);
  105. }
  106. public boolean canUseSharpenedClaws() {
  107. return RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_SHARPENED_CLAWS)
  108. && Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_SHARPENED_CLAWS);
  109. }
  110. public boolean canUseGore() {
  111. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_GORE))
  112. return false;
  113. return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_GORE);
  114. }
  115. public boolean canUseBeastLore() {
  116. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_BEAST_LORE))
  117. return false;
  118. return Permissions.isSubSkillEnabled(getPlayer(), SubSkillType.TAMING_BEAST_LORE);
  119. }
  120. /**
  121. * Award XP for taming.
  122. *
  123. * @param entity The LivingEntity to award XP for
  124. */
  125. public void awardTamingXP(@NotNull LivingEntity entity) {
  126. applyXpGain(ExperienceConfig.getInstance().getTamingXP(entity.getType()), XPGainReason.PVE);
  127. }
  128. /**
  129. * Apply the Fast Food Service ability.
  130. *
  131. * @param wolf The wolf using the ability
  132. * @param damage The damage being absorbed by the wolf
  133. */
  134. public void fastFoodService(@NotNull Wolf wolf, double damage) {
  135. if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_STATIC_CHANCE, SubSkillType.TAMING_FAST_FOOD_SERVICE, getPlayer())) {
  136. return;
  137. }
  138. double health = wolf.getHealth();
  139. double maxHealth = wolf.getMaxHealth();
  140. if (health < maxHealth) {
  141. double newHealth = health + damage;
  142. wolf.setHealth(Math.min(newHealth, maxHealth));
  143. }
  144. }
  145. /**
  146. * Apply the Gore ability.
  147. *
  148. * @param target The LivingEntity to apply Gore on
  149. * @param damage The initial damage
  150. */
  151. public double gore(@NotNull LivingEntity target, double damage) {
  152. if(BleedTimerTask.isBleedOperationAllowed()) {
  153. if (!RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.TAMING_GORE, getPlayer())) {
  154. return 0;
  155. }
  156. BleedTimerTask.add(target, getPlayer(), Taming.goreBleedTicks, 1, 2);
  157. if (target instanceof Player) {
  158. NotificationManager.sendPlayerInformation((Player)target, NotificationType.SUBSKILL_MESSAGE, "Combat.StruckByGore");
  159. }
  160. NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE, "Combat.Gore");
  161. damage = (damage * Taming.goreModifier) - damage;
  162. }
  163. return damage;
  164. }
  165. public double sharpenedClaws() {
  166. return Taming.sharpenedClawsBonusDamage;
  167. }
  168. /**
  169. * Summon an ocelot to your side.
  170. */
  171. public void summonOcelot() {
  172. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_CALL_OF_THE_WILD))
  173. return;
  174. if (!Permissions.callOfTheWild(getPlayer(), EntityType.OCELOT)) {
  175. return;
  176. }
  177. processCallOfTheWild();
  178. }
  179. /**
  180. * Summon a wolf to your side.
  181. */
  182. public void summonWolf() {
  183. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_CALL_OF_THE_WILD))
  184. return;
  185. if (!Permissions.callOfTheWild(getPlayer(), EntityType.WOLF)) {
  186. return;
  187. }
  188. processCallOfTheWild();
  189. }
  190. /**
  191. * Summon a horse to your side.
  192. */
  193. public void summonHorse() {
  194. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_CALL_OF_THE_WILD))
  195. return;
  196. if (!Permissions.callOfTheWild(getPlayer(), EntityType.HORSE)) {
  197. return;
  198. }
  199. processCallOfTheWild();
  200. }
  201. /**
  202. * Handle the Beast Lore ability.
  203. *
  204. * @param target The entity to examine
  205. */
  206. public void beastLore(LivingEntity target) {
  207. Player player = getPlayer();
  208. Tameable beast = (Tameable) target;
  209. String message = LocaleLoader.getString("Combat.BeastLore") + " ";
  210. if (beast.isTamed() && beast.getOwner() != null) {
  211. message = message.concat(LocaleLoader.getString("Combat.BeastLoreOwner", beast.getOwner().getName()) + " ");
  212. }
  213. message = message.concat(LocaleLoader.getString("Combat.BeastLoreHealth", target.getHealth(), target.getMaxHealth()));
  214. if (beast instanceof Horse) {
  215. Horse horse = (Horse) beast;
  216. double jumpStrength = horse.getAttribute(Attribute.HORSE_JUMP_STRENGTH).getValue();
  217. // Taken from https://minecraft.gamepedia.com/Horse#Jump_strength
  218. jumpStrength = -0.1817584952 * Math.pow(jumpStrength, 3) + 3.689713992 * Math.pow(jumpStrength, 2) + 2.128599134 * jumpStrength - 0.343930367;
  219. message = message.concat("\n" + LocaleLoader.getString("Combat.BeastLoreHorseSpeed", horse.getAttribute(Attribute.GENERIC_MOVEMENT_SPEED).getValue() * 43))
  220. .concat("\n" + LocaleLoader.getString("Combat.BeastLoreHorseJumpStrength", jumpStrength));
  221. }
  222. player.sendMessage(message);
  223. }
  224. public void processEnvironmentallyAware(Wolf wolf, double damage) {
  225. if (damage > wolf.getHealth()) {
  226. return;
  227. }
  228. Player owner = getPlayer();
  229. wolf.teleport(owner);
  230. NotificationManager.sendPlayerInformation(owner, NotificationType.SUBSKILL_MESSAGE, "Taming.Listener.Wolf");
  231. }
  232. public void pummel(LivingEntity target, Wolf wolf) {
  233. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.TAMING_PUMMEL))
  234. return;
  235. if(!RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(AdvancedConfig.getInstance().getPummelChance(), getPlayer(), SubSkillType.TAMING_PUMMEL)))
  236. return;
  237. ParticleEffectUtils.playGreaterImpactEffect(target);
  238. target.setVelocity(wolf.getLocation().getDirection().normalize().multiply(1.5D));
  239. if (target instanceof Player) {
  240. Player defender = (Player) target;
  241. if (NotificationManager.doesPlayerUseNotifications(defender)) {
  242. NotificationManager.sendPlayerInformation(defender, NotificationType.SUBSKILL_MESSAGE, "Taming.SubSkill.Pummel.TargetMessage");
  243. }
  244. }
  245. }
  246. public void attackTarget(LivingEntity target) {
  247. if(target instanceof Tameable)
  248. {
  249. Tameable tameable = (Tameable) target;
  250. if(tameable.getOwner() == getPlayer())
  251. {
  252. return;
  253. }
  254. }
  255. double range = 5;
  256. Player player = getPlayer();
  257. for (Entity entity : player.getNearbyEntities(range, range, range)) {
  258. if (entity.getType() != EntityType.WOLF) {
  259. continue;
  260. }
  261. Wolf wolf = (Wolf) entity;
  262. if (!wolf.isTamed() || (wolf.getOwner() != player) || wolf.isSitting()) {
  263. continue;
  264. }
  265. wolf.setTarget(target);
  266. }
  267. }
  268. private void processCallOfTheWild() {
  269. //Prevent summoning too many things accidentally if a player holds down the button
  270. if(lastSummonTimeStamp + 150 > System.currentTimeMillis()) {
  271. return;
  272. } else {
  273. lastSummonTimeStamp = System.currentTimeMillis();
  274. }
  275. Player player = getPlayer();
  276. ItemStack itemInMainHand = player.getInventory().getItemInMainHand();
  277. //Check if the item the player is currently holding is a COTW item
  278. if(isCOTWItem(itemInMainHand)) {
  279. //Get the summoning type
  280. CallOfTheWildType callOfTheWildType = summoningItems.get(itemInMainHand.getType());
  281. TamingSummon tamingSummon = cotwSummonDataProperties.get(callOfTheWildType);
  282. //Players will pay for the cost if at least one thing was summoned
  283. int amountSummoned = 0;
  284. //Check to see if players have the correct amount of the item required to summon
  285. if(itemInMainHand.getAmount() >= tamingSummon.getItemAmountRequired()) {
  286. //Initial Spawn location
  287. Location spawnLocation = Misc.getLocationOffset(player.getLocation(), 1);
  288. //COTW can summon multiple entities per usage
  289. for (int i = 0; i < tamingSummon.getEntitiesSummoned(); i++) {
  290. if (getAmountCurrentlySummoned(callOfTheWildType) >= tamingSummon.getSummonCap()) {
  291. NotificationManager.sendPlayerInformationChatOnly(player, "Taming.Summon.COTW.Limit",
  292. String.valueOf(tamingSummon.getSummonCap()),
  293. StringUtils.getCapitalized(callOfTheWildType.toString()));
  294. break;
  295. }
  296. spawnLocation = Misc.getLocationOffset(spawnLocation, 1);
  297. spawnCOTWEntity(callOfTheWildType, spawnLocation, tamingSummon.getEntityType());
  298. //Inform the player about what they have just done
  299. if (tamingSummon.getSummonLifespan() > 0) {
  300. NotificationManager.sendPlayerInformationChatOnly(player, "Taming.Summon.COTW.Success.WithLifespan",
  301. StringUtils.getCapitalized(callOfTheWildType.toString()), String.valueOf(tamingSummon.getSummonLifespan()));
  302. } else {
  303. NotificationManager.sendPlayerInformationChatOnly(player, "Taming.Summon.COTW.Success.WithoutLifespan", StringUtils.getCapitalized(callOfTheWildType.toString()));
  304. }
  305. //Send Sound
  306. SoundManager.sendSound(player, player.getLocation(), SoundType.ABILITY_ACTIVATED_GENERIC);
  307. amountSummoned++;
  308. }
  309. //Remove items from the player if they had at least one entity summoned successfully
  310. if(amountSummoned >= 1) {
  311. //Remove the items used to summon
  312. int itemAmountAfterPayingCost = itemInMainHand.getAmount() - tamingSummon.getItemAmountRequired();
  313. itemInMainHand.setAmount(itemAmountAfterPayingCost);
  314. player.updateInventory();
  315. }
  316. } else {
  317. //Player did not have enough of the item in their main hand
  318. int difference = tamingSummon.getItemAmountRequired() - itemInMainHand.getAmount();
  319. NotificationManager.sendPlayerInformationChatOnly(player, "Taming.Summon.COTW.NeedMoreItems", String.valueOf(difference), StringUtils.getPrettyItemString(itemInMainHand.getType()));
  320. }
  321. }
  322. }
  323. private void spawnCOTWEntity(CallOfTheWildType callOfTheWildType, Location spawnLocation, EntityType entityType) {
  324. switch(callOfTheWildType) {
  325. case CAT:
  326. //Entity type is needed for cats because in 1.13 and below we spawn ocelots, in 1.14 and above we spawn cats
  327. spawnCat(spawnLocation, entityType);
  328. break;
  329. case HORSE:
  330. spawnHorse(spawnLocation);
  331. break;
  332. case WOLF:
  333. spawnWolf(spawnLocation);
  334. break;
  335. }
  336. }
  337. private void spawnWolf(Location spawnLocation) {
  338. LivingEntity callOfWildEntity = (LivingEntity) getPlayer().getWorld().spawnEntity(spawnLocation, EntityType.WOLF);
  339. //This is used to prevent XP gains for damaging this entity
  340. applyMetaDataToCOTWEntity(callOfWildEntity);
  341. setBaseCOTWEntityProperties(callOfWildEntity);
  342. ((Wolf) callOfWildEntity).setAdult();
  343. addToTracker(callOfWildEntity, CallOfTheWildType.WOLF);
  344. //Setup wolf stats
  345. callOfWildEntity.setMaxHealth(20.0);
  346. callOfWildEntity.setHealth(callOfWildEntity.getMaxHealth());
  347. callOfWildEntity.setCustomName(LocaleLoader.getString("Taming.Summon.Name.Format", getPlayer().getName(), StringUtils.getPrettyEntityTypeString(EntityType.WOLF)));
  348. }
  349. private void spawnCat(Location spawnLocation, EntityType entityType) {
  350. LivingEntity callOfWildEntity = (LivingEntity) getPlayer().getWorld().spawnEntity(spawnLocation, entityType);
  351. //This is used to prevent XP gains for damaging this entity
  352. applyMetaDataToCOTWEntity(callOfWildEntity);
  353. setBaseCOTWEntityProperties(callOfWildEntity);
  354. addToTracker(callOfWildEntity, CallOfTheWildType.CAT);
  355. //Randomize the cat
  356. if(callOfWildEntity instanceof Ocelot) {
  357. int numberOfTypes = Ocelot.Type.values().length;
  358. ((Ocelot) callOfWildEntity).setCatType(Ocelot.Type.values()[Misc.getRandom().nextInt(numberOfTypes)]);
  359. ((Ocelot) callOfWildEntity).setAdult();
  360. } else if(callOfWildEntity instanceof Cat) {
  361. int numberOfTypes = Cat.Type.values().length;
  362. ((Cat) callOfWildEntity).setCatType(Cat.Type.values()[Misc.getRandom().nextInt(numberOfTypes)]);
  363. ((Cat) callOfWildEntity).setAdult();
  364. }
  365. callOfWildEntity.setCustomName(LocaleLoader.getString("Taming.Summon.Name.Format", getPlayer().getName(), StringUtils.getPrettyEntityTypeString(entityType)));
  366. //Particle effect
  367. ParticleEffectUtils.playCallOfTheWildEffect(callOfWildEntity);
  368. }
  369. private void spawnHorse(Location spawnLocation) {
  370. LivingEntity callOfWildEntity = (LivingEntity) getPlayer().getWorld().spawnEntity(spawnLocation, EntityType.HORSE);
  371. applyMetaDataToCOTWEntity(callOfWildEntity);
  372. setBaseCOTWEntityProperties(callOfWildEntity);
  373. addToTracker(callOfWildEntity, CallOfTheWildType.HORSE);
  374. //Randomize Horse
  375. Horse horse = (Horse) callOfWildEntity;
  376. callOfWildEntity.setMaxHealth(15.0 + (Misc.getRandom().nextDouble() * 15));
  377. callOfWildEntity.setHealth(callOfWildEntity.getMaxHealth());
  378. horse.setColor(Horse.Color.values()[Misc.getRandom().nextInt(Horse.Color.values().length)]);
  379. horse.setStyle(Horse.Style.values()[Misc.getRandom().nextInt(Horse.Style.values().length)]);
  380. horse.setJumpStrength(Math.max(AdvancedConfig.getInstance().getMinHorseJumpStrength(), Math.min(Math.min(Misc.getRandom().nextDouble(), Misc.getRandom().nextDouble()) * 2, AdvancedConfig.getInstance().getMaxHorseJumpStrength())));
  381. horse.setAdult();
  382. //TODO: setSpeed, once available
  383. callOfWildEntity.setCustomName(LocaleLoader.getString("Taming.Summon.Name.Format", getPlayer().getName(), StringUtils.getPrettyEntityTypeString(EntityType.HORSE)));
  384. //Particle effect
  385. ParticleEffectUtils.playCallOfTheWildEffect(callOfWildEntity);
  386. }
  387. private void setBaseCOTWEntityProperties(LivingEntity callOfWildEntity) {
  388. ((Tameable) callOfWildEntity).setOwner(getPlayer());
  389. callOfWildEntity.setRemoveWhenFarAway(false);
  390. }
  391. private void applyMetaDataToCOTWEntity(LivingEntity summonedEntity) {
  392. //This helps identify the entity as being summoned by COTW
  393. mcMMO.getCompatibilityManager().getPersistentDataLayer().flagMetadata(MobMetaFlagType.COTW_SUMMONED_MOB, summonedEntity);
  394. }
  395. /**
  396. * Whether or not the itemstack is used for COTW
  397. * @param itemStack target ItemStack
  398. * @return true if it is used for any COTW
  399. */
  400. public boolean isCOTWItem(ItemStack itemStack) {
  401. return summoningItems.containsKey(itemStack.getType());
  402. }
  403. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  404. private int getAmountCurrentlySummoned(CallOfTheWildType callOfTheWildType) {
  405. //The tracker is unreliable so validate its contents first
  406. recalibrateTracker();
  407. return playerSummonedEntities.get(callOfTheWildType).size();
  408. }
  409. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  410. private void addToTracker(LivingEntity livingEntity, CallOfTheWildType callOfTheWildType) {
  411. TrackedTamingEntity trackedEntity = new TrackedTamingEntity(livingEntity, callOfTheWildType, this);
  412. playerSummonedEntities.get(callOfTheWildType).add(trackedEntity);
  413. }
  414. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  415. public List<TrackedTamingEntity> getTrackedEntities(CallOfTheWildType callOfTheWildType) {
  416. return playerSummonedEntities.get(callOfTheWildType);
  417. }
  418. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  419. public void removeFromTracker(TrackedTamingEntity trackedEntity) {
  420. playerSummonedEntities.get(trackedEntity.getCallOfTheWildType()).remove(trackedEntity);
  421. NotificationManager.sendPlayerInformationChatOnly(getPlayer(), "Taming.Summon.COTW.TimeExpired", StringUtils.getPrettyEntityTypeString(trackedEntity.getLivingEntity().getType()));
  422. }
  423. /**
  424. * Builds a new tracked list by determining which tracked things are still valid
  425. */
  426. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  427. private void recalibrateTracker() {
  428. for(CallOfTheWildType callOfTheWildType : CallOfTheWildType.values()) {
  429. ArrayList<TrackedTamingEntity> validEntities = getValidTrackedEntities(callOfTheWildType);
  430. playerSummonedEntities.put(callOfTheWildType, validEntities); //Replace the old list with the new list
  431. }
  432. }
  433. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  434. private ArrayList<TrackedTamingEntity> getValidTrackedEntities(CallOfTheWildType callOfTheWildType) {
  435. ArrayList<TrackedTamingEntity> validTrackedEntities = new ArrayList<>();
  436. for(TrackedTamingEntity trackedTamingEntity : getTrackedEntities(callOfTheWildType)) {
  437. LivingEntity livingEntity = trackedTamingEntity.getLivingEntity();
  438. //Remove from existence
  439. if(livingEntity != null && livingEntity.isValid()) {
  440. validTrackedEntities.add(trackedTamingEntity);
  441. }
  442. }
  443. return validTrackedEntities;
  444. }
  445. /**
  446. * Remove all tracked entities from existence if they currently exist
  447. * Clear the tracked entity lists afterwards
  448. */
  449. //TODO: The way this tracker was written is garbo, I should just rewrite it, I'll save that for a future update
  450. public void cleanupAllSummons() {
  451. for(List<TrackedTamingEntity> trackedTamingEntities : playerSummonedEntities.values()) {
  452. for(TrackedTamingEntity trackedTamingEntity : trackedTamingEntities) {
  453. LivingEntity livingEntity = trackedTamingEntity.getLivingEntity();
  454. //Remove from existence
  455. if(livingEntity != null && livingEntity.isValid()) {
  456. livingEntity.setHealth(0);
  457. livingEntity.remove();
  458. }
  459. }
  460. //Clear the list
  461. trackedTamingEntities.clear();
  462. }
  463. }
  464. }