Repair.java 10 KB

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