Skills.java 19 KB

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