SuperSkillManagerImpl.java 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  1. package com.gmail.nossr50.util.input;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  5. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  6. import com.neetgames.mcmmo.player.MMOPlayerData;
  7. import com.gmail.nossr50.datatypes.skills.AbilityToolType;
  8. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  9. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  10. import com.gmail.nossr50.mcMMO;
  11. import com.gmail.nossr50.runnables.skills.AbilityDisableTask;
  12. import com.gmail.nossr50.runnables.skills.ToolLowerTask;
  13. import com.gmail.nossr50.util.EventUtils;
  14. import com.gmail.nossr50.util.Misc;
  15. import com.gmail.nossr50.util.player.NotificationManager;
  16. import com.gmail.nossr50.util.skills.PerksUtils;
  17. import com.gmail.nossr50.util.skills.RankUtils;
  18. import com.gmail.nossr50.util.skills.SkillUtils;
  19. import com.gmail.nossr50.util.sounds.SoundManager;
  20. import com.gmail.nossr50.util.sounds.SoundType;
  21. import com.neetgames.mcmmo.player.SuperSkillManager;
  22. import org.bukkit.entity.Player;
  23. import org.bukkit.inventory.ItemStack;
  24. import org.jetbrains.annotations.NotNull;
  25. import java.util.HashMap;
  26. import java.util.Map;
  27. public class SuperSkillManagerImpl implements SuperSkillManager {
  28. private final McMMOPlayer mmoPlayer;
  29. private final Player player;
  30. private final Map<SuperAbilityType, Boolean> superAbilityState = new HashMap<>();
  31. private final Map<SuperAbilityType, Boolean> abilityInformed = new HashMap<>();
  32. private boolean abilityActivationPermission = true;
  33. private final Map<AbilityToolType, Boolean> toolMode = new HashMap<>();
  34. private final MMOPlayerData mmoPlayerData;
  35. public SuperSkillManagerImpl(@NotNull McMMOPlayer mmoPlayer, @NotNull MMOPlayerData mmoPlayerData) {
  36. this.mmoPlayer = mmoPlayer;
  37. this.mmoPlayerData = mmoPlayerData;
  38. this.player = Misc.adaptPlayer(mmoPlayer);
  39. for (SuperAbilityType superAbilityType : SuperAbilityType.values()) {
  40. superAbilityState.put(superAbilityType, false);
  41. abilityInformed.put(superAbilityType, true); // This is intended
  42. }
  43. for (AbilityToolType abilityToolType : AbilityToolType.values()) {
  44. toolMode.put(abilityToolType, false);
  45. }
  46. }
  47. public void processAbilityActivation(PrimarySkillType skill) {
  48. Player player = Misc.adaptPlayer(mmoPlayer);
  49. if (!skill.getPermissions(player)) {
  50. return;
  51. }
  52. if (Config.getInstance().getAbilitiesOnlyActivateWhenSneaking() && !player.isSneaking()) {
  53. return;
  54. }
  55. ItemStack inHand = player.getInventory().getItemInMainHand();
  56. if (mcMMO.getModManager().isCustomTool(inHand) && !mcMMO.getModManager().getTool(inHand).isAbilityEnabled()) {
  57. return;
  58. }
  59. if (!getAbilityActivationPermission()) {
  60. return;
  61. }
  62. //Don't activate 2 abilities at once
  63. for (SuperAbilityType superAbilityType : SuperAbilityType.values()) {
  64. if (getAbilityMode(superAbilityType)) {
  65. return;
  66. }
  67. }
  68. SuperAbilityType ability = skill.getSuperAbilityType();
  69. /*
  70. * Woodcutting & Axes need to be treated differently.
  71. * Basically the tool always needs to ready and we check to see if the cooldown is over when the user takes action
  72. */
  73. /*
  74. * Woodcutting & Axes need to be treated differently.
  75. * Basically the tool always needs to ready and we check to see if the cooldown is over when the user takes action
  76. */
  77. //TODO: Convert this later
  78. if (mmoPlayer.getAbilityActivationProcessor().isHoldingTool() && !isAbilityToolPrimed(tool)) {
  79. if (skill != PrimarySkillType.WOODCUTTING && skill != PrimarySkillType.AXES) {
  80. int timeRemaining = calculateTimeRemaining(ability);
  81. if (isAbilityOnCooldown(ability)) {
  82. NotificationManager.sendPlayerInformation(player, NotificationType.ABILITY_COOLDOWN, "Skills.TooTired", String.valueOf(timeRemaining));
  83. return;
  84. }
  85. }
  86. if (Config.getInstance().getAbilityMessagesEnabled()) {
  87. /*
  88. *
  89. * IF THE TOOL IS AN AXE
  90. *
  91. */
  92. if(tool == ToolType.AXE) {
  93. processAxeToolMessages();
  94. } else {
  95. NotificationManager.sendPlayerInformation(player, NotificationType.TOOL, tool.getRaiseTool());
  96. }
  97. //Send Sound
  98. SoundManager.sendSound(player, player.getLocation(), SoundType.TOOL_READY);
  99. }
  100. setToolPreparationMode(tool, true);
  101. new ToolLowerTask(this, tool).runTaskLater(mcMMO.p, 4 * Misc.TICK_CONVERSION_FACTOR);
  102. }
  103. //TODO: Older code below
  104. if (mmoPlayer.getAbilityActivationProcessor().isHoldingTool() && !isAbilityToolPrimed(tool)) {
  105. if (skill != PrimarySkillType.WOODCUTTING && skill != PrimarySkillType.AXES) {
  106. int timeRemaining = calculateTimeRemaining(ability);
  107. if (!getAbilityMode(ability) && timeRemaining > 0) {
  108. NotificationManager.sendPlayerInformation(player, NotificationType.ABILITY_COOLDOWN, "Skills.TooTired", String.valueOf(timeRemaining));
  109. return;
  110. }
  111. }
  112. if (Config.getInstance().getAbilityMessagesEnabled()) {
  113. NotificationManager.sendPlayerInformation(player, NotificationType.TOOL, tool.getRaiseToolLocaleKey());
  114. SoundManager.sendSound(player, player.getLocation(), SoundType.TOOL_READY);
  115. }
  116. setAbilityToolPrime(tool, true);
  117. new ToolLowerTask(mmoPlayer, tool).runTaskLater(mcMMO.p, 4 * Misc.TICK_CONVERSION_FACTOR);
  118. }
  119. }
  120. /**
  121. * Check to see if an ability can be activated.
  122. *
  123. * @param primarySkillType The primarySkillType the ability is based on
  124. */
  125. public void checkAbilityActivation(PrimarySkillType primarySkillType) {
  126. AbilityToolType tool = primarySkillType.getTool();
  127. SuperAbilityType ability = primarySkillType.getSuperAbilityType();
  128. if (getAbilityMode(ability) || !ability.getPermissions(player)) {
  129. return;
  130. }
  131. //TODO: This is hacky and temporary solution until skills are move to the new system
  132. //Potential problems with this include skills with two super abilities (ie mining)
  133. if(!primarySkillType.isSuperAbilityUnlocked(player))
  134. {
  135. int diff = RankUtils.getSuperAbilityUnlockRequirement(primarySkillType.getSuperAbilityType()) - mmoPlayer.getSkillLevel(primarySkillType);
  136. //Inform the player they are not yet skilled enough
  137. NotificationManager.sendPlayerInformation(player, NotificationType.ABILITY_COOLDOWN, "Skills.AbilityGateRequirementFail", String.valueOf(diff), primarySkillType.getName());
  138. return;
  139. }
  140. int timeRemaining = calculateTimeRemaining(ability);
  141. if (timeRemaining > 0) {
  142. /*
  143. * Axes and Woodcutting are odd because they share the same tool.
  144. * We show them the too tired message when they take action.
  145. */
  146. if (primarySkillType == PrimarySkillType.WOODCUTTING || primarySkillType == PrimarySkillType.AXES) {
  147. NotificationManager.sendPlayerInformation(player, NotificationType.ABILITY_COOLDOWN, "Skills.TooTired", String.valueOf(timeRemaining));
  148. //SoundManager.sendSound(player, player.getLocation(), SoundType.TIRED);
  149. }
  150. return;
  151. }
  152. if (EventUtils.callPlayerAbilityActivateEvent(player, primarySkillType).isCancelled()) {
  153. return;
  154. }
  155. //These values change depending on whether or not the server is in retro mode
  156. int abilityLengthVar = AdvancedConfig.getInstance().getAbilityLength();
  157. int abilityLengthCap = AdvancedConfig.getInstance().getAbilityLengthCap();
  158. int ticks;
  159. //Ability cap of 0 or below means no cap
  160. if(abilityLengthCap > 0)
  161. {
  162. ticks = PerksUtils.handleActivationPerks(player, 2 + (Math.min(abilityLengthCap, mmoPlayer.getSkillLevel(primarySkillType)) / abilityLengthVar), ability.getMaxLength());
  163. } else {
  164. ticks = PerksUtils.handleActivationPerks(player, 2 + (mmoPlayer.getSkillLevel(primarySkillType) / abilityLengthVar), ability.getMaxLength());
  165. }
  166. if (mmoPlayer.hasSkillChatNotifications()) {
  167. NotificationManager.sendPlayerInformation(player, NotificationType.SUPER_ABILITY, ability.getAbilityOn());
  168. //player.sendMessage(ability.getAbilityOn());
  169. }
  170. if (AdvancedConfig.getInstance().sendAbilityNotificationToOtherPlayers()) {
  171. SkillUtils.sendSkillMessage(player, NotificationType.SUPER_ABILITY_ALERT_OTHERS, ability.getAbilityPlayer());
  172. }
  173. //Sounds
  174. SoundManager.worldSendSound(player.getWorld(), player.getLocation(), SoundType.ABILITY_ACTIVATED_GENERIC);
  175. // Enable the ability
  176. mmoPlayer.getPersistentPlayerData().setAbilityDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR));
  177. setAbilityMode(ability, true);
  178. if (ability == SuperAbilityType.SUPER_BREAKER || ability == SuperAbilityType.GIGA_DRILL_BREAKER) {
  179. SkillUtils.handleAbilitySpeedIncrease(player);
  180. }
  181. setAbilityToolPrime(tool, false);
  182. new AbilityDisableTask(mmoPlayer, ability).runTaskLater(mcMMO.p, ticks * Misc.TICK_CONVERSION_FACTOR);
  183. }
  184. /*
  185. * Abilities
  186. */
  187. /**
  188. * Reset the mode of all abilities.
  189. */
  190. public void disableSuperAbilities() {
  191. for (SuperAbilityType ability : SuperAbilityType.values()) {
  192. // Correctly disable and handle any special deactivate code
  193. new AbilityDisableTask(mmoPlayer, ability).run();
  194. }
  195. }
  196. public void resetCooldowns() {
  197. this.mmoPlayerData.resetCooldowns();
  198. }
  199. /**
  200. * Get the mode of an ability.
  201. *
  202. * @param ability The ability to check
  203. * @return true if the ability is enabled, false otherwise
  204. */
  205. public boolean getAbilityMode(SuperAbilityType ability) {
  206. return superAbilityState.get(ability);
  207. }
  208. /**
  209. * Set the mode of an ability.
  210. *
  211. * @param ability The ability to check
  212. * @param isActive True if the ability is active, false otherwise
  213. */
  214. public void setAbilityMode(SuperAbilityType ability, boolean isActive) {
  215. superAbilityState.put(ability, isActive);
  216. }
  217. /**
  218. * Get the informed state of an ability
  219. *
  220. * @param ability The ability to check
  221. * @return true if the ability is informed, false otherwise
  222. */
  223. public boolean getAbilityInformed(SuperAbilityType ability) {
  224. return abilityInformed.get(ability);
  225. }
  226. /**
  227. * Set the informed state of an ability.
  228. *
  229. * @param ability The ability to check
  230. * @param isInformed True if the ability is informed, false otherwise
  231. */
  232. public void setAbilityInformed(SuperAbilityType ability, boolean isInformed) {
  233. abilityInformed.put(ability, isInformed);
  234. }
  235. /**
  236. * Whether or not a tool is primed
  237. *
  238. * @param abilityToolType ability tool to check
  239. * @return true if the abilityToolType is primed, false otherwise
  240. */
  241. public boolean isAbilityToolPrimed(AbilityToolType abilityToolType) {
  242. return toolMode.get(abilityToolType);
  243. }
  244. public boolean getAbilityActivationPermission() {
  245. return abilityActivationPermission;
  246. }
  247. public void toggleAbilityActivationPermission() {
  248. abilityActivationPermission = !abilityActivationPermission;
  249. }
  250. /*
  251. * Tools
  252. */
  253. /**
  254. * Reset the prep modes of all tools.
  255. */
  256. public void unprimeAllAbilityTools() {
  257. for (AbilityToolType abilityToolType : AbilityToolType.values()) {
  258. setAbilityToolPrime(abilityToolType, false);
  259. }
  260. }
  261. /**
  262. * Set the current prep mode of a abilityToolType.
  263. *
  264. * @param abilityToolType Tool to set the mode for
  265. * @param isPrepared true if the abilityToolType should be prepped, false otherwise
  266. */
  267. public void setAbilityToolPrime(AbilityToolType abilityToolType, boolean isPrepared) {
  268. toolMode.put(abilityToolType, isPrepared);
  269. }
  270. /**
  271. * Calculate the time remaining until the superAbilityType's cooldown expires.
  272. *
  273. * @param superAbilityType SuperAbilityType whose cooldown to check
  274. *
  275. * @return the number of seconds remaining before the cooldown expires
  276. */
  277. public int calculateTimeRemaining(SuperAbilityType superAbilityType) {
  278. long deactivatedTimestamp = mmoPlayer.getAbilityDATS(superAbilityType) * Misc.TIME_CONVERSION_FACTOR;
  279. return (int) (((deactivatedTimestamp + (PerksUtils.handleCooldownPerks(Misc.adaptPlayer(mmoPlayer), superAbilityType.getCooldown()) * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR);
  280. }
  281. }