Skills.java 18 KB

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