Skills.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. package com.gmail.nossr50.skills;
  2. import org.bukkit.Bukkit;
  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.Leaderboard;
  11. import com.gmail.nossr50.Users;
  12. import com.gmail.nossr50.m;
  13. import com.gmail.nossr50.mcPermissions;
  14. import com.gmail.nossr50.config.LoadProperties;
  15. import com.gmail.nossr50.spout.SpoutStuff;
  16. import com.gmail.nossr50.datatypes.AbilityType;
  17. import com.gmail.nossr50.datatypes.PlayerProfile;
  18. import com.gmail.nossr50.datatypes.PlayerStat;
  19. import com.gmail.nossr50.datatypes.SkillType;
  20. import com.gmail.nossr50.datatypes.ToolType;
  21. import com.gmail.nossr50.events.McMMOPlayerLevelUpEvent;
  22. import com.gmail.nossr50.locale.mcLocale;
  23. public class Skills {
  24. private final static int TIME_CONVERSION_FACTOR = 1000;
  25. private final static int MAX_DISTANCE_AWAY = 10;
  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. * @return true if the cooldown is over, false otherwise
  32. */
  33. public static boolean cooldownOver(long oldTime, int cooldown){
  34. long currentTime = System.currentTimeMillis();
  35. if (currentTime - oldTime >= (cooldown * TIME_CONVERSION_FACTOR)) {
  36. return true;
  37. }
  38. else {
  39. return false;
  40. }
  41. }
  42. /**
  43. * Calculate the time remaining until the cooldown expires.
  44. *
  45. * @param deactivatedTimeStamp Time of deactivation
  46. * @param cooldown The length of the cooldown
  47. * @return the number of seconds remaining before the cooldown expires
  48. */
  49. public static int calculateTimeLeft(long deactivatedTimeStamp, int cooldown) {
  50. return (int) (((deactivatedTimeStamp + (cooldown * TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / TIME_CONVERSION_FACTOR);
  51. }
  52. /**
  53. * Sends a message to the player when the cooldown expires.
  54. *
  55. * @param player The player to send a message to
  56. * @param PP The profile of the player
  57. * @param curTime The current system time
  58. * @param ability The ability to watch cooldowns for
  59. */
  60. public static void watchCooldown(Player player, PlayerProfile PP, long curTime, AbilityType ability) {
  61. if (!ability.getInformed(PP) && curTime - (PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR) >= (ability.getCooldown() * TIME_CONVERSION_FACTOR)) {
  62. ability.setInformed(PP, true);
  63. player.sendMessage(ability.getAbilityRefresh());
  64. }
  65. }
  66. /**
  67. * Process activating abilities & readying the tool.
  68. *
  69. * @param player The player using the ability
  70. * @param skill The skill the ability is tied to
  71. */
  72. public static void activationCheck(Player player, SkillType skill) {
  73. if (LoadProperties.enableOnlyActivateWhenSneaking && !player.isSneaking()) {
  74. return;
  75. }
  76. PlayerProfile PP = Users.getProfile(player);
  77. AbilityType ability = skill.getAbility();
  78. ToolType tool = skill.getTool();
  79. ItemStack inHand = player.getItemInHand();
  80. /* Check if any abilities are active */
  81. if (!PP.getAbilityUse() || PP.getSuperBreakerMode() || PP.getSerratedStrikesMode() || PP.getTreeFellerMode() || PP.getGreenTerraMode() || PP.getBerserkMode() || PP.getGigaDrillBreakerMode()) {
  82. return;
  83. }
  84. /* Woodcutting & Axes need to be treated differently.
  85. * Basically the tool always needs to ready and we check to see if the cooldown is over when the user takes action
  86. */
  87. if (skill == SkillType.WOODCUTTING || skill == SkillType.AXES) {
  88. if (tool.inHand(inHand) && !tool.getToolMode(PP)) {
  89. if (LoadProperties.enableAbilityMessages) {
  90. player.sendMessage(tool.getRaiseTool());
  91. }
  92. tool.setToolATS(PP, System.currentTimeMillis());
  93. tool.setToolMode(PP, true);
  94. }
  95. }
  96. else if (ability.getPermissions(player) && tool.inHand(inHand) && !tool.getToolMode(PP)) {
  97. if (!ability.getMode(PP) && !cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown())) {
  98. player.sendMessage(mcLocale.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
  99. return;
  100. }
  101. if (LoadProperties.enableAbilityMessages) {
  102. player.sendMessage(tool.getRaiseTool());
  103. }
  104. tool.setToolATS(PP, System.currentTimeMillis());
  105. tool.setToolMode(PP, true);
  106. }
  107. }
  108. /**
  109. * Monitors various things relating to skill abilities.
  110. *
  111. * @param player The player using the skill
  112. * @param PP The profile of the player
  113. * @param curTime The current system time
  114. * @param skill The skill being monitored
  115. */
  116. public static void monitorSkill(Player player, PlayerProfile PP, long curTime, SkillType skill) {
  117. final int FOUR_SECONDS = 4000;
  118. ToolType tool = skill.getTool();
  119. AbilityType ability = skill.getAbility();
  120. if (tool.getToolMode(PP) && curTime - (tool.getToolATS(PP) * TIME_CONVERSION_FACTOR) >= FOUR_SECONDS) {
  121. tool.setToolMode(PP, false);
  122. player.sendMessage(tool.getLowerTool());
  123. }
  124. if (ability.getPermissions(player)) {
  125. if (ability.getMode(PP) && (PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR) <= curTime) {
  126. ability.setMode(PP, false);
  127. ability.setInformed(PP, false);
  128. player.sendMessage(ability.getAbilityOff());
  129. for (Player y : player.getWorld().getPlayers()) {
  130. if (y != player && m.isNear(player.getLocation(), y.getLocation(), MAX_DISTANCE_AWAY)) {
  131. y.sendMessage(ability.getAbilityPlayerOff(player));
  132. }
  133. }
  134. }
  135. }
  136. }
  137. /**
  138. * Update the leaderboards.
  139. *
  140. * @param skillType The skill to update the leaderboards for
  141. * @param player The player whose skill to update
  142. */
  143. public static void ProcessLeaderboardUpdate(SkillType skillType, Player player) {
  144. PlayerProfile PP = Users.getProfile(player);
  145. PlayerStat ps = new PlayerStat();
  146. if (skillType != SkillType.ALL) {
  147. ps.statVal = PP.getSkillLevel(skillType);
  148. }
  149. else {
  150. ps.statVal = m.getPowerLevel(player, PP);
  151. }
  152. ps.name = player.getName();
  153. Leaderboard.updateLeaderboard(ps, skillType);
  154. }
  155. /**
  156. * Check the XP of a skill.
  157. *
  158. * @param skillType The skill to check
  159. * @param player The player whose skill to check
  160. */
  161. public static void XpCheckSkill(SkillType skillType, Player player) {
  162. PlayerProfile PP = Users.getProfile(player);
  163. if (PP.getSkillXpLevel(skillType) >= PP.getXpToLevel(skillType)) {
  164. int skillups = 0;
  165. while (PP.getSkillXpLevel(skillType) >= PP.getXpToLevel(skillType)) {
  166. if (skillType.getMaxLevel() >= PP.getSkillLevel(skillType) + 1) {
  167. skillups++;
  168. PP.removeXP(skillType, PP.getXpToLevel(skillType));
  169. PP.skillUp(skillType, 1);
  170. McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType);
  171. Bukkit.getPluginManager().callEvent(eventToFire);
  172. }
  173. else {
  174. PP.removeXP(skillType, PP.getXpToLevel(skillType));
  175. }
  176. }
  177. if (!LoadProperties.useMySQL) {
  178. ProcessLeaderboardUpdate(skillType, player);
  179. ProcessLeaderboardUpdate(SkillType.ALL, player);
  180. }
  181. String capitalized = m.getCapitalized(skillType.toString());
  182. /* Spout Stuff */
  183. if (LoadProperties.spoutEnabled && player instanceof SpoutPlayer) {
  184. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  185. if (sPlayer.isSpoutCraftEnabled()) {
  186. if (LoadProperties.xpbar) {
  187. SpoutStuff.updateXpBar(sPlayer);
  188. }
  189. SpoutStuff.levelUpNotification(skillType, sPlayer);
  190. }
  191. else {
  192. player.sendMessage(mcLocale.getString("Skills."+capitalized+"Up", new Object[] {String.valueOf(skillups), PP.getSkillLevel(skillType)}));
  193. }
  194. }
  195. else {
  196. player.sendMessage(mcLocale.getString("Skills."+capitalized+"Up", new Object[] {String.valueOf(skillups), PP.getSkillLevel(skillType)}));
  197. }
  198. }
  199. }
  200. /**
  201. * Check XP of all skills.
  202. *
  203. * @param player The player to check XP for.
  204. */
  205. public static void XpCheckAll(Player player) {
  206. for (SkillType x : SkillType.values()) {
  207. //Don't want to do anything with this one
  208. if (x == SkillType.ALL) {
  209. continue;
  210. }
  211. XpCheckSkill(x, player);
  212. }
  213. }
  214. /**
  215. * Get the skill represented by the given string
  216. *
  217. * @param skillName The name of the skill
  218. * @return the SkillType if it exists, null otherwise
  219. */
  220. public static SkillType getSkillType(String skillName) {
  221. for (SkillType x : SkillType.values()) {
  222. if (x.toString().equals(skillName.toUpperCase()))
  223. return x;
  224. }
  225. return null;
  226. }
  227. /**
  228. * Checks if the given string represents a valid skill
  229. *
  230. * @param skillname The name of the skill to check
  231. * @return true if this is a valid skill, false otherwise
  232. */
  233. public static boolean isSkill(String skillName) {
  234. if (getSkillType(skillName) != null) {
  235. return true;
  236. }
  237. else {
  238. return false;
  239. }
  240. }
  241. /**
  242. * Get the format string for
  243. * @param skillname
  244. * @param level
  245. * @param XP
  246. * @param XPToLevel
  247. * @return
  248. */
  249. public static String getSkillStats(String skillname, Integer level, Integer XP, Integer XPToLevel) {
  250. //TODO: Ditch this function in favor of better locale setup.
  251. ChatColor parColor = ChatColor.DARK_AQUA;
  252. ChatColor xpColor = ChatColor.GRAY;
  253. ChatColor LvlColor = ChatColor.GREEN;
  254. ChatColor skillColor = ChatColor.YELLOW;
  255. return skillColor + skillname + LvlColor + level + parColor +" XP" + "(" + xpColor + XP + parColor + "/" + xpColor + XPToLevel + parColor + ")";
  256. }
  257. /**
  258. * Check if the player has any combat skill permissions.
  259. *
  260. * @param player The player to check permissions for
  261. * @return true if the player has combat skills, false otherwise
  262. */
  263. public static boolean hasCombatSkills(Player player) {
  264. if (mcPermissions.getInstance().axes(player)
  265. || mcPermissions.getInstance().archery(player)
  266. || mcPermissions.getInstance().swords(player)
  267. || mcPermissions.getInstance().taming(player)
  268. || mcPermissions.getInstance().unarmed(player)) {
  269. return true;
  270. }
  271. else {
  272. return false;
  273. }
  274. }
  275. /**
  276. * Check if the player has any gathering skill permissions.
  277. *
  278. * @param player The player to check permissions for
  279. * @return true if the player has gathering skills, false otherwise
  280. */
  281. public static boolean hasGatheringSkills(Player player) {
  282. if (mcPermissions.getInstance().excavation(player)
  283. || mcPermissions.getInstance().fishing(player)
  284. || mcPermissions.getInstance().herbalism(player)
  285. || mcPermissions.getInstance().mining(player)
  286. || mcPermissions.getInstance().woodcutting(player)) {
  287. return true;
  288. }
  289. else {
  290. return false;
  291. }
  292. }
  293. /**
  294. * Check if the player has any misc skill permissions.
  295. *
  296. * @param player The player to check permissions for
  297. * @return true if the player has misc skills, false otherwise
  298. */
  299. public static boolean hasMiscSkills(Player player) {
  300. if (mcPermissions.getInstance().acrobatics(player) || mcPermissions.getInstance().repair(player)) {
  301. return true;
  302. }
  303. else {
  304. return false;
  305. }
  306. }
  307. /**
  308. * Handle tool durability loss from abilities.
  309. *
  310. * @param inhand The item to damage
  311. * @param durabilityLoss The durability to remove from the item
  312. */
  313. public static void abilityDurabilityLoss(ItemStack inhand, int durabilityLoss) {
  314. if (LoadProperties.toolsLoseDurabilityFromAbilities) {
  315. if (!inhand.containsEnchantment(Enchantment.DURABILITY)) {
  316. inhand.setDurability((short) (inhand.getDurability() + durabilityLoss));
  317. }
  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. if (type.getTool().inHand(player.getItemInHand())) {
  330. if (type.getTool().getToolMode(PP)) {
  331. type.getTool().setToolMode(PP, false);
  332. }
  333. /* Axes and Woodcutting are odd because they share the same tool.
  334. * We show them the too tired message when they take action.
  335. */
  336. if (type == SkillType.WOODCUTTING || type == SkillType.AXES) {
  337. if (!ability.getMode(PP) && !cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown())) {
  338. player.sendMessage(mcLocale.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
  339. return;
  340. }
  341. }
  342. int ticks = 2 + (PP.getSkillLevel(type) / 50);
  343. if (!ability.getMode(PP) && cooldownOver(PP.getSkillDATS(ability), ability.getCooldown())) {
  344. player.sendMessage(ability.getAbilityOn());
  345. for (Player y : player.getWorld().getPlayers()) {
  346. if (y != player && m.isNear(player.getLocation(), y.getLocation(), MAX_DISTANCE_AWAY)) {
  347. y.sendMessage(ability.getAbilityPlayer(player));
  348. }
  349. }
  350. PP.setSkillDATS(ability, System.currentTimeMillis()+(ticks * TIME_CONVERSION_FACTOR));
  351. ability.setMode(PP, true);
  352. }
  353. }
  354. }
  355. /**
  356. * Check to see if ability should be triggered.
  357. *
  358. * @param player The player using the ability
  359. * @param block The block modified by the ability
  360. * @param ability The ability to check
  361. * @return true if the ability should activate, false otherwise
  362. */
  363. public static boolean triggerCheck(Player player, Block block, AbilityType ability) {
  364. boolean activate = true;
  365. if (!ability.getPermissions(player)) {
  366. activate = false;
  367. return activate;
  368. }
  369. switch (ability) {
  370. case BERSERK:
  371. case GIGA_DRILL_BREAKER:
  372. case SUPER_BREAKER:
  373. case LEAF_BLOWER:
  374. if (!m.blockBreakSimulate(block, player, true)) {
  375. activate = false;
  376. break;
  377. }
  378. case GREEN_TERRA:
  379. if (!ability.blockCheck(block.getType())) {
  380. activate = false;
  381. break;
  382. }
  383. default:
  384. activate = false;
  385. break;
  386. }
  387. return activate;
  388. }
  389. }