Skills.java 20 KB

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