Skills.java 18 KB

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