Repair.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. package com.gmail.nossr50.skills;
  2. import java.util.Map;
  3. import java.util.Map.Entry;
  4. import org.bukkit.ChatColor;
  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.bukkit.inventory.PlayerInventory;
  10. import org.getspout.spoutapi.SpoutManager;
  11. import org.getspout.spoutapi.player.SpoutPlayer;
  12. import com.gmail.nossr50.ItemChecks;
  13. import com.gmail.nossr50.Users;
  14. import com.gmail.nossr50.m;
  15. import com.gmail.nossr50.mcPermissions;
  16. import com.gmail.nossr50.config.LoadProperties;
  17. import com.gmail.nossr50.spout.SpoutSounds;
  18. import com.gmail.nossr50.datatypes.PlayerProfile;
  19. import com.gmail.nossr50.datatypes.SkillType;
  20. import com.gmail.nossr50.locale.mcLocale;
  21. public class Repair {
  22. /**
  23. * Handle all the item repair checks.
  24. *
  25. * @param player Player repairing the item
  26. * @param is The item being repaired
  27. */
  28. public static void repairCheck(Player player, ItemStack is) {
  29. PlayerProfile PP = Users.getProfile(player);
  30. short durabilityBefore = is.getDurability();
  31. PlayerInventory inventory = player.getInventory();
  32. int skillLevel = PP.getSkillLevel(SkillType.REPAIR);
  33. if (durabilityBefore > 0 && is.getAmount() == 1) {
  34. /*
  35. * REPAIR ARMOR
  36. */
  37. if (ItemChecks.isArmor(is) && LoadProperties.repairArmor && mcPermissions.getInstance().armorRepair(player)) {
  38. if (ItemChecks.isDiamondArmor(is) && inventory.contains(LoadProperties.rDiamond) && skillLevel >= LoadProperties.repairdiamondlevel && mcPermissions.getInstance().diamondRepair(player)) {
  39. inventory.removeItem(new ItemStack(LoadProperties.rDiamond));
  40. repairItem(player, is);
  41. xpHandler(player, PP, is, durabilityBefore, 6, true);
  42. }
  43. else if (ItemChecks.isIronArmor(is) && inventory.contains(LoadProperties.rIron) && skillLevel >= LoadProperties.repairIronLevel && mcPermissions.getInstance().ironRepair(player)) {
  44. inventory.removeItem(new ItemStack(LoadProperties.rIron));
  45. repairItem(player, is);
  46. xpHandler(player, PP, is, durabilityBefore, 2, true);
  47. }
  48. else if (ItemChecks.isGoldArmor(is) && inventory.contains(LoadProperties.rGold) && skillLevel >= LoadProperties.repairGoldLevel && mcPermissions.getInstance().goldRepair(player)) {
  49. inventory.removeItem(new ItemStack(LoadProperties.rGold));
  50. repairItem(player, is);
  51. xpHandler(player, PP, is, durabilityBefore, 4, true);
  52. }
  53. else if (ItemChecks.isLeatherArmor(is) && inventory.contains(LoadProperties.rLeather)) {
  54. inventory.removeItem(new ItemStack(LoadProperties.rLeather));
  55. repairItem(player, is);
  56. xpHandler(player, PP, is, durabilityBefore, 1, true);
  57. }
  58. else {
  59. needMoreVespeneGas(is, player); //UNABLE TO REPAIR
  60. }
  61. }
  62. /*
  63. * REPAIR TOOLS
  64. */
  65. else if (ItemChecks.isTool(is) && LoadProperties.repairTools && mcPermissions.getInstance().toolRepair(player)) {
  66. if (ItemChecks.isStoneTool(is) && inventory.contains(LoadProperties.rStone) && skillLevel >= LoadProperties.repairStoneLevel && mcPermissions.getInstance().stoneRepair(player)) {
  67. inventory.removeItem(new ItemStack(LoadProperties.rStone));
  68. repairItem(player, is);
  69. xpHandler(player, PP, is, durabilityBefore, 2, false);
  70. }
  71. else if (ItemChecks.isWoodTool(is) && inventory.contains(LoadProperties.rWood)) {
  72. inventory.removeItem(new ItemStack(LoadProperties.rWood));
  73. repairItem(player, is);
  74. xpHandler(player, PP, is, durabilityBefore, 2, false);
  75. }
  76. else if (ItemChecks.isIronTool(is) && inventory.contains(LoadProperties.rIron) && skillLevel >= LoadProperties.repairIronLevel && mcPermissions.getInstance().ironRepair(player)) {
  77. inventory.removeItem(new ItemStack(LoadProperties.rIron));
  78. repairItem(player, is);
  79. xpHandler(player, PP, is, durabilityBefore, 1, true);
  80. }
  81. else if (ItemChecks.isDiamondTool(is) && inventory.contains(LoadProperties.rDiamond) && skillLevel >= LoadProperties.repairdiamondlevel && mcPermissions.getInstance().diamondRepair(player)) {
  82. inventory.removeItem(new ItemStack(LoadProperties.rDiamond));
  83. repairItem(player, is);
  84. xpHandler(player, PP, is, durabilityBefore, 1, true);
  85. }
  86. else if (ItemChecks.isGoldTool(is) && inventory.contains(LoadProperties.rGold) && skillLevel >= LoadProperties.repairGoldLevel && mcPermissions.getInstance().goldRepair(player)) {
  87. inventory.removeItem(new ItemStack(LoadProperties.rGold));
  88. repairItem(player, is);
  89. xpHandler(player, PP, is, durabilityBefore, 8, true);
  90. }
  91. else if (is.getType().equals(Material.BOW) && inventory.contains(LoadProperties.rString)){
  92. inventory.removeItem(new ItemStack(LoadProperties.rString));
  93. repairItem(player, is);
  94. xpHandler(player, PP, is, durabilityBefore, 2, false);
  95. }
  96. else {
  97. needMoreVespeneGas(is, player); //UNABLE TO REPAIR
  98. }
  99. }
  100. }
  101. else {
  102. player.sendMessage(mcLocale.getString("Skills.FullDurability"));
  103. }
  104. }
  105. /**
  106. * Handle the XP gain for repair events.
  107. *
  108. * @param player Player repairing the item
  109. * @param PP PlayerProfile of the repairing player
  110. * @param is Item being repaired
  111. * @param durabilityBefore Durability of the item before repair
  112. * @param modify Amount to modify the durability by
  113. * @param boost True if the modifier is a boost, false if the modifier is a reduction
  114. */
  115. private static void xpHandler(Player player, PlayerProfile PP, ItemStack is, short durabilityBefore, int modify, boolean boost) {
  116. short durabilityAfter = is.getDurability();
  117. short dif = (short) (durabilityBefore - durabilityAfter);
  118. if (boost) {
  119. dif = (short) (dif * modify);
  120. }
  121. else{
  122. dif = (short) (dif / modify);
  123. }
  124. if (ItemChecks.isShovel(is)) {
  125. dif = (short) (dif / 3);
  126. }
  127. else if(ItemChecks.isSword(is)) {
  128. dif = (short) (dif / 2);
  129. }
  130. else if(ItemChecks.isHoe(is)) {
  131. dif = (short) (dif / 2);
  132. }
  133. PP.addXP(SkillType.REPAIR, dif*10, player);
  134. Skills.XpCheckSkill(SkillType.REPAIR, player);
  135. //CLANG CLANG
  136. if (LoadProperties.spoutEnabled) {
  137. SpoutSounds.playRepairNoise(player);
  138. }
  139. }
  140. /**
  141. * Get current Arcane Forging rank.
  142. *
  143. * @param skillLevel The skill level of the player whose rank is being checked
  144. * @return The player's current Arcane Forging rank
  145. */
  146. public static int getArcaneForgingRank(int skillLevel) {
  147. if (skillLevel >= LoadProperties.arcaneRank4) {
  148. return 4;
  149. }
  150. else if (skillLevel >= LoadProperties.arcaneRank3) {
  151. return 3;
  152. }
  153. else if (skillLevel >= LoadProperties.arcaneRank2) {
  154. return 2;
  155. }
  156. else if (skillLevel >= LoadProperties.arcaneRank1) {
  157. return 1;
  158. }
  159. else {
  160. return 0;
  161. }
  162. }
  163. /**
  164. * Handles removing & downgrading enchants.
  165. *
  166. * @param player Player repairing the item
  167. * @param is Item being repaired
  168. */
  169. private static void addEnchants(Player player, ItemStack is) {
  170. Map<Enchantment, Integer> enchants = is.getEnchantments();
  171. if (enchants.size() == 0) {
  172. return;
  173. }
  174. int rank = getArcaneForgingRank(Users.getProfile(player).getSkillLevel(SkillType.REPAIR));
  175. if (rank == 0 || !mcPermissions.getInstance().arcaneForging(player)) {
  176. for (Enchantment x : enchants.keySet()) {
  177. is.removeEnchantment(x);
  178. }
  179. player.sendMessage(mcLocale.getString("Repair.LostEnchants"));
  180. return;
  181. }
  182. boolean downgraded = false;
  183. for (Entry<Enchantment, Integer> enchant : enchants.entrySet()) {
  184. Enchantment enchantment = enchant.getKey();
  185. if (Math.random() * 100 <= getEnchantChance(rank)) {
  186. int enchantLevel = enchant.getValue();
  187. if (LoadProperties.mayDowngradeEnchants && enchantLevel > 1) {
  188. if (Math.random() * 100 <= getDowngradeChance(rank)) {
  189. is.addEnchantment(enchantment, enchantLevel--);
  190. downgraded = true;
  191. }
  192. }
  193. }
  194. else {
  195. is.removeEnchantment(enchantment);
  196. }
  197. }
  198. Map<Enchantment, Integer> newEnchants = is.getEnchantments();
  199. if (newEnchants.isEmpty()) {
  200. player.sendMessage(mcLocale.getString("Repair.ArcaneFailed"));
  201. }
  202. else if (downgraded || newEnchants.size() < enchants.size()) {
  203. player.sendMessage(mcLocale.getString("Repair.Downgraded"));
  204. }
  205. else {
  206. player.sendMessage(mcLocale.getString("Repair.ArcanePerfect"));
  207. }
  208. }
  209. /**
  210. * Gets chance of keeping enchantment during repair.
  211. *
  212. * @param rank Arcane Forging rank
  213. * @return The chance of keeping the enchantment
  214. */
  215. public static int getEnchantChance(int rank) {
  216. switch (rank) {
  217. case 4:
  218. return LoadProperties.keepEnchantsRank4;
  219. case 3:
  220. return LoadProperties.keepEnchantsRank3;
  221. case 2:
  222. return LoadProperties.keepEnchantsRank2;
  223. case 1:
  224. return LoadProperties.keepEnchantsRank1;
  225. default:
  226. return 0;
  227. }
  228. }
  229. /**
  230. * Gets chance of enchantment being downgraded during repair.
  231. *
  232. * @param rank Arcane Forging rank
  233. * @return The chance of the enchantment being downgraded
  234. */
  235. public static int getDowngradeChance(int rank) {
  236. switch (rank) {
  237. case 4:
  238. return LoadProperties.downgradeRank4;
  239. case 3:
  240. return LoadProperties.downgradeRank3;
  241. case 2:
  242. return LoadProperties.downgradeRank2;
  243. case 1:
  244. return LoadProperties.downgradeRank1;
  245. default:
  246. return 100;
  247. }
  248. }
  249. /**
  250. * Computes repair bonuses.
  251. *
  252. * @param player The player repairing an item
  253. * @param durability The durability of the item being repaired
  254. * @param ramt The base amount of durability repaired to the item
  255. * @return The final amount of durability repaired to the item
  256. */
  257. private static short repairCalculate(Player player, short durability, int ramt) {
  258. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  259. float bonus = (float) skillLevel / 500;
  260. bonus = (ramt * bonus);
  261. ramt += bonus;
  262. if (checkPlayerProcRepair(player)) {
  263. ramt = (short) (ramt * 2);
  264. }
  265. durability -= ramt;
  266. if (durability < 0) {
  267. durability = 0;
  268. }
  269. return durability;
  270. }
  271. /**
  272. * Gets the base durability amount to repair an item.
  273. *
  274. * @param is The item being repaired
  275. * @param player The player repairing the item
  276. * @return The final amount of durability repaired to the item
  277. */
  278. private static short getRepairAmount(ItemStack is, Player player){
  279. short maxDurability = is.getType().getMaxDurability();
  280. int ramt = 0;
  281. if (ItemChecks.isShovel(is)) {
  282. ramt = maxDurability;
  283. }
  284. else if (ItemChecks.isHoe(is) || ItemChecks.isSword(is) || is.getType().equals(Material.SHEARS)) {
  285. ramt = maxDurability / 2;
  286. }
  287. else if (ItemChecks.isAxe(is) || ItemChecks.isMiningPick(is) || is.getType().equals(Material.BOW)) {
  288. ramt = maxDurability / 3;
  289. }
  290. else if (ItemChecks.isBoots(is)) {
  291. ramt = maxDurability / 4;
  292. }
  293. else if (ItemChecks.isHelmet(is)) {
  294. ramt = maxDurability / 5;
  295. }
  296. else if (ItemChecks.isPants(is)) {
  297. ramt = maxDurability / 7;
  298. }
  299. else if (ItemChecks.isChestplate(is)) {
  300. ramt = maxDurability / 8;
  301. }
  302. return repairCalculate(player, is.getDurability(), ramt);
  303. }
  304. /**
  305. * Informs a player that the repair has failed.
  306. *
  307. * @param is The item being repaired
  308. * @param player The player repairing the item
  309. */
  310. private static void needMoreVespeneGas(ItemStack is, Player player) {
  311. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  312. if (is.getAmount() != 1) {
  313. player.sendMessage(mcLocale.getString("Skills.StackedItems"));
  314. }
  315. else {
  316. if (ItemChecks.isDiamondTool(is) || ItemChecks.isDiamondArmor(is)) {
  317. if (skillLevel < LoadProperties.repairdiamondlevel) {
  318. player.sendMessage(mcLocale.getString("Skills.AdeptDiamond"));
  319. }
  320. else {
  321. player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.BLUE + m.prettyItemString(LoadProperties.rDiamond));
  322. }
  323. }
  324. else if (ItemChecks.isIronTool(is) || ItemChecks.isIronArmor(is)) {
  325. if (skillLevel < LoadProperties.repairIronLevel) {
  326. player.sendMessage(mcLocale.getString("Skills.AdeptIron"));
  327. }
  328. else {
  329. player.sendMessage(mcLocale.getString("Skills.NeedMore")+ " " + ChatColor.GRAY + m.prettyItemString(LoadProperties.rIron));
  330. }
  331. }
  332. else if (ItemChecks.isGoldTool(is) || ItemChecks.isGoldArmor(is)) {
  333. if (skillLevel < LoadProperties.repairGoldLevel) {
  334. player.sendMessage(mcLocale.getString("Skills.AdeptGold"));
  335. }
  336. else {
  337. player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.GOLD + m.prettyItemString(LoadProperties.rGold));
  338. }
  339. }
  340. else if (ItemChecks.isStoneTool(is)) {
  341. if (skillLevel < LoadProperties.repairStoneLevel) {
  342. player.sendMessage(mcLocale.getString("Skills.AdeptStone"));
  343. }
  344. else {
  345. player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.GRAY + m.prettyItemString(LoadProperties.rStone));
  346. }
  347. }
  348. else if (ItemChecks.isWoodTool(is)) {
  349. player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.DARK_GREEN + m.prettyItemString(LoadProperties.rWood));
  350. }
  351. else if (ItemChecks.isLeatherArmor(is)) {
  352. player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.YELLOW + m.prettyItemString(LoadProperties.rLeather));
  353. }
  354. else if (is.getType().equals(Material.BOW)) {
  355. player.sendMessage(mcLocale.getString("Skills.NeedMore") + " " + ChatColor.YELLOW + m.prettyItemString(LoadProperties.rString));
  356. }
  357. }
  358. }
  359. /**
  360. * Checks for Super Repair bonus.
  361. *
  362. * @param player The player repairing an item
  363. * @return true if bonus granted, false otherwise
  364. */
  365. public static boolean checkPlayerProcRepair(Player player) {
  366. final int MAX_BONUS_LEVEL = 1000;
  367. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  368. if ((skillLevel > MAX_BONUS_LEVEL || (Math.random() * 1000 <= skillLevel)) && mcPermissions.getInstance().repairBonus(player)) {
  369. player.sendMessage(mcLocale.getString("Skills.FeltEasy"));
  370. return true;
  371. }
  372. return false;
  373. }
  374. /**
  375. * Repairs an item.
  376. *
  377. * @param player The player repairing an item
  378. * @param enchants The enchantments on the item
  379. * @param enchantsLevel The level of the enchantments on the item
  380. */
  381. public static void repairItem(Player player, ItemStack is) {
  382. /* Handle the enchants */
  383. if (LoadProperties.mayLoseEnchants && !mcPermissions.getInstance().arcaneBypass(player)) {
  384. addEnchants(player, is);
  385. }
  386. is.setDurability(getRepairAmount(is, player));
  387. }
  388. /**
  389. * Handles notifications for placing an anvil.
  390. *
  391. * @param player The player placing the anvil
  392. * @param anvilID The item ID of the anvil block
  393. */
  394. public static void placedAnvilCheck(Player player, int anvilID) {
  395. PlayerProfile PP = Users.getProfile(player);
  396. if (!PP.getPlacedAnvil()) {
  397. if (LoadProperties.spoutEnabled) {
  398. SpoutPlayer sPlayer = SpoutManager.getPlayer(player);
  399. if (sPlayer.isSpoutCraftEnabled()) {
  400. sPlayer.sendNotification("[mcMMO] Anvil Placed", "Right click to repair!", Material.getMaterial(anvilID)); //TODO: Use Locale
  401. }
  402. }
  403. else {
  404. player.sendMessage(mcLocale.getString("mcBlockListener.PlacedAnvil"));
  405. }
  406. PP.togglePlacedAnvil();
  407. }
  408. }
  409. }