Skills.java 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453
  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.experience.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 (!PP.getAbilityInformed(ability) && curTime - (PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR) >= (ability.getCooldown() * TIME_CONVERSION_FACTOR)) {
  62. PP.setAbilityInformed(ability, 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()) {
  82. return;
  83. }
  84. for (AbilityType x : AbilityType.values()) {
  85. if (PP.getAbilityMode(x)) {
  86. return;
  87. }
  88. }
  89. /* Woodcutting & Axes need to be treated differently.
  90. * Basically the tool always needs to ready and we check to see if the cooldown is over when the user takes action
  91. */
  92. if (skill == SkillType.WOODCUTTING || skill == SkillType.AXES) {
  93. if (tool.inHand(inHand) && !PP.getToolPreparationMode(tool)) {
  94. if (LoadProperties.enableAbilityMessages) {
  95. player.sendMessage(tool.getRaiseTool());
  96. }
  97. PP.setToolPreparationATS(tool, System.currentTimeMillis());
  98. PP.setToolPreparationMode(tool, true);
  99. }
  100. }
  101. else if (ability.getPermissions(player) && tool.inHand(inHand) && !PP.getToolPreparationMode(tool)) {
  102. if (!PP.getAbilityMode(ability) && !cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown())) {
  103. player.sendMessage(mcLocale.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
  104. return;
  105. }
  106. if (LoadProperties.enableAbilityMessages) {
  107. player.sendMessage(tool.getRaiseTool());
  108. }
  109. PP.setToolPreparationATS(tool, System.currentTimeMillis());
  110. PP.setToolPreparationMode(tool, true);
  111. }
  112. }
  113. /**
  114. * Monitors various things relating to skill abilities.
  115. *
  116. * @param player The player using the skill
  117. * @param PP The profile of the player
  118. * @param curTime The current system time
  119. * @param skill The skill being monitored
  120. */
  121. public static void monitorSkill(Player player, PlayerProfile PP, long curTime, SkillType skill) {
  122. final int FOUR_SECONDS = 4000;
  123. ToolType tool = skill.getTool();
  124. AbilityType ability = skill.getAbility();
  125. if (PP.getToolPreparationMode(tool) && curTime - (PP.getToolPreparationATS(tool) * TIME_CONVERSION_FACTOR) >= FOUR_SECONDS) {
  126. PP.setToolPreparationMode(tool, false);
  127. player.sendMessage(tool.getLowerTool());
  128. }
  129. if (ability.getPermissions(player)) {
  130. if (PP.getAbilityMode(ability) && (PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR) <= curTime) {
  131. PP.setAbilityMode(ability, false);
  132. PP.setAbilityInformed(ability, false);
  133. player.sendMessage(ability.getAbilityOff());
  134. for (Player y : player.getWorld().getPlayers()) {
  135. if (y != player && m.isNear(player.getLocation(), y.getLocation(), MAX_DISTANCE_AWAY)) {
  136. y.sendMessage(ability.getAbilityPlayerOff(player));
  137. }
  138. }
  139. }
  140. }
  141. }
  142. /**
  143. * Update the leaderboards.
  144. *
  145. * @param skillType The skill to update the leaderboards for
  146. * @param player The player whose skill to update
  147. */
  148. public static void ProcessLeaderboardUpdate(SkillType skillType, Player player) {
  149. PlayerProfile PP = Users.getProfile(player);
  150. PlayerStat ps = new PlayerStat();
  151. if (skillType != SkillType.ALL) {
  152. ps.statVal = PP.getSkillLevel(skillType);
  153. }
  154. else {
  155. ps.statVal = m.getPowerLevel(player, PP);
  156. }
  157. ps.name = player.getName();
  158. Leaderboard.updateLeaderboard(ps, skillType);
  159. }
  160. /**
  161. * Check the XP of a skill.
  162. *
  163. * @param skillType The skill to check
  164. * @param player The player whose skill to check
  165. */
  166. public static void XpCheckSkill(SkillType skillType, Player player) {
  167. PlayerProfile PP = Users.getProfile(player);
  168. if (PP.getSkillXpLevel(skillType) >= PP.getXpToLevel(skillType)) {
  169. int skillups = 0;
  170. while (PP.getSkillXpLevel(skillType) >= PP.getXpToLevel(skillType)) {
  171. if (skillType.getMaxLevel() >= PP.getSkillLevel(skillType) + 1) {
  172. skillups++;
  173. PP.addLevels(skillType, 1);
  174. McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType);
  175. Bukkit.getPluginManager().callEvent(eventToFire);
  176. }
  177. else {
  178. PP.addLevels(skillType, 0);
  179. }
  180. }
  181. if (!LoadProperties.useMySQL) {
  182. ProcessLeaderboardUpdate(skillType, player);
  183. ProcessLeaderboardUpdate(SkillType.ALL, player);
  184. }
  185. String capitalized = m.getCapitalized(skillType.toString());
  186. /* Spout Stuff */
  187. if (LoadProperties.spoutEnabled && player instanceof SpoutPlayer) {
  188. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  189. if (sPlayer.isSpoutCraftEnabled()) {
  190. if (LoadProperties.xpbar) {
  191. SpoutStuff.updateXpBar(sPlayer);
  192. }
  193. SpoutStuff.levelUpNotification(skillType, sPlayer);
  194. }
  195. else {
  196. player.sendMessage(mcLocale.getString("Skills."+capitalized+"Up", new Object[] {String.valueOf(skillups), PP.getSkillLevel(skillType)}));
  197. }
  198. }
  199. else {
  200. player.sendMessage(mcLocale.getString("Skills."+capitalized+"Up", new Object[] {String.valueOf(skillups), PP.getSkillLevel(skillType)}));
  201. }
  202. }
  203. }
  204. /**
  205. * Check XP of all skills.
  206. *
  207. * @param player The player to check XP for.
  208. */
  209. public static void XpCheckAll(Player player) {
  210. for (SkillType x : SkillType.values()) {
  211. //Don't want to do anything with this one
  212. if (x == SkillType.ALL) {
  213. continue;
  214. }
  215. XpCheckSkill(x, player);
  216. }
  217. }
  218. /**
  219. * Get the skill represented by the given string
  220. *
  221. * @param skillName The name of the skill
  222. * @return the SkillType if it exists, null otherwise
  223. */
  224. public static SkillType getSkillType(String skillName) {
  225. for (SkillType x : SkillType.values()) {
  226. if (x.toString().equals(skillName.toUpperCase()))
  227. return x;
  228. }
  229. return null;
  230. }
  231. /**
  232. * Checks if the given string represents a valid skill
  233. *
  234. * @param skillname The name of the skill to check
  235. * @return true if this is a valid skill, false otherwise
  236. */
  237. public static boolean isSkill(String skillName) {
  238. if (getSkillType(skillName) != null) {
  239. return true;
  240. }
  241. else {
  242. return false;
  243. }
  244. }
  245. /**
  246. * Get the format string for
  247. * @param skillname
  248. * @param level
  249. * @param XP
  250. * @param XPToLevel
  251. * @return
  252. */
  253. public static String getSkillStats(String skillname, Integer level, Integer XP, Integer XPToLevel) {
  254. //TODO: Ditch this function in favor of better locale setup.
  255. ChatColor parColor = ChatColor.DARK_AQUA;
  256. ChatColor xpColor = ChatColor.GRAY;
  257. ChatColor LvlColor = ChatColor.GREEN;
  258. ChatColor skillColor = ChatColor.YELLOW;
  259. return skillColor + skillname + LvlColor + level + parColor +" XP" + "(" + xpColor + XP + parColor + "/" + xpColor + XPToLevel + parColor + ")";
  260. }
  261. /**
  262. * Check if the player has any combat skill permissions.
  263. *
  264. * @param player The player to check permissions for
  265. * @return true if the player has combat skills, false otherwise
  266. */
  267. public static boolean hasCombatSkills(Player player) {
  268. if (mcPermissions.getInstance().axes(player)
  269. || mcPermissions.getInstance().archery(player)
  270. || mcPermissions.getInstance().swords(player)
  271. || mcPermissions.getInstance().taming(player)
  272. || mcPermissions.getInstance().unarmed(player)) {
  273. return true;
  274. }
  275. else {
  276. return false;
  277. }
  278. }
  279. /**
  280. * Check if the player has any gathering skill permissions.
  281. *
  282. * @param player The player to check permissions for
  283. * @return true if the player has gathering skills, false otherwise
  284. */
  285. public static boolean hasGatheringSkills(Player player) {
  286. if (mcPermissions.getInstance().excavation(player)
  287. || mcPermissions.getInstance().fishing(player)
  288. || mcPermissions.getInstance().herbalism(player)
  289. || mcPermissions.getInstance().mining(player)
  290. || mcPermissions.getInstance().woodcutting(player)) {
  291. return true;
  292. }
  293. else {
  294. return false;
  295. }
  296. }
  297. /**
  298. * Check if the player has any misc skill permissions.
  299. *
  300. * @param player The player to check permissions for
  301. * @return true if the player has misc skills, false otherwise
  302. */
  303. public static boolean hasMiscSkills(Player player) {
  304. if (mcPermissions.getInstance().acrobatics(player) || mcPermissions.getInstance().repair(player)) {
  305. return true;
  306. }
  307. else {
  308. return false;
  309. }
  310. }
  311. /**
  312. * Handle tool durability loss from abilities.
  313. *
  314. * @param inhand The item to damage
  315. * @param durabilityLoss The durability to remove from the item
  316. */
  317. public static void abilityDurabilityLoss(ItemStack inhand, int durabilityLoss) {
  318. if (LoadProperties.toolsLoseDurabilityFromAbilities) {
  319. if (!inhand.containsEnchantment(Enchantment.DURABILITY)) {
  320. inhand.setDurability((short) (inhand.getDurability() + durabilityLoss));
  321. }
  322. }
  323. }
  324. /**
  325. * Check to see if an ability can be activated.
  326. *
  327. * @param player The player activating the ability
  328. * @param type The skill the ability is based on
  329. */
  330. public static void abilityCheck(Player player, SkillType type) {
  331. PlayerProfile PP = Users.getProfile(player);
  332. AbilityType ability = type.getAbility();
  333. ToolType tool = type.getTool();
  334. if (type.getTool().inHand(player.getItemInHand())) {
  335. if (PP.getToolPreparationMode(tool)) {
  336. PP.setToolPreparationMode(tool, false);
  337. }
  338. /* Axes and Woodcutting are odd because they share the same tool.
  339. * We show them the too tired message when they take action.
  340. */
  341. if (type == SkillType.WOODCUTTING || type == SkillType.AXES) {
  342. if (!PP.getAbilityMode(ability) && !cooldownOver(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown())) {
  343. player.sendMessage(mcLocale.getString("Skills.TooTired") + ChatColor.YELLOW + " (" + calculateTimeLeft(PP.getSkillDATS(ability) * TIME_CONVERSION_FACTOR, ability.getCooldown()) + "s)");
  344. return;
  345. }
  346. }
  347. int ticks = 2 + (PP.getSkillLevel(type) / 50);
  348. if (!PP.getAbilityMode(ability) && cooldownOver(PP.getSkillDATS(ability), ability.getCooldown())) {
  349. player.sendMessage(ability.getAbilityOn());
  350. for (Player y : player.getWorld().getPlayers()) {
  351. if (y != player && m.isNear(player.getLocation(), y.getLocation(), MAX_DISTANCE_AWAY)) {
  352. y.sendMessage(ability.getAbilityPlayer(player));
  353. }
  354. }
  355. PP.setSkillDATS(ability, System.currentTimeMillis()+(ticks * TIME_CONVERSION_FACTOR));
  356. PP.setAbilityMode(ability, true);
  357. }
  358. }
  359. }
  360. /**
  361. * Check to see if ability should be triggered.
  362. *
  363. * @param player The player using the ability
  364. * @param block The block modified by the ability
  365. * @param ability The ability to check
  366. * @return true if the ability should activate, false otherwise
  367. */
  368. public static boolean triggerCheck(Player player, Block block, AbilityType ability) {
  369. boolean activate = true;
  370. if (!ability.getPermissions(player)) {
  371. activate = false;
  372. return activate;
  373. }
  374. switch (ability) {
  375. case BERSERK:
  376. case GIGA_DRILL_BREAKER:
  377. case SUPER_BREAKER:
  378. case LEAF_BLOWER:
  379. if (!m.blockBreakSimulate(block, player, true)) {
  380. activate = false;
  381. break;
  382. }
  383. /* FALLS THROUGH */
  384. case GREEN_TERRA:
  385. if (!ability.blockCheck(block.getType())) {
  386. activate = false;
  387. break;
  388. }
  389. break;
  390. default:
  391. activate = false;
  392. break;
  393. }
  394. return activate;
  395. }
  396. }