SkillTools.java 20 KB

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