Repair.java 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. package com.gmail.nossr50.skills.repair;
  2. import java.util.Map;
  3. import java.util.Random;
  4. import java.util.Map.Entry;
  5. import org.bukkit.Material;
  6. import org.bukkit.enchantments.Enchantment;
  7. import org.bukkit.entity.Player;
  8. import org.bukkit.inventory.ItemStack;
  9. import org.getspout.spoutapi.SpoutManager;
  10. import org.getspout.spoutapi.player.SpoutPlayer;
  11. import com.gmail.nossr50.mcMMO;
  12. import com.gmail.nossr50.config.Config;
  13. import com.gmail.nossr50.spout.SpoutSounds;
  14. import com.gmail.nossr50.util.Permissions;
  15. import com.gmail.nossr50.util.Skills;
  16. import com.gmail.nossr50.util.Users;
  17. import com.gmail.nossr50.datatypes.PlayerProfile;
  18. import com.gmail.nossr50.datatypes.SkillType;
  19. import com.gmail.nossr50.locale.LocaleLoader;
  20. public class Repair {
  21. private static Random random = new Random();
  22. private static Config configInstance = Config.getInstance();
  23. private static Permissions permInstance = Permissions.getInstance();
  24. /**
  25. * Handle the XP gain for repair events.
  26. *
  27. * @param player Player repairing the item
  28. * @param PP PlayerProfile of the repairing player
  29. * @param is Item being repaired
  30. * @param durabilityBefore Durability of the item before repair
  31. * @param modify Amount to modify the durability by
  32. * @param boost True if the modifier is a boost, false if the modifier is a reduction
  33. */
  34. protected static void xpHandler(Player player, PlayerProfile PP, short durabilityBefore, short durabilityAfter, double modify) {
  35. short dif = (short) (durabilityBefore - durabilityAfter);
  36. dif = (short) (dif * modify);
  37. Skills.xpProcessing(player, PP, SkillType.REPAIR, dif * 10);
  38. //CLANG CLANG
  39. if (mcMMO.p.spoutEnabled) {
  40. SpoutSounds.playRepairNoise(player, mcMMO.p);
  41. }
  42. }
  43. /**
  44. * Get current Arcane Forging rank.
  45. *
  46. * @param skillLevel The skill level of the player whose rank is being checked
  47. * @return The player's current Arcane Forging rank
  48. */
  49. public static int getArcaneForgingRank(PlayerProfile PP) {
  50. int skillLevel = PP.getSkillLevel(SkillType.REPAIR);
  51. if (skillLevel >= configInstance.getArcaneForgingRankLevels4()) {
  52. return 4;
  53. }
  54. else if (skillLevel >= configInstance.getArcaneForgingRankLevels3()) {
  55. return 3;
  56. }
  57. else if (skillLevel >= configInstance.getArcaneForgingRankLevels2()) {
  58. return 2;
  59. }
  60. else if (skillLevel >= configInstance.getArcaneForgingRankLevels1()) {
  61. return 1;
  62. }
  63. else {
  64. return 0;
  65. }
  66. }
  67. /**
  68. * Handles removing & downgrading enchants.
  69. *
  70. * @param player Player repairing the item
  71. * @param is Item being repaired
  72. */
  73. protected static void addEnchants(Player player, ItemStack is) {
  74. Map<Enchantment, Integer> enchants = is.getEnchantments();
  75. if (enchants.size() == 0) {
  76. return;
  77. }
  78. int rank = getArcaneForgingRank(Users.getProfile(player));
  79. if (rank == 0 || !permInstance.arcaneForging(player)) {
  80. for (Enchantment x : enchants.keySet()) {
  81. is.removeEnchantment(x);
  82. }
  83. player.sendMessage(LocaleLoader.getString("Repair.Arcane.Lost"));
  84. return;
  85. }
  86. boolean downgraded = false;
  87. for (Entry<Enchantment, Integer> enchant : enchants.entrySet()) {
  88. Enchantment enchantment = enchant.getKey();
  89. if (random.nextInt(100) <= getEnchantChance(rank)) {
  90. int enchantLevel = enchant.getValue();
  91. if (configInstance.getArcaneForgingDowngradeEnabled() && enchantLevel > 1) {
  92. if (random.nextInt(100) <= getDowngradeChance(rank)) {
  93. is.addEnchantment(enchantment, enchantLevel--);
  94. downgraded = true;
  95. }
  96. }
  97. }
  98. else {
  99. is.removeEnchantment(enchantment);
  100. }
  101. }
  102. Map<Enchantment, Integer> newEnchants = is.getEnchantments();
  103. if (newEnchants.isEmpty()) {
  104. player.sendMessage(LocaleLoader.getString("Repair.Arcane.Fail"));
  105. }
  106. else if (downgraded || newEnchants.size() < enchants.size()) {
  107. player.sendMessage(LocaleLoader.getString("Repair.Arcane.Downgrade"));
  108. }
  109. else {
  110. player.sendMessage(LocaleLoader.getString("Repair.Arcane.Perfect"));
  111. }
  112. }
  113. /**
  114. * Gets chance of keeping enchantment during repair.
  115. *
  116. * @param rank Arcane Forging rank
  117. * @return The chance of keeping the enchantment
  118. */
  119. public static int getEnchantChance(int rank) {
  120. switch (rank) {
  121. case 4:
  122. return configInstance.getArcaneForgingKeepEnchantsChanceRank4();
  123. case 3:
  124. return configInstance.getArcaneForgingKeepEnchantsChanceRank3();
  125. case 2:
  126. return configInstance.getArcaneForgingKeepEnchantsChanceRank2();
  127. case 1:
  128. return configInstance.getArcaneForgingKeepEnchantsChanceRank1();
  129. default:
  130. return 0;
  131. }
  132. }
  133. /**
  134. * Gets chance of enchantment being downgraded during repair.
  135. *
  136. * @param rank Arcane Forging rank
  137. * @return The chance of the enchantment being downgraded
  138. */
  139. public static int getDowngradeChance(int rank) {
  140. switch (rank) {
  141. case 4:
  142. return configInstance.getArcaneForgingDowngradeChanceRank4();
  143. case 3:
  144. return configInstance.getArcaneForgingDowngradeChanceRank3();
  145. case 2:
  146. return configInstance.getArcaneForgingDowngradeChanceRank2();
  147. case 1:
  148. return configInstance.getArcaneForgingDowngradeChanceRank1();
  149. default:
  150. return 100;
  151. }
  152. }
  153. /**
  154. * Computes repair bonuses.
  155. *
  156. * @param player The player repairing an item
  157. * @param skillLevel the skillLevel of the player in Repair
  158. * @param durability The durability of the item being repaired
  159. * @param repairAmount The base amount of durability repaired to the item
  160. * @return The final amount of durability repaired to the item
  161. */
  162. protected static short repairCalculate(Player player, int skillLevel, short durability, int repairAmount) {
  163. float bonus = (float) skillLevel / 500;
  164. if (permInstance.repairMastery(player)) {
  165. bonus = (repairAmount * bonus);
  166. repairAmount += bonus;
  167. }
  168. if (checkPlayerProcRepair(player)) {
  169. repairAmount = (short) (repairAmount * 2);
  170. }
  171. durability -= repairAmount;
  172. if (durability < 0) {
  173. durability = 0;
  174. }
  175. return durability;
  176. }
  177. /**
  178. * Checks for Super Repair bonus.
  179. *
  180. * @param player The player repairing an item
  181. * @return true if bonus granted, false otherwise
  182. */
  183. public static boolean checkPlayerProcRepair(Player player) {
  184. final int MAX_BONUS_LEVEL = 1000;
  185. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  186. if ((skillLevel > MAX_BONUS_LEVEL || random.nextInt(1000) <= skillLevel) && permInstance.repairBonus(player)) {
  187. player.sendMessage(LocaleLoader.getString("Repair.Skills.FeltEasy"));
  188. return true;
  189. }
  190. return false;
  191. }
  192. /**
  193. * Handles notifications for placing an anvil.
  194. *
  195. * @param player The player placing the anvil
  196. * @param anvilID The item ID of the anvil block
  197. */
  198. public static void placedAnvilCheck(Player player, int anvilID) {
  199. PlayerProfile PP = Users.getProfile(player);
  200. if (!PP.getPlacedAnvil()) {
  201. if (mcMMO.p.spoutEnabled) {
  202. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  203. if (sPlayer.isSpoutCraftEnabled()) {
  204. sPlayer.sendNotification("[mcMMO] Anvil Placed", "Right click to repair!", Material.getMaterial(anvilID)); //TODO: Use Locale
  205. }
  206. }
  207. else {
  208. player.sendMessage(LocaleLoader.getString("Repair.Listener.Anvil"));
  209. }
  210. PP.togglePlacedAnvil();
  211. }
  212. }
  213. }