SkillUtils.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. package com.gmail.nossr50.util.skills;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.config.HiddenConfig;
  5. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  6. import com.gmail.nossr50.datatypes.experience.XPGainSource;
  7. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  8. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  9. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  10. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  11. import com.gmail.nossr50.datatypes.skills.SuperAbilityType;
  12. import com.gmail.nossr50.locale.LocaleLoader;
  13. import com.gmail.nossr50.mcMMO;
  14. import com.gmail.nossr50.util.ItemUtils;
  15. import com.gmail.nossr50.util.Misc;
  16. import com.gmail.nossr50.util.StringUtils;
  17. import com.gmail.nossr50.util.compat.layers.persistentdata.AbstractPersistentDataLayer;
  18. import com.gmail.nossr50.util.experience.MMOExperienceBarManager;
  19. import com.gmail.nossr50.util.player.NotificationManager;
  20. import org.bukkit.Bukkit;
  21. import org.bukkit.Location;
  22. import org.bukkit.Material;
  23. import org.bukkit.enchantments.Enchantment;
  24. import org.bukkit.entity.Player;
  25. import org.bukkit.inventory.ItemStack;
  26. import org.bukkit.inventory.Recipe;
  27. import org.bukkit.inventory.ShapedRecipe;
  28. import org.bukkit.inventory.ShapelessRecipe;
  29. import org.bukkit.inventory.meta.ItemMeta;
  30. import org.bukkit.potion.PotionEffect;
  31. import org.bukkit.potion.PotionEffectType;
  32. import org.jetbrains.annotations.NotNull;
  33. import org.jetbrains.annotations.Nullable;
  34. import java.util.Iterator;
  35. public class SkillUtils {
  36. public static void applyXpGain(McMMOPlayer mmoPlayer, PrimarySkillType primarySkillType, float xp, XPGainReason xpGainReason) {
  37. mmoPlayer.getExperienceManager().beginXpGain(mmoPlayer.getPlayer(), primarySkillType, xp, xpGainReason, XPGainSource.SELF);
  38. }
  39. public static void applyXpGain(McMMOPlayer mmoPlayer, PrimarySkillType primarySkillType, float xp, XPGainReason xpGainReason, XPGainSource xpGainSource) {
  40. mmoPlayer.getExperienceManager().beginXpGain(mmoPlayer.getPlayer(), primarySkillType, xp, xpGainReason, xpGainSource);
  41. }
  42. public static MMOExperienceBarManager.BarState asBarState(String str) {
  43. for(MMOExperienceBarManager.BarState barState : MMOExperienceBarManager.BarState.values()) {
  44. if(barState.toString().equalsIgnoreCase(str)) {
  45. return barState;
  46. }
  47. }
  48. mcMMO.p.getLogger().severe("Unable to read bar state for value " + str + " setting to default instead.");
  49. return MMOExperienceBarManager.BarState.NORMAL;
  50. }
  51. /*
  52. * Skill Stat Calculations
  53. */
  54. public static String[] calculateLengthDisplayValues(Player player, float skillValue, PrimarySkillType skill) {
  55. int maxLength = skill.getSuperAbilityType().getMaxLength();
  56. int abilityLengthVar = AdvancedConfig.getInstance().getAbilityLength();
  57. int abilityLengthCap = AdvancedConfig.getInstance().getAbilityLengthCap();
  58. int length;
  59. if(abilityLengthCap > 0)
  60. {
  61. length = (int) Math.min(abilityLengthCap, 2 + (skillValue / abilityLengthVar));
  62. } else {
  63. length = 2 + (int) (skillValue / abilityLengthVar);
  64. }
  65. int enduranceLength = PerksUtils.handleActivationPerks(player, length, maxLength);
  66. if (maxLength != 0) {
  67. length = Math.min(length, maxLength);
  68. }
  69. return new String[] { String.valueOf(length), String.valueOf(enduranceLength) };
  70. }
  71. /*
  72. * Others
  73. */
  74. public static int handleFoodSkills(Player player, int eventFoodLevel, SubSkillType subSkillType) {
  75. int curRank = RankUtils.getRank(player, subSkillType);
  76. int currentFoodLevel = player.getFoodLevel();
  77. int foodChange = eventFoodLevel - currentFoodLevel;
  78. foodChange+=curRank;
  79. return currentFoodLevel + foodChange;
  80. }
  81. /**
  82. * Calculate the time remaining until the cooldown expires.
  83. *
  84. * @param deactivatedTimeStamp Time of deactivation
  85. * @param cooldown The length of the cooldown
  86. * @param player The Player to check for cooldown perks
  87. *
  88. * @return the number of seconds remaining before the cooldown expires
  89. */
  90. public static int calculateTimeLeft(long deactivatedTimeStamp, int cooldown, Player player) {
  91. return (int) (((deactivatedTimeStamp + (PerksUtils.handleCooldownPerks(player, cooldown) * Misc.TIME_CONVERSION_FACTOR)) - System.currentTimeMillis()) / Misc.TIME_CONVERSION_FACTOR);
  92. }
  93. /**
  94. * Check if the cooldown has expired.
  95. * This does NOT account for cooldown perks!
  96. *
  97. * @param deactivatedTimeStamp Time of deactivation in seconds
  98. * @param cooldown The length of the cooldown in seconds
  99. *
  100. * @return true if the cooldown is expired
  101. */
  102. public static boolean cooldownExpired(long deactivatedTimeStamp, int cooldown) {
  103. return System.currentTimeMillis() >= (deactivatedTimeStamp + cooldown) * Misc.TIME_CONVERSION_FACTOR;
  104. }
  105. /**
  106. * Checks if the given string represents a valid skill
  107. *
  108. * @param skillName The name of the skill to check
  109. * @return true if this is a valid skill, false otherwise
  110. */
  111. public static boolean isSkill(String skillName) {
  112. return Config.getInstance().getLocale().equalsIgnoreCase("en_US") ? PrimarySkillType.getSkill(skillName) != null : isLocalizedSkill(skillName);
  113. }
  114. public static void sendSkillMessage(Player player, NotificationType notificationType, String key) {
  115. Location location = player.getLocation();
  116. for (Player otherPlayer : player.getWorld().getPlayers()) {
  117. if (otherPlayer != player && Misc.isNear(location, otherPlayer.getLocation(), Misc.SKILL_MESSAGE_MAX_SENDING_DISTANCE)) {
  118. NotificationManager.sendNearbyPlayersInformation(otherPlayer, notificationType, key, player.getName());
  119. }
  120. }
  121. }
  122. public static void handleAbilitySpeedIncrease(Player player) {
  123. if (HiddenConfig.getInstance().useEnchantmentBuffs()) {
  124. ItemStack heldItem = player.getInventory().getItemInMainHand();
  125. if(heldItem == null)
  126. return;
  127. if (!ItemUtils.canBeSuperAbilityDigBoosted(heldItem)) {
  128. return;
  129. }
  130. int originalDigSpeed = heldItem.getEnchantmentLevel(Enchantment.DIG_SPEED);
  131. //Add dig speed
  132. //Lore no longer gets added, no point to it afaik
  133. //ItemUtils.addAbilityLore(heldItem); //lore can be a secondary failsafe for 1.13 and below
  134. ItemUtils.addDigSpeedToItem(heldItem, heldItem.getEnchantmentLevel(Enchantment.DIG_SPEED));
  135. //1.13.2+ will have persistent metadata for this item
  136. AbstractPersistentDataLayer compatLayer = mcMMO.getCompatibilityManager().getPersistentDataLayer();
  137. compatLayer.setSuperAbilityBoostedItem(heldItem, originalDigSpeed);
  138. }
  139. else {
  140. int duration = 0;
  141. int amplifier = 0;
  142. if (player.hasPotionEffect(PotionEffectType.FAST_DIGGING)) {
  143. for (PotionEffect effect : player.getActivePotionEffects()) {
  144. if (effect.getType() == PotionEffectType.FAST_DIGGING) {
  145. duration = effect.getDuration();
  146. amplifier = effect.getAmplifier();
  147. break;
  148. }
  149. }
  150. }
  151. McMMOPlayer mmoPlayer = mcMMO.getUserManager().queryMcMMOPlayer(player);
  152. //Not Loaded
  153. if(mmoPlayer == null)
  154. return;
  155. PrimarySkillType skill = mmoPlayer.getSuperAbilityManager().getAbilityMode(SuperAbilityType.SUPER_BREAKER) ? PrimarySkillType.MINING : PrimarySkillType.EXCAVATION;
  156. int abilityLengthVar = AdvancedConfig.getInstance().getAbilityLength();
  157. int abilityLengthCap = AdvancedConfig.getInstance().getAbilityLengthCap();
  158. int ticks;
  159. if(abilityLengthCap > 0)
  160. {
  161. ticks = PerksUtils.handleActivationPerks(player, Math.min(abilityLengthCap, 2 + (mmoPlayer.getExperienceManager().getSkillLevel(skill) / abilityLengthVar)),
  162. skill.getSuperAbilityType().getMaxLength()) * Misc.TICK_CONVERSION_FACTOR;
  163. } else {
  164. ticks = PerksUtils.handleActivationPerks(player, 2 + ((mmoPlayer.getExperienceManager().getSkillLevel(skill)) / abilityLengthVar),
  165. skill.getSuperAbilityType().getMaxLength()) * Misc.TICK_CONVERSION_FACTOR;
  166. }
  167. PotionEffect abilityBuff = new PotionEffect(PotionEffectType.FAST_DIGGING, duration + ticks, amplifier + 10);
  168. player.addPotionEffect(abilityBuff, true);
  169. }
  170. }
  171. public static void removeAbilityBoostsFromInventory(@NotNull Player player) {
  172. for (ItemStack itemStack : player.getInventory().getContents()) {
  173. removeAbilityBuff(itemStack);
  174. }
  175. }
  176. public static void removeAbilityBuff(@Nullable ItemStack itemStack) {
  177. if(itemStack == null)
  178. return;
  179. if(!ItemUtils.canBeSuperAbilityDigBoosted(itemStack))
  180. return;
  181. //1.13.2+ will have persistent metadata for this itemStack
  182. AbstractPersistentDataLayer compatLayer = mcMMO.getCompatibilityManager().getPersistentDataLayer();
  183. if(compatLayer.isLegacyAbilityTool(itemStack)) {
  184. ItemMeta itemMeta = itemStack.getItemMeta();
  185. //TODO: can be optimized
  186. if(itemMeta.hasEnchant(Enchantment.DIG_SPEED)) {
  187. itemMeta.removeEnchant(Enchantment.DIG_SPEED);
  188. }
  189. itemStack.setItemMeta(itemMeta);
  190. ItemUtils.removeAbilityLore(itemStack);
  191. }
  192. if(compatLayer.isSuperAbilityBoosted(itemStack)) {
  193. compatLayer.removeBonusDigSpeedOnSuperAbilityTool(itemStack);
  194. }
  195. }
  196. public static void handleDurabilityChange(ItemStack itemStack, int durabilityModifier) {
  197. handleDurabilityChange(itemStack, durabilityModifier, 1.0);
  198. }
  199. /**
  200. * Modify the durability of an ItemStack.
  201. *
  202. * @param itemStack The ItemStack which durability should be modified
  203. * @param durabilityModifier the amount to modify the durability by
  204. * @param maxDamageModifier the amount to adjust the max damage by
  205. */
  206. public static void handleDurabilityChange(ItemStack itemStack, double durabilityModifier, double maxDamageModifier) {
  207. if(itemStack.getItemMeta() != null && itemStack.getItemMeta().isUnbreakable()) {
  208. return;
  209. }
  210. Material type = itemStack.getType();
  211. short maxDurability = mcMMO.getRepairableManager().isRepairable(type) ? mcMMO.getRepairableManager().getRepairable(type).getMaximumDurability() : type.getMaxDurability();
  212. durabilityModifier = (int) Math.min(durabilityModifier / (itemStack.getEnchantmentLevel(Enchantment.DURABILITY) + 1), maxDurability * maxDamageModifier);
  213. itemStack.setDurability((short) Math.min(itemStack.getDurability() + durabilityModifier, maxDurability));
  214. }
  215. private static boolean isLocalizedSkill(String skillName) {
  216. for (PrimarySkillType skill : PrimarySkillType.values()) {
  217. if (skillName.equalsIgnoreCase(LocaleLoader.getString(StringUtils.getCapitalized(skill.toString()) + ".SkillName"))) {
  218. return true;
  219. }
  220. }
  221. return false;
  222. }
  223. protected static Material getRepairAndSalvageItem(ItemStack inHand) {
  224. if (ItemUtils.isDiamondTool(inHand) || ItemUtils.isDiamondArmor(inHand)) {
  225. return Material.DIAMOND;
  226. }
  227. else if (ItemUtils.isGoldTool(inHand) || ItemUtils.isGoldArmor(inHand)) {
  228. return Material.GOLD_INGOT;
  229. }
  230. else if (ItemUtils.isIronTool(inHand) || ItemUtils.isIronArmor(inHand)) {
  231. return Material.IRON_INGOT;
  232. }
  233. else if (ItemUtils.isStoneTool(inHand)) {
  234. return Material.COBBLESTONE;
  235. }
  236. else if (ItemUtils.isWoodTool(inHand)) {
  237. return Material.OAK_WOOD;
  238. }
  239. else if (ItemUtils.isLeatherArmor(inHand)) {
  240. return Material.LEATHER;
  241. }
  242. else if (ItemUtils.isStringTool(inHand)) {
  243. return Material.STRING;
  244. }
  245. else {
  246. return null;
  247. }
  248. }
  249. public static int getRepairAndSalvageQuantities(ItemStack item) {
  250. return getRepairAndSalvageQuantities(item.getType(), getRepairAndSalvageItem(item));
  251. }
  252. public static int getRepairAndSalvageQuantities(Material itemMaterial, Material recipeMaterial) {
  253. int quantity = 0;
  254. if(mcMMO.getMaterialMapStore().isNetheriteTool(itemMaterial) || mcMMO.getMaterialMapStore().isNetheriteArmor(itemMaterial)) {
  255. //One netherite bar requires 4 netherite scraps
  256. return 4;
  257. }
  258. for(Iterator<? extends Recipe> recipeIterator = Bukkit.getServer().recipeIterator(); recipeIterator.hasNext();) {
  259. Recipe bukkitRecipe = recipeIterator.next();
  260. if(bukkitRecipe.getResult().getType() != itemMaterial)
  261. continue;
  262. if(bukkitRecipe instanceof ShapelessRecipe) {
  263. for (ItemStack ingredient : ((ShapelessRecipe) bukkitRecipe).getIngredientList()) {
  264. if (ingredient != null
  265. && (recipeMaterial == null || ingredient.getType() == recipeMaterial)
  266. && (ingredient.getType() == recipeMaterial)) {
  267. quantity += ingredient.getAmount();
  268. }
  269. }
  270. } else if(bukkitRecipe instanceof ShapedRecipe) {
  271. for (ItemStack ingredient : ((ShapedRecipe) bukkitRecipe).getIngredientMap().values()) {
  272. if (ingredient != null
  273. && (recipeMaterial == null || ingredient.getType() == recipeMaterial)
  274. && (ingredient.getType() == recipeMaterial)) {
  275. quantity += ingredient.getAmount();
  276. }
  277. }
  278. }
  279. }
  280. return quantity;
  281. }
  282. }