mcRepair.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. package com.gmail.nossr50;
  2. import org.bukkit.ChatColor;
  3. import org.bukkit.block.Block;
  4. import org.bukkit.entity.Player;
  5. import org.bukkit.inventory.ItemStack;
  6. public class mcRepair {
  7. private static mcMMO plugin;
  8. public mcRepair(mcMMO instance) {
  9. plugin = instance;
  10. }
  11. private static volatile mcRepair instance;
  12. public static mcRepair getInstance() {
  13. if (instance == null) {
  14. instance = new mcRepair(plugin);
  15. }
  16. return instance;
  17. }
  18. public void repairCheck(Player player, ItemStack is, Block block){
  19. player.sendMessage("mcMMO Debug: The block is an Anvil.");
  20. if(block != null
  21. && mcPermissions.getInstance().repair(player)){
  22. player.sendMessage("mcMMO Debug: The block is not null and the player has access to repair.");
  23. if(player.getItemInHand().getDurability() > 0){
  24. player.sendMessage("mcMMO Debug: The item is not at full durability.");
  25. /*
  26. * ARMOR
  27. */
  28. if(isArmor(is)){
  29. player.sendMessage("mcMMO Debug: The item is armor.");
  30. /*
  31. * DIAMOND ARMOR
  32. */
  33. if(isDiamondArmor(is) && hasDiamond(player) && mcUsers.getProfile(player).getRepairInt() >= 50){
  34. player.sendMessage("mcMMO Debug: CODE 1");
  35. removeDiamond(player);
  36. player.getItemInHand().setDurability(getArmorRepairAmount(is, player));
  37. mcUsers.getProfile(player).addRepairGather(75);
  38. } else if (isIronArmor(is) && hasIron(player)){
  39. player.sendMessage("mcMMO Debug: CODE 2");
  40. /*
  41. * IRON ARMOR
  42. */
  43. removeIron(player);
  44. player.getItemInHand().setDurability(getArmorRepairAmount(is, player));
  45. mcUsers.getProfile(player).addRepairGather(20);
  46. //GOLD ARMOR
  47. } else if (isGoldArmor(is) && hasGold(player)){
  48. player.sendMessage("mcMMO Debug: CODE 3");
  49. removeGold(player);
  50. player.getItemInHand().setDurability(getArmorRepairAmount(is, player));
  51. mcUsers.getProfile(player).addRepairGather(50);
  52. } else {
  53. needMoreVespeneGas(is, player);
  54. }
  55. }
  56. /*
  57. * TOOLS
  58. */
  59. if(isTools(is)){
  60. /*
  61. * IRON TOOLS
  62. */
  63. if(isIronTools(is) && hasIron(player)){
  64. player.sendMessage("mcMMO Debug: CODE 4");
  65. is.setDurability(getToolRepairAmount(is, player));
  66. removeIron(player);
  67. mcUsers.getProfile(player).addRepairGather(20);
  68. } else if (isDiamondTools(is) && hasDiamond(player) && mcUsers.getProfile(player).getRepairInt() >= 50){ //Check if its diamond and the player has diamonds
  69. /*
  70. * DIAMOND TOOLS
  71. */
  72. player.sendMessage("mcMMO Debug: CODE 5");
  73. is.setDurability(getToolRepairAmount(is, player));
  74. removeDiamond(player);
  75. mcUsers.getProfile(player).addRepairGather(75);
  76. } else if(isGoldTools(is) && hasGold(player)){
  77. player.sendMessage("mcMMO Debug: CODE 6");
  78. is.setDurability(getToolRepairAmount(is, player));
  79. removeGold(player);
  80. mcUsers.getProfile(player).addRepairGather(50);
  81. } else {
  82. player.sendMessage("mcMMO Debug: CODE 7");
  83. needMoreVespeneGas(is, player);
  84. }
  85. }
  86. } else {
  87. player.sendMessage("That is at full durability.");
  88. }
  89. player.updateInventory();
  90. /*
  91. * GIVE SKILL IF THERE IS ENOUGH XP
  92. */
  93. if(mcUsers.getProfile(player).getRepairGatherInt() >= mcUsers.getProfile(player).getXpToLevel("repair")){
  94. int skillups = 0;
  95. while(mcUsers.getProfile(player).getRepairGatherInt() >= mcUsers.getProfile(player).getXpToLevel("repair")){
  96. skillups++;
  97. mcUsers.getProfile(player).removeRepairGather(mcUsers.getProfile(player).getXpToLevel("repair"));
  98. mcUsers.getProfile(player).skillUpRepair(1);
  99. }
  100. player.sendMessage(ChatColor.YELLOW+"Repair skill increased by "+skillups+"."+" Total ("+mcUsers.getProfile(player).getRepair()+")");
  101. }
  102. }
  103. }
  104. public boolean isArmor(ItemStack is){
  105. if(is.getTypeId() == 306 || is.getTypeId() == 307 ||is.getTypeId() == 308 ||is.getTypeId() == 309 ||
  106. is.getTypeId() == 310 ||is.getTypeId() == 311 ||is.getTypeId() == 312 ||is.getTypeId() == 313 ||
  107. is.getTypeId() == 314 || is.getTypeId() == 315 || is.getTypeId() == 316 || is.getTypeId() == 317){
  108. return true;
  109. } else {
  110. return false;
  111. }
  112. }
  113. public boolean isGoldArmor(ItemStack is){
  114. if(is.getTypeId() == 314 || is.getTypeId() == 315 || is.getTypeId() == 316 || is.getTypeId() == 317){
  115. return true;
  116. } else {
  117. return false;
  118. }
  119. }
  120. public boolean isIronArmor(ItemStack is){
  121. if(is.getTypeId() == 306 || is.getTypeId() == 307 || is.getTypeId() == 308 || is.getTypeId() == 309)
  122. {
  123. return true;
  124. } else {
  125. return false;
  126. }
  127. }
  128. public boolean isDiamondArmor(ItemStack is){
  129. if(is.getTypeId() == 310 || is.getTypeId() == 311 || is.getTypeId() == 312 || is.getTypeId() == 313)
  130. {
  131. return true;
  132. } else {
  133. return false;
  134. }
  135. }
  136. public boolean isTools(ItemStack is){
  137. if(is.getTypeId() == 256 || is.getTypeId() == 257 || is.getTypeId() == 258 || is.getTypeId() == 267 || is.getTypeId() == 292 || //IRON
  138. is.getTypeId() == 276 || is.getTypeId() == 277 || is.getTypeId() == 278 || is.getTypeId() == 279 || is.getTypeId() == 293 || //DIAMOND
  139. is.getTypeId() == 283 || is.getTypeId() == 285 || is.getTypeId() == 286 || is.getTypeId() == 284) //GOLD
  140. {
  141. return true;
  142. } else {
  143. return false;
  144. }
  145. }
  146. public boolean isGoldTools(ItemStack is){
  147. if(is.getTypeId() == 283 || is.getTypeId() == 285 || is.getTypeId() == 286 || is.getTypeId() == 284 || is.getTypeId() == 294){
  148. return true;
  149. } else {
  150. return false;
  151. }
  152. }
  153. public boolean isIronTools(ItemStack is){
  154. if(is.getTypeId() == 256 || is.getTypeId() == 257 || is.getTypeId() == 258 || is.getTypeId() == 267 || is.getTypeId() == 292)
  155. {
  156. return true;
  157. } else {
  158. return false;
  159. }
  160. }
  161. public boolean isDiamondTools(ItemStack is){
  162. if(is.getTypeId() == 276 || is.getTypeId() == 277 || is.getTypeId() == 278 || is.getTypeId() == 279 || is.getTypeId() == 293)
  163. {
  164. return true;
  165. } else {
  166. return false;
  167. }
  168. }
  169. public void removeIron(Player player){
  170. ItemStack[] inventory = player.getInventory().getContents();
  171. for(ItemStack x : inventory){
  172. if(x.getTypeId() == 265){
  173. if(x.getAmount() == 1){
  174. x.setTypeId(0);
  175. x.setAmount(0);
  176. player.getInventory().setContents(inventory);
  177. } else{
  178. x.setAmount(x.getAmount() - 1);
  179. player.getInventory().setContents(inventory);
  180. }
  181. return;
  182. }
  183. }
  184. }
  185. public void removeGold(Player player){
  186. ItemStack[] inventory = player.getInventory().getContents();
  187. for(ItemStack x : inventory){
  188. if(x.getTypeId() == 266){
  189. if(x.getAmount() == 1){
  190. x.setTypeId(0);
  191. x.setAmount(0);
  192. player.getInventory().setContents(inventory);
  193. } else{
  194. x.setAmount(x.getAmount() - 1);
  195. player.getInventory().setContents(inventory);
  196. }
  197. return;
  198. }
  199. }
  200. }
  201. public void removeDiamond(Player player){
  202. ItemStack[] inventory = player.getInventory().getContents();
  203. for(ItemStack x : inventory){
  204. if(x.getTypeId() == 264){
  205. if(x.getAmount() == 1){
  206. x.setTypeId(0);
  207. x.setAmount(0);
  208. player.getInventory().setContents(inventory);
  209. } else{
  210. x.setAmount(x.getAmount() - 1);
  211. player.getInventory().setContents(inventory);
  212. }
  213. return;
  214. }
  215. }
  216. }
  217. public boolean hasGold(Player player){
  218. ItemStack[] inventory = player.getInventory().getContents();
  219. for(ItemStack x : inventory){
  220. if(x.getTypeId() == 266){
  221. return true;
  222. }
  223. }
  224. return false;
  225. }
  226. public boolean hasDiamond(Player player){
  227. ItemStack[] inventory = player.getInventory().getContents();
  228. for(ItemStack x : inventory){
  229. if(x.getTypeId() == 264){
  230. return true;
  231. }
  232. }
  233. return false;
  234. }
  235. public boolean hasIron(Player player){
  236. ItemStack[] inventory = player.getInventory().getContents();
  237. for(ItemStack x : inventory){
  238. if(x.getTypeId() == 265){
  239. return true;
  240. }
  241. }
  242. return false;
  243. }
  244. public short getToolRepairAmount(ItemStack is, Player player){
  245. short durability = is.getDurability();
  246. switch(is.getTypeId())
  247. {
  248. case 284:
  249. durability = 0;
  250. break;
  251. case 256:
  252. durability = 0;
  253. break;
  254. case 277:
  255. durability = 0;
  256. break;
  257. case 257:
  258. durability -= 84;
  259. break;
  260. case 258:
  261. durability -= 84;
  262. break;
  263. case 267:
  264. durability -= 84;
  265. break;
  266. case 292:
  267. durability -= 84;
  268. break;
  269. case 276:
  270. durability -= 509;
  271. break;
  272. case 278:
  273. durability -= 509;
  274. break;
  275. case 279:
  276. durability -= 509;
  277. break;
  278. case 293:
  279. durability -= 509;
  280. break;
  281. case 283:
  282. durability -= 13;
  283. break;
  284. case 285:
  285. durability -= 13;
  286. break;
  287. case 286:
  288. durability -= 13;
  289. break;
  290. case 294:
  291. durability -= 13;
  292. break;
  293. }
  294. if(durability < 0)
  295. durability = 0;
  296. if(checkPlayerProcRepair(player))
  297. durability = 0;
  298. return durability;
  299. }
  300. //This determines how much we repair
  301. public short getArmorRepairAmount(ItemStack is, Player player){
  302. short durability = is.getDurability();
  303. switch(is.getTypeId())
  304. {
  305. case 306:
  306. durability -= 27;
  307. break;
  308. case 310:
  309. durability -= 55;
  310. break;
  311. case 307:
  312. durability -= 24;
  313. break;
  314. case 311:
  315. durability -= 48;
  316. break;
  317. case 308:
  318. durability -= 27;
  319. break;
  320. case 312:
  321. durability -= 53;
  322. break;
  323. case 309:
  324. durability -= 40;
  325. break;
  326. case 313:
  327. durability -= 80;
  328. break;
  329. }
  330. if(durability < 0)
  331. durability = 0;
  332. if(checkPlayerProcRepair(player))
  333. durability = 0;
  334. return durability;
  335. }
  336. public void needMoreVespeneGas(ItemStack is, Player player){
  337. if ((isDiamondTools(is) || isDiamondArmor(is)) && mcUsers.getProfile(player).getRepairInt() < 50){
  338. player.sendMessage(ChatColor.DARK_RED +"You're not adept enough to repair Diamond");
  339. } else if (isDiamondTools(is) && !hasDiamond(player) || isIronTools(is) && !hasIron(player) || isGoldTools(is) && !hasGold(player)){
  340. if(isDiamondTools(is) && !hasDiamond(player))
  341. player.sendMessage(ChatColor.DARK_RED+"You need more "+ChatColor.BLUE+ "Diamonds");
  342. if(isIronTools(is) && !hasIron(player))
  343. player.sendMessage(ChatColor.DARK_RED+"You need more "+ChatColor.GRAY+ "Iron");
  344. //herp
  345. if(isGoldTools(is) && !hasGold(player))
  346. player.sendMessage(ChatColor.DARK_RED+"You need more "+ChatColor.GOLD+"Gold");
  347. } else if (isDiamondArmor(is) && !hasDiamond(player)){
  348. player.sendMessage(ChatColor.DARK_RED+"You need more "+ChatColor.BLUE+ "Diamonds");
  349. } else if (isIronArmor(is) && !hasIron(player)){
  350. player.sendMessage(ChatColor.DARK_RED+"You need more "+ChatColor.GRAY+ "Iron");
  351. } else if (isGoldArmor(is) && !hasGold(player))
  352. player.sendMessage(ChatColor.DARK_RED+"You need more "+ChatColor.GOLD+"Gold");
  353. }
  354. public boolean checkPlayerProcRepair(Player player){
  355. if(mcUsers.getProfile(player).getRepairInt() >= 750){
  356. if(Math.random() * 10 > 2){
  357. player.sendMessage(ChatColor.GRAY + "That took no effort.");
  358. return true;
  359. }
  360. } else if (mcUsers.getProfile(player).getRepairInt() >= 450 && mcUsers.getProfile(player).getRepairInt() < 750){
  361. if(Math.random() * 10 > 4){
  362. player.sendMessage(ChatColor.GRAY + "That felt really easy.");
  363. return true;
  364. }
  365. } else if (mcUsers.getProfile(player).getRepairInt() >= 150 && mcUsers.getProfile(player).getRepairInt() < 450){
  366. if(Math.random() * 10 > 6){
  367. player.sendMessage(ChatColor.GRAY + "That felt pretty easy.");
  368. return true;
  369. }
  370. } else if (mcUsers.getProfile(player).getRepairInt() >= 50 && mcUsers.getProfile(player).getRepairInt() < 150){
  371. if(Math.random() * 10 > 8){
  372. player.sendMessage(ChatColor.GRAY + "That felt easy.");
  373. return true;
  374. }
  375. }
  376. return false;
  377. }
  378. }