Skills.java 17 KB

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