SuperAbilityManager.java 11 KB

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