RepairManager.java 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. package com.gmail.nossr50.skills.repair;
  2. import com.gmail.nossr50.config.AdvancedConfig;
  3. import com.gmail.nossr50.config.Config;
  4. import com.gmail.nossr50.config.experience.ExperienceConfig;
  5. import com.gmail.nossr50.datatypes.experience.XPGainReason;
  6. import com.gmail.nossr50.datatypes.interactions.NotificationType;
  7. import com.gmail.nossr50.datatypes.player.McMMOPlayer;
  8. import com.gmail.nossr50.datatypes.skills.PrimarySkillType;
  9. import com.gmail.nossr50.datatypes.skills.SubSkillType;
  10. import com.gmail.nossr50.locale.LocaleLoader;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.skills.SkillManager;
  13. import com.gmail.nossr50.skills.repair.repairables.Repairable;
  14. import com.gmail.nossr50.util.EventUtils;
  15. import com.gmail.nossr50.util.Misc;
  16. import com.gmail.nossr50.util.Permissions;
  17. import com.gmail.nossr50.util.StringUtils;
  18. import com.gmail.nossr50.util.player.NotificationManager;
  19. import com.gmail.nossr50.util.random.RandomChanceSkillStatic;
  20. import com.gmail.nossr50.util.random.RandomChanceUtil;
  21. import com.gmail.nossr50.util.skills.RankUtils;
  22. import com.gmail.nossr50.util.skills.SkillActivationType;
  23. import com.gmail.nossr50.util.skills.SkillUtils;
  24. import com.gmail.nossr50.util.sounds.SoundManager;
  25. import com.gmail.nossr50.util.sounds.SoundType;
  26. import org.bukkit.Material;
  27. import org.bukkit.enchantments.Enchantment;
  28. import org.bukkit.entity.Player;
  29. import org.bukkit.inventory.ItemStack;
  30. import org.bukkit.inventory.PlayerInventory;
  31. import java.util.Map;
  32. import java.util.Map.Entry;
  33. public class RepairManager extends SkillManager {
  34. private boolean placedAnvil;
  35. private int lastClick;
  36. public RepairManager(McMMOPlayer mcMMOPlayer) {
  37. super(mcMMOPlayer, PrimarySkillType.REPAIR);
  38. }
  39. /**
  40. * Handles notifications for placing an anvil.
  41. */
  42. public void placedAnvilCheck() {
  43. Player player = getPlayer();
  44. if (getPlacedAnvil()) {
  45. return;
  46. }
  47. if (Config.getInstance().getRepairAnvilMessagesEnabled()) {
  48. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Repair.Listener.Anvil");
  49. }
  50. if (Config.getInstance().getRepairAnvilPlaceSoundsEnabled()) {
  51. SoundManager.sendSound(player, player.getLocation(), SoundType.ANVIL);
  52. }
  53. togglePlacedAnvil();
  54. }
  55. public void handleRepair(ItemStack item) {
  56. Player player = getPlayer();
  57. Repairable repairable = mcMMO.getRepairableManager().getRepairable(item.getType());
  58. if (item.getItemMeta().isUnbreakable()) {
  59. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Anvil.Unbreakable");
  60. return;
  61. }
  62. // Permissions checks on material and item types
  63. if (!Permissions.repairMaterialType(player, repairable.getRepairMaterialType())) {
  64. NotificationManager.sendPlayerInformation(player, NotificationType.NO_PERMISSION, "mcMMO.NoPermission");
  65. return;
  66. }
  67. if (!Permissions.repairItemType(player, repairable.getRepairItemType())) {
  68. NotificationManager.sendPlayerInformation(player, NotificationType.NO_PERMISSION, "mcMMO.NoPermission");
  69. return;
  70. }
  71. int skillLevel = getSkillLevel();
  72. int minimumRepairableLevel = repairable.getMinimumLevel();
  73. // Level check
  74. if (skillLevel < minimumRepairableLevel) {
  75. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Skills.Adept", String.valueOf(minimumRepairableLevel), StringUtils.getPrettyItemString(item.getType()));
  76. return;
  77. }
  78. PlayerInventory inventory = player.getInventory();
  79. Material repairMaterial = repairable.getRepairMaterial();
  80. ItemStack toRemove = new ItemStack(repairMaterial);
  81. short startDurability = item.getDurability();
  82. // Do not repair if at full durability
  83. if (startDurability <= 0) {
  84. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Skills.FullDurability");
  85. return;
  86. }
  87. // Check if they have the proper material to repair with
  88. if (!inventory.contains(repairMaterial)) {
  89. String prettyName = repairable.getRepairMaterialPrettyName() == null ? StringUtils.getPrettyItemString(repairMaterial) : repairable.getRepairMaterialPrettyName();
  90. String materialsNeeded = "";
  91. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Skills.NeedMore.Extra", prettyName, materialsNeeded);
  92. return;
  93. }
  94. // Do not repair stacked items
  95. if (item.getAmount() != 1) {
  96. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Skills.StackedItems");
  97. return;
  98. }
  99. // Clear ability buffs before trying to repair.
  100. SkillUtils.removeAbilityBuff(item);
  101. // Lets get down to business,
  102. // To defeat, the huns.
  103. int baseRepairAmount = repairable.getBaseRepairDurability(item); // Did they send me daughters?
  104. short newDurability = repairCalculate(startDurability, baseRepairAmount); // When I asked for sons?
  105. // Call event
  106. if (EventUtils.callRepairCheckEvent(player, (short) (startDurability - newDurability), toRemove, item).isCancelled()) {
  107. return;
  108. }
  109. // Handle the enchants
  110. if (ArcaneForging.arcaneForgingEnchantLoss && !Permissions.hasRepairEnchantBypassPerk(player)) {
  111. addEnchants(item);
  112. }
  113. // Remove the item
  114. toRemove = inventory.getItem(inventory.first(repairMaterial)).clone();
  115. toRemove.setAmount(1);
  116. inventory.removeItem(toRemove);
  117. // Give out XP like candy
  118. applyXpGain((float) ((getPercentageRepaired(startDurability, newDurability, repairable.getMaximumDurability())
  119. * repairable.getXpMultiplier())
  120. * ExperienceConfig.getInstance().getRepairXPBase()
  121. * ExperienceConfig.getInstance().getRepairXP(repairable.getRepairMaterialType())), XPGainReason.PVE);
  122. // BWONG BWONG BWONG
  123. if (Config.getInstance().getRepairAnvilUseSoundsEnabled()) {
  124. SoundManager.sendSound(player, player.getLocation(), SoundType.ANVIL);
  125. SoundManager.sendSound(player, player.getLocation(), SoundType.ITEM_BREAK);
  126. }
  127. // Repair the item!
  128. item.setDurability(newDurability);
  129. }
  130. private float getPercentageRepaired(short startDurability, short newDurability, short totalDurability) {
  131. return ((startDurability - newDurability) / (float) totalDurability);
  132. }
  133. /**
  134. * Check if the player has tried to use an Anvil before.
  135. *
  136. * @return true if the player has confirmed using an Anvil
  137. */
  138. public boolean checkConfirmation(boolean actualize) {
  139. Player player = getPlayer();
  140. long lastUse = getLastAnvilUse();
  141. if (!SkillUtils.cooldownExpired(lastUse, 3) || !Config.getInstance().getRepairConfirmRequired()) {
  142. return true;
  143. }
  144. if (!actualize) {
  145. return false;
  146. }
  147. actualizeLastAnvilUse();
  148. NotificationManager.sendPlayerInformation(player, NotificationType.SUBSKILL_MESSAGE, "Skills.ConfirmOrCancel", LocaleLoader.getString("Repair.Pretty.Name"));
  149. return false;
  150. }
  151. /**
  152. * Gets the Arcane Forging rank
  153. *
  154. * @return the current Arcane Forging rank
  155. */
  156. public int getArcaneForgingRank() {
  157. return RankUtils.getRank(getPlayer(), SubSkillType.REPAIR_ARCANE_FORGING);
  158. }
  159. /**
  160. * Gets chance of keeping enchantment during repair.
  161. *
  162. * @return The chance of keeping the enchantment
  163. */
  164. public double getKeepEnchantChance() {
  165. return AdvancedConfig.getInstance().getArcaneForgingKeepEnchantsChance(getArcaneForgingRank());
  166. }
  167. /**
  168. * Gets chance of enchantment being downgraded during repair.
  169. *
  170. * @return The chance of the enchantment being downgraded
  171. */
  172. public double getDowngradeEnchantChance() {
  173. return AdvancedConfig.getInstance().getArcaneForgingDowngradeChance(getArcaneForgingRank());
  174. }
  175. /**
  176. * Gets chance of keeping enchantment during repair.
  177. *
  178. * @return The chance of keeping the enchantment
  179. */
  180. /*public double getKeepEnchantChance() {
  181. int skillLevel = getSkillLevel();
  182. for (Tier tier : Tier.values()) {
  183. if (skillLevel >= tier.getLevel()) {
  184. return tier.getKeepEnchantChance();
  185. }
  186. }
  187. return 0;
  188. }*/
  189. /**
  190. * Gets chance of enchantment being downgraded during repair.
  191. *
  192. * @return The chance of the enchantment being downgraded
  193. */
  194. /*public double getDowngradeEnchantChance() {
  195. int skillLevel = getSkillLevel();
  196. for (Tier tier : Tier.values()) {
  197. if (skillLevel >= tier.getLevel()) {
  198. return tier.getDowngradeEnchantChance();
  199. }
  200. }
  201. return 100;
  202. }*/
  203. /**
  204. * Computes repair bonuses.
  205. *
  206. * @param durability The durability of the item being repaired
  207. * @param repairAmount The base amount of durability repaired to the item
  208. * @return The final amount of durability repaired to the item
  209. */
  210. private short repairCalculate(short durability, int repairAmount) {
  211. Player player = getPlayer();
  212. if (Permissions.isSubSkillEnabled(player, SubSkillType.REPAIR_REPAIR_MASTERY)
  213. && RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.REPAIR_REPAIR_MASTERY)) {
  214. double maxBonusCalc = Repair.repairMasteryMaxBonus / 100.0D;
  215. double skillLevelBonusCalc = (Repair.repairMasteryMaxBonus / Repair.repairMasteryMaxBonusLevel) * (getSkillLevel() / 100.0D);
  216. double bonus = repairAmount * Math.min(skillLevelBonusCalc, maxBonusCalc);
  217. repairAmount += bonus;
  218. }
  219. if (Permissions.isSubSkillEnabled(player, SubSkillType.REPAIR_SUPER_REPAIR) && checkPlayerProcRepair()) {
  220. repairAmount *= 2.0D;
  221. }
  222. if (repairAmount <= 0 || repairAmount > Short.MAX_VALUE) {
  223. repairAmount = Short.MAX_VALUE;
  224. }
  225. return (short) Math.max(durability - repairAmount, 0);
  226. }
  227. /**
  228. * Checks for Super Repair bonus.
  229. *
  230. * @return true if bonus granted, false otherwise
  231. */
  232. private boolean checkPlayerProcRepair() {
  233. if(!RankUtils.hasUnlockedSubskill(getPlayer(), SubSkillType.REPAIR_SUPER_REPAIR))
  234. return false;
  235. if (RandomChanceUtil.isActivationSuccessful(SkillActivationType.RANDOM_LINEAR_100_SCALE_WITH_CAP, SubSkillType.REPAIR_SUPER_REPAIR, getPlayer())) {
  236. NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE, "Repair.Skills.FeltEasy");
  237. return true;
  238. }
  239. return false;
  240. }
  241. /**
  242. * Handles removing & downgrading enchants.
  243. *
  244. * @param item Item being repaired
  245. */
  246. private void addEnchants(ItemStack item) {
  247. Player player = getPlayer();
  248. Map<Enchantment, Integer> enchants = item.getEnchantments();
  249. if (enchants.isEmpty()) {
  250. return;
  251. }
  252. if (Permissions.arcaneBypass(player)) {
  253. NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE, "Repair.Arcane.Perfect");
  254. return;
  255. }
  256. if (getArcaneForgingRank() == 0 || !Permissions.isSubSkillEnabled(player, SubSkillType.REPAIR_ARCANE_FORGING)) {
  257. for (Enchantment enchant : enchants.keySet()) {
  258. item.removeEnchantment(enchant);
  259. }
  260. NotificationManager.sendPlayerInformation(getPlayer(), NotificationType.SUBSKILL_MESSAGE_FAILED, "Repair.Arcane.Lost");
  261. return;
  262. }
  263. boolean downgraded = false;
  264. for (Entry<Enchantment, Integer> enchant : enchants.entrySet()) {
  265. int enchantLevel = enchant.getValue();
  266. if(!ExperienceConfig.getInstance().allowUnsafeEnchantments()) {
  267. if(enchantLevel > enchant.getKey().getMaxLevel()) {
  268. enchantLevel = enchant.getKey().getMaxLevel();
  269. item.addEnchantment(enchant.getKey(), enchantLevel);
  270. }
  271. }
  272. Enchantment enchantment = enchant.getKey();
  273. if (RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(getKeepEnchantChance(), getPlayer(), SubSkillType.REPAIR_ARCANE_FORGING))) {
  274. if (ArcaneForging.arcaneForgingDowngrades && enchantLevel > 1
  275. && (!RandomChanceUtil.checkRandomChanceExecutionSuccess(new RandomChanceSkillStatic(100 - getDowngradeEnchantChance(), getPlayer(), SubSkillType.REPAIR_ARCANE_FORGING)))) {
  276. item.addUnsafeEnchantment(enchantment, enchantLevel - 1);
  277. downgraded = true;
  278. }
  279. }
  280. else {
  281. item.removeEnchantment(enchantment);
  282. }
  283. }
  284. Map<Enchantment, Integer> newEnchants = item.getEnchantments();
  285. if (newEnchants.isEmpty()) {
  286. NotificationManager.sendPlayerInformationChatOnly(getPlayer(), "Repair.Arcane.Fail");
  287. }
  288. else if (downgraded || newEnchants.size() < enchants.size()) {
  289. NotificationManager.sendPlayerInformationChatOnly(getPlayer(), "Repair.Arcane.Downgrade");
  290. }
  291. else {
  292. NotificationManager.sendPlayerInformationChatOnly(getPlayer(), "Repair.Arcane.Perfect");
  293. }
  294. }
  295. /*
  296. * Repair Anvil Placement
  297. */
  298. public boolean getPlacedAnvil() {
  299. return placedAnvil;
  300. }
  301. public void togglePlacedAnvil() {
  302. placedAnvil = !placedAnvil;
  303. }
  304. /*
  305. * Repair Anvil Usage
  306. */
  307. public int getLastAnvilUse() {
  308. return lastClick;
  309. }
  310. public void setLastAnvilUse(int value) {
  311. lastClick = value;
  312. }
  313. public void actualizeLastAnvilUse() {
  314. lastClick = (int) (System.currentTimeMillis() / Misc.TIME_CONVERSION_FACTOR);
  315. }
  316. }