Repair.java 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. This file is part of mcMMO.
  3. mcMMO is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 3 of the License, or
  6. (at your option) any later version.
  7. mcMMO is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with mcMMO. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. package com.gmail.nossr50.skills;
  15. import org.bukkit.ChatColor;
  16. import org.bukkit.block.Block;
  17. import org.bukkit.enchantments.Enchantment;
  18. import org.bukkit.entity.Player;
  19. import org.bukkit.inventory.ItemStack;
  20. import org.bukkit.inventory.PlayerInventory;
  21. import com.gmail.nossr50.Users;
  22. import com.gmail.nossr50.m;
  23. import com.gmail.nossr50.mcPermissions;
  24. import com.gmail.nossr50.config.LoadProperties;
  25. import com.gmail.nossr50.spout.SpoutStuff;
  26. import com.gmail.nossr50.datatypes.PlayerProfile;
  27. import com.gmail.nossr50.datatypes.SkillType;
  28. import com.gmail.nossr50.locale.mcLocale;
  29. public class Repair {
  30. private static int rGold = LoadProperties.rGold;
  31. private static String nGold = LoadProperties.nGold;
  32. private static int rStone = LoadProperties.rStone;
  33. private static String nStone = LoadProperties.nStone;
  34. private static int rWood = LoadProperties.rWood;
  35. private static String nWood = LoadProperties.nWood;
  36. private static int rDiamond = LoadProperties.rDiamond;
  37. private static String nDiamond = LoadProperties.nDiamond;
  38. private static int rIron = LoadProperties.rIron;
  39. private static String nIron = LoadProperties.nIron;
  40. private static int rString = LoadProperties.rString;
  41. private static String nString = LoadProperties.nString;
  42. private static int rLeather = LoadProperties.rLeather;
  43. private static String nLeather = LoadProperties.nLeather;
  44. private static int dLevel = LoadProperties.repairdiamondlevel;
  45. private static int iLevel = LoadProperties.repairIronLevel;
  46. private static int gLevel = LoadProperties.repairGoldLevel;
  47. private static int sLevel = LoadProperties.repairStoneLevel;
  48. private static boolean spout = LoadProperties.spoutEnabled;
  49. public static void repairCheck(Player player, ItemStack is, Block block){
  50. PlayerProfile PP = Users.getProfile(player);
  51. short durabilityBefore = is.getDurability();
  52. PlayerInventory inventory = player.getInventory();
  53. int skillLevel = PP.getSkillLevel(SkillType.REPAIR);
  54. //Stuff for keeping enchants
  55. Enchantment[] enchants = new Enchantment[is.getEnchantments().size()];
  56. int[] enchantsLevel = new int[is.getEnchantments().size()];
  57. int pos = 0;
  58. for(Enchantment x : is.getEnchantments().keySet())
  59. {
  60. enchants[pos] = x;
  61. enchantsLevel[pos] = is.getEnchantmentLevel(x);
  62. pos++;
  63. }
  64. if(block != null && mcPermissions.getInstance().repair(player)){
  65. if(durabilityBefore > 0 && is.getAmount() < 2){
  66. /*
  67. * REPAIR ARMOR
  68. */
  69. if(isArmor(is) && LoadProperties.repairArmor){
  70. //DIAMOND ARMOR
  71. if(isDiamondArmor(is) && inventory.contains(rDiamond) && skillLevel >= dLevel){
  72. inventory.removeItem(new ItemStack(rDiamond, 1));
  73. repairItem(player, enchants, enchantsLevel);
  74. xpHandler(player, PP, is, durabilityBefore, 6, true);
  75. }
  76. //IRON ARMOR
  77. else if (isIronArmor(is) && inventory.contains(rIron) && skillLevel >= iLevel){
  78. inventory.removeItem(new ItemStack(rIron, 1));
  79. repairItem(player, enchants, enchantsLevel);
  80. xpHandler(player, PP, is, durabilityBefore, 2, true);
  81. }
  82. //GOLD ARMOR
  83. else if (isGoldArmor(is) && inventory.contains(rGold) && skillLevel >= gLevel){
  84. inventory.removeItem(new ItemStack(rGold, 1));
  85. repairItem(player, enchants, enchantsLevel);
  86. xpHandler(player, PP, is, durabilityBefore, 4, true);
  87. }
  88. //LEATHER ARMOR
  89. else if (isLeatherArmor(is) && inventory.contains(rLeather)){
  90. inventory.removeItem(new ItemStack(rLeather, 1));
  91. repairItem(player, enchants, enchantsLevel);
  92. xpHandler(player, PP, is, durabilityBefore, 1, true);
  93. }
  94. //UNABLE TO REPAIR
  95. else {
  96. needMoreVespeneGas(is, player);
  97. }
  98. }
  99. /*
  100. * REPAIR TOOLS
  101. */
  102. if(isTools(is) && LoadProperties.repairTools){
  103. //STONE TOOLS
  104. if(isStoneTools(is) && inventory.contains(rStone) && skillLevel >= sLevel){
  105. inventory.removeItem(new ItemStack(rStone, 1));
  106. repairItem(player, enchants, enchantsLevel);
  107. xpHandler(player, PP, is, durabilityBefore, 2, false);
  108. }
  109. //WOOD TOOLS
  110. else if(isWoodTools(is) && inventory.contains(rWood)){
  111. inventory.removeItem(new ItemStack(rWood, 1));
  112. repairItem(player, enchants, enchantsLevel);
  113. xpHandler(player, PP, is, durabilityBefore, 2, false);
  114. }
  115. //IRON TOOLS
  116. else if(isIronTools(is) && inventory.contains(rIron) && skillLevel >= iLevel){
  117. inventory.removeItem(new ItemStack(rIron, 1));
  118. repairItem(player, enchants, enchantsLevel);
  119. xpHandler(player, PP, is, durabilityBefore, 1, true);
  120. }
  121. //DIAMOND TOOLS
  122. else if (isDiamondTools(is) && inventory.contains(rDiamond) && skillLevel >= dLevel){
  123. inventory.removeItem(new ItemStack(rDiamond, 1));
  124. repairItem(player, enchants, enchantsLevel);
  125. xpHandler(player, PP, is, durabilityBefore, 1, true);
  126. }
  127. //GOLD TOOLS
  128. else if(isGoldTools(is) && inventory.contains(rGold) && skillLevel >= gLevel){
  129. inventory.removeItem(new ItemStack(rGold, 1));
  130. repairItem(player, enchants, enchantsLevel);
  131. xpHandler(player, PP, is, durabilityBefore, 8, true);
  132. }
  133. //BOW
  134. else if(isBow(is) && inventory.contains(rString)){
  135. inventory.removeItem(new ItemStack(rString, 1));
  136. repairItem(player, enchants, enchantsLevel);
  137. xpHandler(player, PP, is, durabilityBefore, 2, false);
  138. }
  139. //UNABLE TO REPAIR
  140. else {
  141. needMoreVespeneGas(is, player);
  142. }
  143. }
  144. }
  145. else {
  146. player.sendMessage(mcLocale.getString("Skills.FullDurability"));
  147. }
  148. /*
  149. * GIVE SKILL IF THERE IS ENOUGH XP
  150. */
  151. Skills.XpCheckSkill(SkillType.REPAIR, player);
  152. }
  153. }
  154. public static void xpHandler(Player player, PlayerProfile PP, ItemStack is, short durabilityBefore, int modify, boolean boost)
  155. {
  156. short durabilityAfter = is.getDurability();
  157. short dif = (short) (durabilityBefore - durabilityAfter);
  158. if(boost)
  159. dif = (short) (dif * modify);
  160. if(!boost)
  161. dif = (short) (dif / modify);
  162. if(m.isShovel(is))
  163. dif = (short) (dif / 3);
  164. if(m.isSwords(is))
  165. dif = (short) (dif / 2);
  166. if(m.isHoe(is))
  167. dif = (short) (dif / 2);
  168. PP.addXP(SkillType.REPAIR, dif*10, player);
  169. //CLANG CLANG
  170. if(spout)
  171. SpoutStuff.playRepairNoise(player);
  172. }
  173. /**
  174. * Get current Arcane Forging rank.
  175. *
  176. * @param skillLevel The skill level of the player whose rank is being checked
  177. * @return The player's current Arcane Forging rank
  178. */
  179. public static int getArcaneForgingRank(int skillLevel)
  180. {
  181. if(skillLevel >= LoadProperties.arcaneRank4)
  182. return 4;
  183. if (skillLevel >= LoadProperties.arcaneRank3)
  184. return 3;
  185. if(skillLevel >= LoadProperties.arcaneRank2)
  186. return 2;
  187. if (skillLevel >= LoadProperties.arcaneRank1)
  188. return 1;
  189. return 0;
  190. }
  191. public static void addEnchants(ItemStack is, Enchantment[] enchants, int[] enchantsLvl, PlayerProfile PP, Player player){
  192. if(is.getEnchantments().keySet().size() == 0)
  193. return;
  194. int pos = 0;
  195. int rank = getArcaneForgingRank(PP.getSkillLevel(SkillType.REPAIR));
  196. if(rank == 0)
  197. {
  198. if(LoadProperties.mayLoseEnchants)
  199. {
  200. player.sendMessage(mcLocale.getString("Repair.LostEnchants"));
  201. for(Enchantment x : enchants)
  202. {
  203. is.removeEnchantment(x);
  204. }
  205. }
  206. return;
  207. }
  208. boolean failure = false, downgrade = false;
  209. if(LoadProperties.mayLoseEnchants)
  210. {
  211. for(Enchantment x : enchants)
  212. {
  213. //Remove enchant
  214. is.removeEnchantment(x);
  215. if(x.canEnchantItem(is))
  216. {
  217. if(Math.random() * 100 <= getEnchantChance(rank))
  218. {
  219. if(enchantsLvl[pos] > 1)
  220. {
  221. if(LoadProperties.mayDowngradeEnchants)
  222. {
  223. if(Math.random() * 100 <= getDowngradeChance(rank))
  224. {
  225. is.addEnchantment(x, enchantsLvl[pos]-1);
  226. downgrade = true;
  227. } else
  228. {
  229. is.addEnchantment(x, enchantsLvl[pos]);
  230. }
  231. }
  232. }
  233. else {
  234. is.addEnchantment(x, enchantsLvl[pos]);
  235. }
  236. } else {
  237. failure = true;
  238. }
  239. }
  240. pos++;
  241. }
  242. }
  243. if(failure == false && downgrade == false)
  244. {
  245. player.sendMessage(mcLocale.getString("Repair.ArcanePerfect"));
  246. } else {
  247. if(failure == true)
  248. player.sendMessage(mcLocale.getString("Repair.ArcaneFailed"));
  249. if(downgrade == true)
  250. player.sendMessage(mcLocale.getString("Repair.Downgraded"));
  251. }
  252. }
  253. /**
  254. * Gets chance of keeping enchantment during repair.
  255. *
  256. * @param rank Arcane Forging rank
  257. * @return The chance of keeping the enchantment
  258. */
  259. public static int getEnchantChance(int rank)
  260. {
  261. switch(rank)
  262. {
  263. case 4:
  264. return LoadProperties.keepEnchantsRank4;
  265. case 3:
  266. return LoadProperties.keepEnchantsRank3;
  267. case 2:
  268. return LoadProperties.keepEnchantsRank2;
  269. case 1:
  270. return LoadProperties.keepEnchantsRank1;
  271. default:
  272. return 0;
  273. }
  274. }
  275. /**
  276. * Gets chance of enchantment being downgraded during repair.
  277. *
  278. * @param rank Arcane Forging rank
  279. * @return The chance of the enchantment being downgraded
  280. */
  281. public static int getDowngradeChance(int rank)
  282. {
  283. switch(rank)
  284. {
  285. case 4:
  286. return LoadProperties.downgradeRank4;
  287. case 3:
  288. return LoadProperties.downgradeRank3;
  289. case 2:
  290. return LoadProperties.downgradeRank2;
  291. case 1:
  292. return LoadProperties.downgradeRank1;
  293. default:
  294. return 100;
  295. }
  296. }
  297. public static boolean isArmor(ItemStack is){
  298. return is.getTypeId() == 306 || is.getTypeId() == 307 ||is.getTypeId() == 308 ||is.getTypeId() == 309 || //IRON
  299. is.getTypeId() == 310 ||is.getTypeId() == 311 ||is.getTypeId() == 312 ||is.getTypeId() == 313 || //DIAMOND
  300. is.getTypeId() == 314 || is.getTypeId() == 315 || is.getTypeId() == 316 || is.getTypeId() == 317 || //GOLD
  301. is.getTypeId() == 298 || is.getTypeId() == 299 || is.getTypeId() == 300 || is.getTypeId() == 301; //LEATHER
  302. }
  303. public static boolean isLeatherArmor(ItemStack is){
  304. return is.getTypeId() == 298 || is.getTypeId() == 299 || is.getTypeId() == 300 || is.getTypeId() == 301;
  305. }
  306. public static boolean isGoldArmor(ItemStack is){
  307. return is.getTypeId() == 314 || is.getTypeId() == 315 || is.getTypeId() == 316 || is.getTypeId() == 317;
  308. }
  309. public static boolean isIronArmor(ItemStack is){
  310. return is.getTypeId() == 306 || is.getTypeId() == 307 || is.getTypeId() == 308 || is.getTypeId() == 309;
  311. }
  312. public static boolean isDiamondArmor(ItemStack is){
  313. return is.getTypeId() == 310 || is.getTypeId() == 311 || is.getTypeId() == 312 || is.getTypeId() == 313;
  314. }
  315. public static boolean isTools(ItemStack is)
  316. {
  317. return is.getTypeId() == 359 || is.getTypeId() == 256 || is.getTypeId() == 257 || is.getTypeId() == 258 || is.getTypeId() == 267 || is.getTypeId() == 292 || //IRON
  318. is.getTypeId() == 276 || is.getTypeId() == 277 || is.getTypeId() == 278 || is.getTypeId() == 279 || is.getTypeId() == 293 || //DIAMOND
  319. is.getTypeId() == 283 || is.getTypeId() == 285 || is.getTypeId() == 286 || is.getTypeId() == 284 || is.getTypeId() == 294 || //GOLD
  320. is.getTypeId() == 268 || is.getTypeId() == 269 || is.getTypeId() == 270 || is.getTypeId() == 271 || is.getTypeId() == 290 ||//WOOD
  321. is.getTypeId() == 272 || is.getTypeId() == 273 || is.getTypeId() == 274 || is.getTypeId() == 275|| is.getTypeId() == 291 || //STONE
  322. is.getTypeId() == 261; //BOW
  323. }
  324. public static boolean isStoneTools(ItemStack is){
  325. return is.getTypeId() == 272 || is.getTypeId() == 273 || is.getTypeId() == 274 || is.getTypeId() == 275 || is.getTypeId() == 291;
  326. }
  327. public static boolean isWoodTools(ItemStack is){
  328. return is.getTypeId() == 268 || is.getTypeId() == 269 || is.getTypeId() == 270 || is.getTypeId() == 271 || is.getTypeId() == 290;
  329. }
  330. public static boolean isGoldTools(ItemStack is){
  331. return is.getTypeId() == 283 || is.getTypeId() == 285 || is.getTypeId() == 286 || is.getTypeId() == 284 || is.getTypeId() == 294;
  332. }
  333. public static boolean isIronTools(ItemStack is){
  334. return is.getTypeId() == 359 || is.getTypeId() == 256 || is.getTypeId() == 257 || is.getTypeId() == 258 || is.getTypeId() == 267 || is.getTypeId() == 292;
  335. }
  336. public static boolean isDiamondTools(ItemStack is){
  337. return is.getTypeId() == 276 || is.getTypeId() == 277 || is.getTypeId() == 278 || is.getTypeId() == 279 || is.getTypeId() == 293;
  338. }
  339. public static boolean isBow(ItemStack is){
  340. return is.getTypeId() == 261;
  341. }
  342. /**
  343. * Computes repair bonuses.
  344. *
  345. * @param player The player repairing an item
  346. * @param durability The durability of the item being repaired
  347. * @param ramt The base amount of durability repaired to the item
  348. * @return The final amount of durability repaired to the item
  349. */
  350. public static short repairCalculate(Player player, short durability, int ramt){
  351. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  352. float bonus = (float)(skillLevel/500);
  353. bonus = (ramt * bonus);
  354. ramt+=bonus;
  355. if(checkPlayerProcRepair(player))
  356. ramt = (short) (ramt * 2);
  357. durability-=ramt;
  358. if(durability < 0)
  359. durability = 0;
  360. return durability;
  361. }
  362. /**
  363. * Gets the base durability amount to repair an item.
  364. *
  365. * @param is The item being repaired
  366. * @param player The player repairing the item
  367. * @return The final amount of durability repaired to the item
  368. */
  369. public static short getRepairAmount(ItemStack is, Player player){
  370. short durability = is.getDurability();
  371. short maxDurability = is.getType().getMaxDurability();
  372. int ramt = 0;
  373. if(m.isShovel(is))
  374. ramt = maxDurability;
  375. else if(m.isHoe(is) || m.isSwords(is) || is.getTypeId() == 359)
  376. ramt = maxDurability / 2;
  377. else if(m.isAxes(is) || m.isMiningPick(is) || isBow(is))
  378. ramt = maxDurability / 3;
  379. else if(m.isBoots(is))
  380. ramt = maxDurability / 4;
  381. else if(m.isHelmet(is))
  382. ramt = maxDurability / 5;
  383. else if(m.isPants(is))
  384. ramt = maxDurability / 7;
  385. else if(m.isChestplate(is))
  386. ramt = maxDurability / 8;
  387. return repairCalculate(player, durability, ramt);
  388. }
  389. /**
  390. * Informs a player that the repair has failed.
  391. *
  392. * @param is The item being repaired
  393. * @param player The player repairing the item
  394. */
  395. public static void needMoreVespeneGas(ItemStack is, Player player)
  396. {
  397. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  398. if(is.getAmount() > 1)
  399. player.sendMessage(mcLocale.getString("Skills.StackedItems"));
  400. else
  401. {
  402. if(isDiamondTools(is) || isDiamondArmor(is))
  403. {
  404. if(skillLevel < LoadProperties.repairdiamondlevel)
  405. player.sendMessage(mcLocale.getString("Skills.AdeptDiamond"));
  406. else
  407. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.BLUE+ nDiamond);
  408. }
  409. else if(isIronTools(is) || isIronArmor(is))
  410. {
  411. if(skillLevel < LoadProperties.repairIronLevel)
  412. player.sendMessage(mcLocale.getString("Skills.AdeptIron"));
  413. else
  414. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.GRAY+ nIron);
  415. }
  416. else if(isGoldTools(is) || isGoldArmor(is))
  417. {
  418. if(skillLevel < LoadProperties.repairGoldLevel)
  419. player.sendMessage(mcLocale.getString("Skills.AdeptGold"));
  420. else
  421. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.GOLD+nGold);
  422. }
  423. else if(isStoneTools(is))
  424. {
  425. if(skillLevel < LoadProperties.repairStoneLevel)
  426. player.sendMessage(mcLocale.getString("Skills.AdeptStone"));
  427. else
  428. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.GRAY+nStone);
  429. }
  430. else if(isWoodTools(is))
  431. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.DARK_GREEN+ nWood);
  432. else if (isLeatherArmor(is))
  433. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.YELLOW+ nLeather);
  434. else if (isBow(is))
  435. player.sendMessage(mcLocale.getString("Skills.NeedMore")+" "+ChatColor.YELLOW+ nString);
  436. }
  437. }
  438. /**
  439. * Checks for Super Repair bonus.
  440. *
  441. * @param player The player repairing an item.
  442. * @return true if bonus granted, false otherwise
  443. */
  444. public static boolean checkPlayerProcRepair(Player player)
  445. {
  446. int skillLevel = Users.getProfile(player).getSkillLevel(SkillType.REPAIR);
  447. if(skillLevel > 1000 || (Math.random() * 1000 <= skillLevel))
  448. {
  449. player.sendMessage(mcLocale.getString("Skills.FeltEasy"));
  450. return true;
  451. }
  452. return false;
  453. }
  454. /**
  455. * Repairs an item.
  456. *
  457. * @param player The player repairing an item
  458. * @param enchants The enchantments on the item
  459. * @param enchantsLevel The level of the enchantments on the item
  460. */
  461. public static void repairItem(Player player, Enchantment[] enchants, int[] enchantsLevel)
  462. {
  463. PlayerProfile PP = Users.getProfile(player);
  464. ItemStack is = player.getItemInHand();
  465. //Handle the enchantments
  466. addEnchants(is, enchants, enchantsLevel, PP, player);
  467. is.setDurability(getRepairAmount(is, player));
  468. }
  469. }