Skills.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496
  1. package com.gmail.nossr50.util;
  2. import org.bukkit.ChatColor;
  3. import org.bukkit.block.Block;
  4. import org.bukkit.enchantments.Enchantment;
  5. import org.bukkit.entity.Player;
  6. import org.bukkit.inventory.ItemStack;
  7. import org.getspout.spoutapi.SpoutManager;
  8. import org.getspout.spoutapi.player.SpoutPlayer;
  9. import com.gmail.nossr50.mcMMO;
  10. import com.gmail.nossr50.config.AdvancedConfig;
  11. import com.gmail.nossr50.config.Config;
  12. import com.gmail.nossr50.config.SpoutConfig;
  13. import com.gmail.nossr50.datatypes.AbilityType;
  14. import com.gmail.nossr50.datatypes.McMMOPlayer;
  15. import com.gmail.nossr50.datatypes.PlayerProfile;
  16. import com.gmail.nossr50.datatypes.PlayerStat;
  17. import com.gmail.nossr50.datatypes.SkillType;
  18. import com.gmail.nossr50.datatypes.ToolType;
  19. import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
  20. import com.gmail.nossr50.locale.LocaleLoader;
  21. import com.gmail.nossr50.spout.SpoutStuff;
  22. public class Skills {
  23. static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
  24. public static int abilityLengthIncreaseLevel = advancedConfig.getAbilityLength();
  25. /**
  26. * Checks to see if the cooldown for an item or ability is expired.
  27. *
  28. * @param oldTime The time the ability or item was last used
  29. * @param cooldown The amount of time that must pass between uses
  30. * @param player The player whose cooldown is being checked
  31. * @return true if the cooldown is over, false otherwise
  32. */
  33. public static boolean cooldownOver(long oldTime, int cooldown, Player player) {
  34. long currentTime = System.currentTimeMillis();
  35. int adjustedCooldown = cooldown;
  36. //Reduced Cooldown Donor Perks
  37. if (Permissions.cooldownsHalved(player)) {
  38. adjustedCooldown = (int) (adjustedCooldown * 0.5);
  39. }
  40. else if (Permissions.cooldownsThirded(player)) {
  41. adjustedCooldown = (int) (adjustedCooldown * 0.66);
  42. }
  43. else if (Permissions.cooldownsQuartered(player)) {
  44. adjustedCooldown = (int) (adjustedCooldown * 0.75);
  45. }
  46. if (currentTime - oldTime >= (adjustedCooldown * Misc.TIME_CONVERSION_FACTOR)) {
  47. return true;
  48. }
  49. return false;
  50. }
  51. /**
  52. * Calculate the time remaining until the cooldown expires.
  53. *
  54. * @param deactivatedTimeStamp Time of deactivation
  55. * @param cooldown The length of the cooldown
  56. * @return the number of seconds remaining before the cooldown expires
  57. */
  58. public static int calculateTimeLeft(long deactivatedTimeStamp, int cooldown, Player player) {
  59. int adjustedCooldown = cooldown;
  60. //Reduced Cooldown Donor Perks
  61. if (Permissions.cooldownsHalved(player)) {
  62. adjustedCooldown = (int) (adjustedCooldown * 0.5);
  63. }
  64. else if (Permissions.cooldownsThirded(player)) {
  65. adjustedCooldown = (int) (adjustedCooldown * 0.66);
  66. }
  67. else if (Permissions.cooldownsQuartered(player)) {
  68. adjustedCooldown = (int) (adjustedCooldown * 0.75);
  69. }
  70. return (int) (((deactivatedTimeStamp + (adjustedCooldown * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR);
  71. }
  72. /**
  73. * Sends a message to the player when the cooldown expires.
  74. *
  75. * @param player The player to send a message to
  76. * @param profile The profile of the player
  77. * @param ability The ability to watch cooldowns for
  78. */
  79. public static void watchCooldown(Player player, PlayerProfile profile, AbilityType ability) {
  80. if (player == null || profile == null || ability == null)
  81. return;
  82. if (!profile.getAbilityInformed(ability) && cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
  83. profile.setAbilityInformed(ability, true);
  84. player.sendMessage(ability.getAbilityRefresh());
  85. }
  86. }
  87. /**
  88. * Process activating abilities & readying the tool.
  89. *
  90. * @param player The player using the ability
  91. * @param skill The skill the ability is tied to
  92. */
  93. public static void activationCheck(Player player, SkillType skill) {
  94. if (Config.getInstance().getAbilitiesOnlyActivateWhenSneaking() && !player.isSneaking()) {
  95. return;
  96. }
  97. PlayerProfile profile = Users.getProfile(player);
  98. AbilityType ability = skill.getAbility();
  99. ToolType tool = skill.getTool();
  100. ItemStack inHand = player.getItemInHand();
  101. if (ModChecks.isCustomTool(inHand) && !ModChecks.getToolFromItemStack(inHand).isAbilityEnabled()) {
  102. return;
  103. }
  104. /* Check if any abilities are active */
  105. if (profile == null) {
  106. return;
  107. }
  108. if (!profile.getAbilityUse()) {
  109. return;
  110. }
  111. for (AbilityType x : AbilityType.values()) {
  112. if (profile.getAbilityMode(x)) {
  113. return;
  114. }
  115. }
  116. /* Woodcutting & Axes need to be treated differently.
  117. * Basically the tool always needs to ready and we check to see if the cooldown is over when the user takes action
  118. */
  119. if (ability.getPermissions(player) && tool.inHand(inHand) && !profile.getToolPreparationMode(tool)) {
  120. if (skill != SkillType.WOODCUTTING && skill != SkillType.AXES) {
  121. if (!profile.getAbilityMode(ability) && !cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
  122. player.sendMessage(LocaleLoader.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player) + "s)");
  123. return;
  124. }
  125. }
  126. if (Config.getInstance().getAbilityMessagesEnabled()) {
  127. player.sendMessage(tool.getRaiseTool());
  128. }
  129. profile.setToolPreparationATS(tool, System.currentTimeMillis());
  130. profile.setToolPreparationMode(tool, true);
  131. }
  132. }
  133. /**
  134. * Monitors various things relating to skill abilities.
  135. *
  136. * @param player The player using the skill
  137. * @param profile The profile of the player
  138. * @param curTime The current system time
  139. * @param skill The skill being monitored
  140. */
  141. public static void monitorSkill(Player player, PlayerProfile profile, long curTime, SkillType skill) {
  142. final int FOUR_SECONDS = 4000;
  143. ToolType tool = skill.getTool();
  144. AbilityType ability = skill.getAbility();
  145. if (profile == null) {
  146. return;
  147. }
  148. if (profile.getToolPreparationMode(tool) && curTime - (profile.getToolPreparationATS(tool) * Misc.TIME_CONVERSION_FACTOR) >= FOUR_SECONDS) {
  149. profile.setToolPreparationMode(tool, false);
  150. if (Config.getInstance().getAbilityMessagesEnabled()) {
  151. player.sendMessage(tool.getLowerTool());
  152. }
  153. }
  154. if (ability.getPermissions(player)) {
  155. if (profile.getAbilityMode(ability) && (profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR) <= curTime) {
  156. profile.setAbilityMode(ability, false);
  157. profile.setAbilityInformed(ability, false);
  158. player.sendMessage(ability.getAbilityOff());
  159. Misc.sendSkillMessage(player, ability.getAbilityPlayerOff(player));
  160. }
  161. }
  162. }
  163. /**
  164. * Check the XP of a skill.
  165. *
  166. * @param skillType The skill to check
  167. * @param player The player whose skill to check
  168. * @param profile The profile of the player whose skill to check
  169. */
  170. public static void xpCheckSkill(SkillType skillType, Player player, PlayerProfile profile) {
  171. int skillups = 0;
  172. if (profile.getSkillXpLevel(skillType) >= profile.getXpToLevel(skillType)) {
  173. while (profile.getSkillXpLevel(skillType) >= profile.getXpToLevel(skillType)) {
  174. if ((skillType.getMaxLevel() >= profile.getSkillLevel(skillType) + 1) && (Misc.getPowerLevelCap() >= Users.getPlayer(player).getPowerLevel() + 1)) {
  175. profile.removeXP(skillType, profile.getXpToLevel(skillType));
  176. skillups++;
  177. profile.skillUp(skillType, 1);
  178. McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType);
  179. mcMMO.p.getServer().getPluginManager().callEvent(eventToFire);
  180. }
  181. else {
  182. profile.addLevels(skillType, 0);
  183. }
  184. }
  185. String capitalized = Misc.getCapitalized(skillType.toString());
  186. /* Spout Stuff */
  187. if (mcMMO.spoutEnabled) {
  188. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  189. if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) {
  190. SpoutStuff.levelUpNotification(skillType, spoutPlayer);
  191. /* Update custom titles */
  192. if (SpoutConfig.getInstance().getShowPowerLevel()) {
  193. spoutPlayer.setTitle(spoutPlayer.getName()+ "\n" + ChatColor.YELLOW + "P" + ChatColor.GOLD + "lvl" + ChatColor.WHITE + "." + ChatColor.GREEN + String.valueOf(Users.getPlayer(player).getPowerLevel()));
  194. }
  195. }
  196. else {
  197. player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", new Object[] {String.valueOf(skillups), profile.getSkillLevel(skillType)}));
  198. }
  199. }
  200. else {
  201. player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", new Object[] {String.valueOf(skillups), profile.getSkillLevel(skillType)}));
  202. }
  203. }
  204. if (mcMMO.spoutEnabled) {
  205. SpoutPlayer spoutPlayer = (SpoutPlayer) player;
  206. if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) {
  207. if (SpoutConfig.getInstance().getXPBarEnabled()) {
  208. profile.getSpoutHud().updateXpBar();
  209. }
  210. }
  211. }
  212. }
  213. /**
  214. * Check XP of all skills.
  215. *
  216. * @param player The player to check XP for.
  217. * @param profile The profile of the player whose skill to check
  218. */
  219. public static void xpCheckAll(Player player, PlayerProfile profile) {
  220. for (SkillType skillType : SkillType.values()) {
  221. //Don't want to do anything with this one
  222. if (skillType == SkillType.ALL) {
  223. continue;
  224. }
  225. xpCheckSkill(skillType, player, profile);
  226. }
  227. }
  228. /**
  229. * Get the skill represented by the given string
  230. *
  231. * @param skillName The name of the skill
  232. * @return the SkillType if it exists, null otherwise
  233. */
  234. public static SkillType getSkillType(String skillName) {
  235. for (SkillType x : SkillType.values()) {
  236. if (x.toString().equals(skillName.toUpperCase())) {
  237. return x;
  238. }
  239. }
  240. return null;
  241. }
  242. /**
  243. * Checks if the given string represents a valid skill
  244. *
  245. * @param skillname The name of the skill to check
  246. * @return true if this is a valid skill, false otherwise
  247. */
  248. public static boolean isSkill(String skillName) {
  249. if (getSkillType(skillName) != null) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. /**
  255. * Check if the player has any combat skill permissions.
  256. *
  257. * @param player The player to check permissions for
  258. * @return true if the player has combat skills, false otherwise
  259. */
  260. public static boolean hasCombatSkills(Player player) {
  261. if (Permissions.axes(player)
  262. || Permissions.archery(player)
  263. || Permissions.swords(player)
  264. || Permissions.taming(player)
  265. || Permissions.unarmed(player)) {
  266. return true;
  267. }
  268. return false;
  269. }
  270. /**
  271. * Check if the player has any gathering skill permissions.
  272. *
  273. * @param player The player to check permissions for
  274. * @return true if the player has gathering skills, false otherwise
  275. */
  276. public static boolean hasGatheringSkills(Player player) {
  277. if (Permissions.excavation(player)
  278. || Permissions.fishing(player)
  279. || Permissions.herbalism(player)
  280. || Permissions.mining(player)
  281. || Permissions.woodcutting(player)) {
  282. return true;
  283. }
  284. return false;
  285. }
  286. /**
  287. * Check if the player has any misc skill permissions.
  288. *
  289. * @param player The player to check permissions for
  290. * @return true if the player has misc skills, false otherwise
  291. */
  292. public static boolean hasMiscSkills(Player player) {
  293. if (Permissions.acrobatics(player) || Permissions.repair(player)) {
  294. return true;
  295. }
  296. return false;
  297. }
  298. /**
  299. * Handle tool durability loss from abilities.
  300. *
  301. * @param inHand The item to damage
  302. * @param durabilityLoss The durability to remove from the item
  303. */
  304. public static void abilityDurabilityLoss(ItemStack inHand, int durabilityLoss) {
  305. if (Config.getInstance().getAbilitiesDamageTools()) {
  306. if (inHand.containsEnchantment(Enchantment.DURABILITY)) {
  307. int level = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
  308. if (Misc.getRandom().nextInt(level + 1) > 0) {
  309. return;
  310. }
  311. }
  312. inHand.setDurability((short) (inHand.getDurability() + durabilityLoss));
  313. }
  314. }
  315. /**
  316. * Check to see if an ability can be activated.
  317. *
  318. * @param player The player activating the ability
  319. * @param type The skill the ability is based on
  320. */
  321. public static void abilityCheck(Player player, SkillType type) {
  322. PlayerProfile profile = Users.getProfile(player);
  323. if (profile == null) {
  324. return;
  325. }
  326. ToolType tool = type.getTool();
  327. if (!profile.getToolPreparationMode(tool)) {
  328. return;
  329. }
  330. profile.setToolPreparationMode(tool, false);
  331. AbilityType ability = type.getAbility();
  332. /* Axes and Woodcutting are odd because they share the same tool.
  333. * We show them the too tired message when they take action.
  334. */
  335. if (type == SkillType.WOODCUTTING || type == SkillType.AXES) {
  336. if (!profile.getAbilityMode(ability) && !cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
  337. player.sendMessage(LocaleLoader.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player) + "s)");
  338. return;
  339. }
  340. }
  341. int ticks = 2 + (profile.getSkillLevel(type) / abilityLengthIncreaseLevel);
  342. if (Permissions.activationTwelve(player)) {
  343. ticks = ticks + 12;
  344. }
  345. else if (Permissions.activationEight(player)) {
  346. ticks = ticks + 8;
  347. }
  348. else if (Permissions.activationFour(player)) {
  349. ticks = ticks + 4;
  350. }
  351. int maxTicks = ability.getMaxTicks();
  352. if (maxTicks != 0 && ticks > maxTicks) {
  353. ticks = maxTicks;
  354. }
  355. if (!profile.getAbilityMode(ability) && cooldownOver(profile.getSkillDATS(ability), ability.getCooldown(), player)) {
  356. player.sendMessage(ability.getAbilityOn());
  357. Misc.sendSkillMessage(player, ability.getAbilityPlayer(player));
  358. profile.setSkillDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR));
  359. profile.setAbilityMode(ability, true);
  360. }
  361. }
  362. /**
  363. * Check to see if ability should be triggered.
  364. *
  365. * @param player The player using the ability
  366. * @param block The block modified by the ability
  367. * @param ability The ability to check
  368. * @return true if the ability should activate, false otherwise
  369. */
  370. public static boolean triggerCheck(Player player, Block block, AbilityType ability) {
  371. boolean activate = true;
  372. if (!ability.getPermissions(player)) {
  373. activate = false;
  374. return activate;
  375. }
  376. switch (ability) {
  377. case BERSERK:
  378. case GIGA_DRILL_BREAKER:
  379. case SUPER_BREAKER:
  380. case LEAF_BLOWER:
  381. if (!ability.blockCheck(block)) {
  382. activate = false;
  383. break;
  384. }
  385. if (!Misc.blockBreakSimulate(block, player, true)) {
  386. activate = false;
  387. break;
  388. }
  389. break;
  390. case GREEN_TERRA:
  391. if (!ability.blockCheck(block)) {
  392. activate = false;
  393. break;
  394. }
  395. break;
  396. default:
  397. activate = false;
  398. break;
  399. }
  400. return activate;
  401. }
  402. /**
  403. * Handle the processing of XP gain from individual skills.
  404. *
  405. * @param player The player that gained XP
  406. * @param profile The profile of the player gaining XP
  407. * @param type The type of skill to gain XP from
  408. * @param xp the amount of XP to gain
  409. */
  410. public static void xpProcessing(Player player, PlayerProfile profile, SkillType type, int xp) {
  411. if (player == null)
  412. return;
  413. if (type.getPermissions(player)) {
  414. if (Users.getPlayer(player) == null)
  415. return;
  416. if ((type.getMaxLevel() < profile.getSkillLevel(type) + 1) || (Misc.getPowerLevelCap() < Users.getPlayer(player).getPowerLevel() + 1))
  417. return;
  418. Users.getPlayer(player).addXP(type, xp);
  419. xpCheckSkill(type, player, profile);
  420. }
  421. }
  422. }