SkillTools.java 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  1. package com.gmail.nossr50.skills.utilities;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import org.bukkit.Material;
  5. import org.bukkit.block.Block;
  6. import org.bukkit.enchantments.Enchantment;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.event.entity.FoodLevelChangeEvent;
  9. import org.bukkit.inventory.ItemStack;
  10. import org.bukkit.inventory.PlayerInventory;
  11. import org.bukkit.inventory.meta.ItemMeta;
  12. import org.getspout.spoutapi.SpoutManager;
  13. import org.getspout.spoutapi.player.SpoutPlayer;
  14. import com.gmail.nossr50.mcMMO;
  15. import com.gmail.nossr50.config.AdvancedConfig;
  16. import com.gmail.nossr50.config.Config;
  17. import com.gmail.nossr50.datatypes.PlayerProfile;
  18. import com.gmail.nossr50.events.experience.McMMOPlayerLevelUpEvent;
  19. import com.gmail.nossr50.locale.LocaleLoader;
  20. import com.gmail.nossr50.mods.ModChecks;
  21. import com.gmail.nossr50.spout.SpoutConfig;
  22. import com.gmail.nossr50.spout.SpoutTools;
  23. import com.gmail.nossr50.util.Misc;
  24. import com.gmail.nossr50.util.Permissions;
  25. import com.gmail.nossr50.util.StringUtils;
  26. import com.gmail.nossr50.util.Users;
  27. public class SkillTools {
  28. static AdvancedConfig advancedConfig = AdvancedConfig.getInstance();
  29. public static int abilityLengthIncreaseLevel = advancedConfig.getAbilityLength();
  30. public static boolean abilitiesEnabled = Config.getInstance().getAbilitiesEnabled();
  31. public static final int LUCKY_SKILL_ACTIVATION_CHANCE = 75;
  32. public static final int NORMAL_SKILL_ACTIVATION_CHANCE = 100;
  33. public static void handleFoodSkills(Player player, SkillType skill, FoodLevelChangeEvent event, int baseLevel, int maxLevel, int rankChange) {
  34. int skillLevel = Users.getPlayer(player).getProfile().getSkillLevel(skill);
  35. int currentFoodLevel = player.getFoodLevel();
  36. int newFoodLevel = event.getFoodLevel();
  37. int foodChange = newFoodLevel - currentFoodLevel;
  38. for (int i = baseLevel; i <= maxLevel; i+= rankChange) {
  39. if (skillLevel >= i) {
  40. foodChange++;
  41. }
  42. }
  43. event.setFoodLevel(currentFoodLevel + foodChange);
  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.getPlayer(player).getProfile();
  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", 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. else if (ability == AbilityType.SUPER_BREAKER || ability == AbilityType.GIGA_DRILL_BREAKER) {
  180. handleAbilitySpeedDecrease(player);
  181. }
  182. profile.setAbilityMode(ability, false);
  183. profile.setAbilityInformed(ability, false);
  184. player.sendMessage(ability.getAbilityOff());
  185. sendSkillMessage(player, ability.getAbilityPlayerOff(player));
  186. }
  187. }
  188. }
  189. /**
  190. * Check the XP of a skill.
  191. *
  192. * @param skillType The skill to check
  193. * @param player The player whose skill to check
  194. * @param profile The profile of the player whose skill to check
  195. */
  196. public static void xpCheckSkill(SkillType skillType, Player player, PlayerProfile profile) {
  197. int skillups = 0;
  198. if (profile.getSkillXpLevel(skillType) >= profile.getXpToLevel(skillType)) {
  199. while (profile.getSkillXpLevel(skillType) >= profile.getXpToLevel(skillType)) {
  200. if ((skillType.getMaxLevel() >= profile.getSkillLevel(skillType) + 1) && (Misc.getPowerLevelCap() >= Users.getPlayer(player).getPowerLevel() + 1)) {
  201. profile.removeXp(skillType, profile.getXpToLevel(skillType));
  202. skillups++;
  203. profile.skillUp(skillType, 1);
  204. McMMOPlayerLevelUpEvent eventToFire = new McMMOPlayerLevelUpEvent(player, skillType);
  205. mcMMO.p.getServer().getPluginManager().callEvent(eventToFire);
  206. }
  207. else {
  208. profile.addLevels(skillType, 0);
  209. }
  210. }
  211. String capitalized = StringUtils.getCapitalized(skillType.toString());
  212. /* Spout Stuff */
  213. if (mcMMO.spoutEnabled) {
  214. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  215. if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) {
  216. SpoutTools.levelUpNotification(skillType, spoutPlayer);
  217. /* Update custom titles */
  218. if (SpoutConfig.getInstance().getShowPowerLevel()) {
  219. spoutPlayer.setTitle(LocaleLoader.getString("Spout.Title", spoutPlayer.getName(), Users.getPlayer(player).getPowerLevel()));
  220. }
  221. }
  222. else {
  223. player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", skillups, profile.getSkillLevel(skillType)));
  224. }
  225. }
  226. else {
  227. player.sendMessage(LocaleLoader.getString(capitalized + ".Skillup", skillups, profile.getSkillLevel(skillType)));
  228. }
  229. }
  230. if (mcMMO.spoutEnabled) {
  231. SpoutPlayer spoutPlayer = SpoutManager.getPlayer(player);
  232. if (spoutPlayer != null && spoutPlayer.isSpoutCraftEnabled()) {
  233. if (SpoutConfig.getInstance().getXPBarEnabled()) {
  234. profile.getSpoutHud().updateXpBar();
  235. }
  236. }
  237. }
  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 (!Config.getInstance().getLocale().equalsIgnoreCase("en_US")) {
  247. return isLocalizedSkill(skillName);
  248. }
  249. if (SkillType.getSkill(skillName) != null) {
  250. return true;
  251. }
  252. return false;
  253. }
  254. private static boolean isLocalizedSkill(String skillName) {
  255. for (SkillType skill : SkillType.values()) {
  256. if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"))) {
  257. return true;
  258. }
  259. }
  260. return false;
  261. }
  262. public static String getSkillName(SkillType skill) {
  263. if (!Config.getInstance().getLocale().equalsIgnoreCase("en_US")) {
  264. return StringUtils.getCapitalized(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"));
  265. }
  266. return StringUtils.getCapitalized(skill.toString());
  267. }
  268. /**
  269. * Check if the player has any combat skill permissions.
  270. *
  271. * @param player The player to check permissions for
  272. * @return true if the player has combat skills, false otherwise
  273. */
  274. public static boolean hasCombatSkills(Player player) {
  275. if (Permissions.axes(player)
  276. || Permissions.archery(player)
  277. || Permissions.swords(player)
  278. || Permissions.taming(player)
  279. || Permissions.unarmed(player)) {
  280. return true;
  281. }
  282. return false;
  283. }
  284. /**
  285. * Check if the player has any gathering skill permissions.
  286. *
  287. * @param player The player to check permissions for
  288. * @return true if the player has gathering skills, false otherwise
  289. */
  290. public static boolean hasGatheringSkills(Player player) {
  291. if (Permissions.excavation(player)
  292. || Permissions.fishing(player)
  293. || Permissions.herbalism(player)
  294. || Permissions.mining(player)
  295. || Permissions.woodcutting(player)) {
  296. return true;
  297. }
  298. return false;
  299. }
  300. /**
  301. * Check if the player has any misc skill permissions.
  302. *
  303. * @param player The player to check permissions for
  304. * @return true if the player has misc skills, false otherwise
  305. */
  306. public static boolean hasMiscSkills(Player player) {
  307. if (Permissions.acrobatics(player) || Permissions.repair(player)) {
  308. return true;
  309. }
  310. return false;
  311. }
  312. /**
  313. * Handle tool durability loss from abilities.
  314. *
  315. * @param inHand The item to damage
  316. * @param durabilityLoss The durability to remove from the item
  317. */
  318. public static void abilityDurabilityLoss(ItemStack inHand, int durabilityLoss) {
  319. if (Config.getInstance().getAbilitiesDamageTools()) {
  320. if (inHand.containsEnchantment(Enchantment.DURABILITY)) {
  321. int level = inHand.getEnchantmentLevel(Enchantment.DURABILITY);
  322. if (Misc.getRandom().nextInt(level + 1) > 0) {
  323. return;
  324. }
  325. }
  326. inHand.setDurability((short) (inHand.getDurability() + durabilityLoss));
  327. }
  328. }
  329. /**
  330. * Check to see if an ability can be activated.
  331. *
  332. * @param player The player activating the ability
  333. * @param type The skill the ability is based on
  334. */
  335. public static void abilityCheck(Player player, SkillType type) {
  336. PlayerProfile profile = Users.getPlayer(player).getProfile();
  337. ToolType tool = type.getTool();
  338. AbilityType ability = type.getAbility();
  339. profile.setToolPreparationMode(tool, false);
  340. /* Axes and Woodcutting are odd because they share the same tool.
  341. * We show them the too tired message when they take action.
  342. */
  343. if (type == SkillType.WOODCUTTING || type == SkillType.AXES) {
  344. if (!profile.getAbilityMode(ability) && !cooldownOver(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)) {
  345. player.sendMessage(LocaleLoader.getString("Skills.TooTired", calculateTimeLeft(profile.getSkillDATS(ability) * Misc.TIME_CONVERSION_FACTOR, ability.getCooldown(), player)));
  346. return;
  347. }
  348. }
  349. int ticks = 2 + (profile.getSkillLevel(type) / abilityLengthIncreaseLevel);
  350. if (Permissions.activationTwelve(player)) {
  351. ticks = ticks + 12;
  352. }
  353. else if (Permissions.activationEight(player)) {
  354. ticks = ticks + 8;
  355. }
  356. else if (Permissions.activationFour(player)) {
  357. ticks = ticks + 4;
  358. }
  359. int maxTicks = ability.getMaxTicks();
  360. if (maxTicks != 0 && ticks > maxTicks) {
  361. ticks = maxTicks;
  362. }
  363. if (!profile.getAbilityMode(ability) && cooldownOver(profile.getSkillDATS(ability), ability.getCooldown(), player)) {
  364. player.sendMessage(ability.getAbilityOn());
  365. SkillTools.sendSkillMessage(player, ability.getAbilityPlayer(player));
  366. profile.setSkillDATS(ability, System.currentTimeMillis() + (ticks * Misc.TIME_CONVERSION_FACTOR));
  367. profile.setAbilityMode(ability, true);
  368. if (ability == AbilityType.BERSERK) {
  369. player.setCanPickupItems(false);
  370. }
  371. else if (ability == AbilityType.SUPER_BREAKER || ability == AbilityType.GIGA_DRILL_BREAKER) {
  372. handleAbilitySpeedIncrease(player);
  373. }
  374. }
  375. }
  376. /**
  377. * Check to see if ability should be triggered.
  378. *
  379. * @param player The player using the ability
  380. * @param block The block modified by the ability
  381. * @param ability The ability to check
  382. * @return true if the ability should activate, false otherwise
  383. */
  384. public static boolean triggerCheck(Player player, Block block, AbilityType ability) {
  385. boolean activate = true;
  386. switch (ability) {
  387. case BERSERK:
  388. case GIGA_DRILL_BREAKER:
  389. case SUPER_BREAKER:
  390. case LEAF_BLOWER:
  391. if (!ability.blockCheck(block)) {
  392. activate = false;
  393. break;
  394. }
  395. if (!Misc.blockBreakSimulate(block, player, true)) {
  396. activate = false;
  397. break;
  398. }
  399. break;
  400. case GREEN_TERRA:
  401. if (!ability.blockCheck(block)) {
  402. activate = false;
  403. break;
  404. }
  405. break;
  406. default:
  407. activate = false;
  408. break;
  409. }
  410. return activate;
  411. }
  412. /**
  413. * Calculate activation chance for a skill.
  414. *
  415. * @param isLucky true if the player has the appropriate "lucky" perk, false otherwise
  416. * @return the activation chance
  417. */
  418. public static int calculateActivationChance(boolean isLucky) {
  419. if (isLucky) {
  420. return LUCKY_SKILL_ACTIVATION_CHANCE;
  421. }
  422. return NORMAL_SKILL_ACTIVATION_CHANCE;
  423. }
  424. public static void sendSkillMessage(Player player, String message) {
  425. for (Player otherPlayer : player.getWorld().getPlayers()) {
  426. if (otherPlayer != player && Misc.isNear(player.getLocation(), otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
  427. otherPlayer.sendMessage(message);
  428. }
  429. }
  430. }
  431. /**
  432. * Check if a skill level is higher than the max bonus level of the ability.
  433. *
  434. * @param skillLevel Skill level to check
  435. * @param maxLevel Max level of the ability
  436. * @return whichever value is lower
  437. */
  438. public static int skillCheck(int skillLevel, int maxLevel) {
  439. //TODO: Could we just use Math.min(skillLevel, maxLevel) here?
  440. if (skillLevel > maxLevel) {
  441. return maxLevel;
  442. }
  443. return skillLevel;
  444. }
  445. public static void handleAbilitySpeedIncrease(Player player) {
  446. ItemStack heldItem = player.getItemInHand();
  447. if (heldItem == null || heldItem.getType() == Material.AIR ) {
  448. return;
  449. }
  450. int efficiencyLevel = heldItem.getEnchantmentLevel(Enchantment.DIG_SPEED);
  451. ItemMeta itemMeta = heldItem.getItemMeta();
  452. List<String> itemLore = new ArrayList<String>();
  453. if (itemMeta.hasLore()) {
  454. itemLore = itemMeta.getLore();
  455. }
  456. itemLore.add("mcMMO Ability Tool");
  457. itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel + 5, true);
  458. itemMeta.setLore(itemLore);
  459. heldItem.setItemMeta(itemMeta);
  460. }
  461. public static void handleAbilitySpeedDecrease(Player player) {
  462. PlayerInventory playerInventory = player.getInventory();
  463. for (int i = 0; i < playerInventory.getContents().length; i++) {
  464. ItemStack item = playerInventory.getItem(i);
  465. playerInventory.setItem(i, removeAbilityBuff(item));
  466. }
  467. }
  468. public static ItemStack removeAbilityBuff(ItemStack item) {
  469. if (item == null || item.getType() == Material.AIR ) {
  470. return item;
  471. }
  472. if (item.containsEnchantment(Enchantment.DIG_SPEED)) {
  473. ItemMeta itemMeta = item.getItemMeta();
  474. if (itemMeta.hasLore()) {
  475. List<String> itemLore = itemMeta.getLore();
  476. if (itemLore.remove("mcMMO Ability Tool")) {
  477. int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
  478. if (efficiencyLevel <= 5) {
  479. itemMeta.removeEnchant(Enchantment.DIG_SPEED);
  480. }
  481. else {
  482. itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - 5, true);
  483. }
  484. itemMeta.setLore(itemLore);
  485. item.setItemMeta(itemMeta);
  486. }
  487. }
  488. }
  489. return item;
  490. }
  491. }