Skills.java 16 KB

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