SkillUtils.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. package com.gmail.nossr50.util.skills;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  4. import com.gmail.nossr50.datatypes.experience.XPGainSource;
  5. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  6. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  7. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  8. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  9. import com.gmail.nossr50.locale.LocaleLoader;
  10. import com.gmail.nossr50.mcMMO;
  11. import com.gmail.nossr50.util.ItemUtils;
  12. import com.gmail.nossr50.util.Misc;
  13. import com.gmail.nossr50.util.StringUtils;
  14. import com.gmail.nossr50.util.player.NotificationManager;
  15. import org.bukkit.Location;
  16. import org.bukkit.Material;
  17. import org.bukkit.enchantments.Enchantment;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.inventory.ItemStack;
  20. import org.bukkit.inventory.Recipe;
  21. import org.bukkit.inventory.ShapedRecipe;
  22. import org.bukkit.inventory.ShapelessRecipe;
  23. import org.bukkit.inventory.meta.ItemMeta;
  24. import java.util.ArrayList;
  25. import java.util.List;
  26. public class SkillUtils {
  27. public static void applyXpGain(McMMOPlayer mcMMOPlayer, PrimarySkillType skill, float xp, XPGainReason xpGainReason) {
  28. mcMMOPlayer.beginXpGain(skill, xp, xpGainReason, XPGainSource.SELF);
  29. }
  30. public static void applyXpGain(McMMOPlayer mcMMOPlayer, PrimarySkillType skill, float xp, XPGainReason xpGainReason, XPGainSource xpGainSource) {
  31. mcMMOPlayer.beginXpGain(skill, xp, xpGainReason, xpGainSource);
  32. }
  33. /*
  34. * Skill Stat Calculations
  35. */
  36. public static String[] calculateLengthDisplayValues(Player player, float skillValue, PrimarySkillType skill) {
  37. int maxLength = skill.getAbility().getMaxLength();
  38. int abilityLengthVar = AdvancedConfig.getInstance().getAbilityLength();
  39. int abilityLengthCap = AdvancedConfig.getInstance().getAbilityLengthCap();
  40. int length;
  41. if(abilityLengthCap > 0)
  42. {
  43. length = (int) Math.min(abilityLengthCap, 2 + (skillValue / abilityLengthVar));
  44. } else {
  45. length = 2 + (int) (skillValue / abilityLengthVar);
  46. }
  47. int enduranceLength = PerksUtils.handleActivationPerks(player, length, maxLength);
  48. if (maxLength != 0) {
  49. length = Math.min(length, maxLength);
  50. }
  51. return new String[] { String.valueOf(length), String.valueOf(enduranceLength) };
  52. }
  53. /*
  54. * Others
  55. */
  56. public static int handleFoodSkills(Player player, int eventFoodLevel, SubSkillType subSkillType) {
  57. int curRank = RankUtils.getRank(player, subSkillType);
  58. int currentFoodLevel = player.getFoodLevel();
  59. int foodChange = eventFoodLevel - currentFoodLevel;
  60. foodChange+=curRank;
  61. return currentFoodLevel + foodChange;
  62. }
  63. /**
  64. * Calculate the time remaining until the cooldown expires.
  65. *
  66. * @param deactivatedTimeStamp Time of deactivation
  67. * @param cooldown The length of the cooldown
  68. * @param player The Player to check for cooldown perks
  69. *
  70. * @return the number of seconds remaining before the cooldown expires
  71. */
  72. public static int calculateTimeLeft(long deactivatedTimeStamp, int cooldown, Player player) {
  73. return (int) (((deactivatedTimeStamp + (PerksUtils.handleCooldownPerks(player, cooldown) * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR);
  74. }
  75. /**
  76. * Check if the cooldown has expired.
  77. * This does NOT account for cooldown perks!
  78. *
  79. * @param deactivatedTimeStamp Time of deactivation in seconds
  80. * @param cooldown The length of the cooldown in seconds
  81. *
  82. * @return true if the cooldown is expired
  83. */
  84. public static boolean cooldownExpired(long deactivatedTimeStamp, int cooldown) {
  85. return System.currentTimeMillis() >= (deactivatedTimeStamp + cooldown) * Misc.TIME_CONVERSION_FACTOR;
  86. }
  87. /**
  88. * Checks if the given string represents a valid skill
  89. *
  90. * @param skillName The name of the skill to check
  91. * @return true if this is a valid skill, false otherwise
  92. */
  93. public static boolean isSkill(String skillName) {
  94. return mcMMO.getConfigManager().getConfigLanguage().getTargetLanguage().equalsIgnoreCase("en_US") ? PrimarySkillType.getSkill(skillName) != null : isLocalizedSkill(skillName);
  95. }
  96. public static void sendSkillMessage(Player player, NotificationType notificationType, String key) {
  97. Location location = player.getLocation();
  98. for (Player otherPlayer : player.getWorld().getPlayers()) {
  99. if (otherPlayer != player && Misc.isNear(location, otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
  100. NotificationManager.sendNearbyPlayersInformation(player, notificationType, key, player.getName());
  101. }
  102. }
  103. }
  104. public static void handleAbilitySpeedIncrease(Player player) {
  105. ItemStack heldItem = player.getInventory().getItemInMainHand();
  106. if (heldItem == null || heldItem.getType() == Material.AIR) {
  107. return;
  108. }
  109. int efficiencyLevel = heldItem.getEnchantmentLevel(Enchantment.DIG_SPEED);
  110. ItemMeta itemMeta = heldItem.getItemMeta();
  111. List<String> itemLore = new ArrayList<String>();
  112. if (itemMeta.hasLore()) {
  113. itemLore = itemMeta.getLore();
  114. }
  115. itemLore.add("mcMMO Ability Tool");
  116. itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel + AdvancedConfig.getInstance().getEnchantBuff(), true);
  117. itemMeta.setLore(itemLore);
  118. heldItem.setItemMeta(itemMeta);
  119. /*else {
  120. int duration = 0;
  121. int amplifier = 0;
  122. if (player.hasPotionEffect(PotionEffectType.FAST_DIGGING)) {
  123. for (PotionEffect effect : player.getActivePotionEffects()) {
  124. if (effect.getType() == PotionEffectType.FAST_DIGGING) {
  125. duration = effect.getDuration();
  126. amplifier = effect.getAmplifier();
  127. break;
  128. }
  129. }
  130. }
  131. McMMOPlayer mcMMOPlayer = UserManager.getPlayer(player);
  132. PrimarySkillType skill = mcMMOPlayer.getAbilityMode(SuperAbilityType.SUPER_BREAKER) ? PrimarySkillType.MINING : PrimarySkillType.EXCAVATION;
  133. int abilityLengthVar = AdvancedConfig.getInstance().getAbilityLength();
  134. int abilityLengthCap = AdvancedConfig.getInstance().getAbilityLengthCap();
  135. int ticks;
  136. if(abilityLengthCap > 0)
  137. {
  138. ticks = PerksUtils.handleActivationPerks(player, Math.min(abilityLengthCap, 2 + (mcMMOPlayer.getSkillLevel(skill) / abilityLengthVar)),
  139. skill.getAbility().getMaxLength()) * Misc.TICK_CONVERSION_FACTOR;
  140. } else {
  141. ticks = PerksUtils.handleActivationPerks(player, 2 + ((mcMMOPlayer.getSkillLevel(skill)) / abilityLengthVar),
  142. skill.getAbility().getMaxLength()) * Misc.TICK_CONVERSION_FACTOR;
  143. }
  144. PotionEffect abilityBuff = new PotionEffect(PotionEffectType.FAST_DIGGING, duration + ticks, amplifier + 10);
  145. player.addPotionEffect(abilityBuff, true);
  146. }*/
  147. }
  148. public static void handleAbilitySpeedDecrease(Player player) {
  149. for (ItemStack item : player.getInventory().getContents()) {
  150. removeAbilityBuff(item);
  151. }
  152. }
  153. public static void removeAbilityBuff(ItemStack item) {
  154. if (item == null || item.getType() == Material.AIR || (!ItemUtils.isPickaxe(item) && !ItemUtils.isShovel(item)) || !item.containsEnchantment(Enchantment.DIG_SPEED)) {
  155. return;
  156. }
  157. ItemMeta itemMeta = item.getItemMeta();
  158. if (itemMeta.hasLore()) {
  159. List<String> itemLore = itemMeta.getLore();
  160. if (itemLore.remove("mcMMO Ability Tool")) {
  161. int efficiencyLevel = item.getEnchantmentLevel(Enchantment.DIG_SPEED);
  162. if (efficiencyLevel <= AdvancedConfig.getInstance().getEnchantBuff()) {
  163. itemMeta.removeEnchant(Enchantment.DIG_SPEED);
  164. }
  165. else {
  166. itemMeta.addEnchant(Enchantment.DIG_SPEED, efficiencyLevel - AdvancedConfig.getInstance().getEnchantBuff(), true);
  167. }
  168. itemMeta.setLore(itemLore);
  169. item.setItemMeta(itemMeta);
  170. }
  171. }
  172. }
  173. public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) {
  174. handleDurabilityChange(itemStack, durabilityModifier, 1.0);
  175. }
  176. /**
  177. * Modify the durability of an ItemStack.
  178. *
  179. * @param itemStack The ItemStack which durability should be modified
  180. * @param durabilityModifier the amount to modify the durability by
  181. * @param maxDamageModifier the amount to adjust the max damage by
  182. */
  183. public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier, double maxDamageModifier) {
  184. if (itemStack.hasItemMeta() && itemStack.getItemMeta().isUnbreakable()) {
  185. return;
  186. }
  187. Material type = itemStack.getType();
  188. short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability();
  189. durabilityModifier = (int) Math.min(durabilityModifier / (itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1), maxDurability * maxDamageModifier);
  190. itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability));
  191. }
  192. private static boolean isLocalizedSkill(String skillName) {
  193. for (PrimarySkillType skill : PrimarySkillType.values()) {
  194. if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"))) {
  195. return true;
  196. }
  197. }
  198. return false;
  199. }
  200. protected static Material getRepairAndSalvageItem(ItemStack inHand) {
  201. if (ItemUtils.isDiamondTool(inHand) || ItemUtils.isDiamondArmor(inHand)) {
  202. return Material.DIAMOND;
  203. }
  204. else if (ItemUtils.isGoldTool(inHand) || ItemUtils.isGoldArmor(inHand)) {
  205. return Material.GOLD_INGOT;
  206. }
  207. else if (ItemUtils.isIronTool(inHand) || ItemUtils.isIronArmor(inHand)) {
  208. return Material.IRON_INGOT;
  209. }
  210. else if (ItemUtils.isStoneTool(inHand)) {
  211. return Material.COBBLESTONE;
  212. }
  213. else if (ItemUtils.isWoodTool(inHand)) {
  214. return Material.OAK_WOOD;
  215. }
  216. else if (ItemUtils.isLeatherArmor(inHand)) {
  217. return Material.LEATHER;
  218. }
  219. else if (ItemUtils.isStringTool(inHand)) {
  220. return Material.STRING;
  221. }
  222. else {
  223. return null;
  224. }
  225. }
  226. public static int getRepairAndSalvageQuantities(ItemStack item) {
  227. return getRepairAndSalvageQuantities(item, getRepairAndSalvageItem(item), (byte) -1);
  228. }
  229. public static int getRepairAndSalvageQuantities(ItemStack item, Material repairMaterial, byte repairMetadata) {
  230. // Workaround for Bukkit bug where damaged items would not return any recipes
  231. item = item.clone();
  232. item.setDurability((short) 0);
  233. int quantity = 0;
  234. List<Recipe> recipes = mcMMO.p.getServer().getRecipesFor(item);
  235. if (recipes.isEmpty()) {
  236. return quantity;
  237. }
  238. Recipe recipe = recipes.get(0);
  239. if (recipe instanceof ShapelessRecipe) {
  240. for (ItemStack ingredient : ((ShapelessRecipe) recipe).getIngredientList()) {
  241. if (ingredient != null && (repairMaterial == null || ingredient.getType() == repairMaterial) && (repairMetadata == -1 || ingredient.getType().equals(repairMaterial))) {
  242. quantity += ingredient.getAmount();
  243. }
  244. }
  245. }
  246. else if (recipe instanceof ShapedRecipe) {
  247. for (ItemStack ingredient : ((ShapedRecipe) recipe).getIngredientMap().values()) {
  248. if (ingredient != null && (repairMaterial == null || ingredient.getType() == repairMaterial) && (repairMetadata == -1 || ingredient.getType().equals(repairMaterial))) {
  249. quantity += ingredient.getAmount();
  250. }
  251. }
  252. }
  253. return quantity;
  254. }
  255. }