Advertisement
Guest User

Untitled

a guest
Jun 11th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.52 KB | None | 0 0
  1. public class Mining extends SkillTask {
  2.  
  3. /**
  4. * The rock being mined
  5. */
  6. private final MinableRock rock;
  7.  
  8. /**
  9. * The time before we successfully mine the ore
  10. */
  11. private int incr;
  12.  
  13. /**
  14. * The total delay before the rock is mined
  15. */
  16. private int totalDelay;
  17.  
  18. /**
  19. * The delay for the animation
  20. */
  21. private int animationDelay;
  22.  
  23. /**
  24. * The animation we are performing for our pickaxe
  25. */
  26. private final int animation;
  27.  
  28. /**
  29. * Constructs a new {@link Mining} skill task
  30. *
  31. * @param player
  32. * The {@link Player} mining
  33. * @param delay
  34. * The delay before the rock is mined
  35. * @param rock
  36. * The {@link MinableRock} being mined
  37. * @param animation
  38. * The animation for the pickaxe the player is wielding
  39. */
  40. public Mining(Player player, int delay, MinableRock rock, int animation) {
  41. super(player, 1, ActionPolicy.NO_WALK, ActionPolicy.NO_STACK);
  42. this.rock = rock;
  43. this.totalDelay = delay;
  44. this.animation = animation;
  45. }
  46.  
  47. /**
  48. * Fetches the mining data
  49. *
  50. * @param id
  51. * The id of the rock to check
  52. * @return The data for the rock
  53. */
  54. private static int[] fetchData(int id) {
  55. for (int[] i : data) {
  56. if (i[0] == id) {
  57. return i;
  58. }
  59. }
  60. return null;
  61. }
  62.  
  63. /**
  64. * Attempts to mine a rock
  65. *
  66. * @param player
  67. * The {@link Player} attempting to mine a rock
  68. * @param object
  69. * The id of the object clicked
  70. * @param position
  71. * The position of the rock
  72. * @return If the rock can be mined
  73. */
  74. public static boolean attemptMining(final Player player, final int object, final Position position) {
  75.  
  76. int[] data = fetchData(object);
  77.  
  78. if (data == null) {
  79. // data is null so we aren't mining
  80. return false;
  81. }
  82.  
  83. if (System.currentTimeMillis() - player.getAttribute("last_mining_action", 0L) < 2500) {
  84. return false;
  85. }
  86.  
  87. /*
  88. * Does the player have inventory space if not, reset Mining status.
  89. */
  90. if (!SkillHandler.noInventorySpace(player, "mining")) {
  91. return false;
  92. }
  93.  
  94.  
  95.  
  96. /*
  97. * Fetch our data for the rock
  98. */
  99. final Item item = new Item(data[1]);
  100. final double experience = data[3];
  101. final int animation = getAnimation(player);
  102. final int requirement = data[2];
  103.  
  104. /*
  105. * Does the player have the required level to mine this rock?
  106. */
  107. if (!SkillHandler.hasRequiredLevel(player, 14, requirement, "mining", "mine " + "here")) {
  108. return false;
  109. }
  110.  
  111. /*
  112. * Does the player have the correct pickaxe.
  113. */
  114. if (!hasPickaxe(player))
  115. return false;
  116.  
  117. /*
  118. * Is the rock depleted?
  119. */
  120. if (MineralContainer.isMineralDepleted(10797, position)) {
  121. player.sendMessage("This mineral is depleted, wait until it refills.");
  122. return false;
  123. }
  124.  
  125. player.turnPlayerTo(player.objectX, player.objectY);
  126. player.getMovementHandler().stopMovement();
  127.  
  128. /*
  129. * Start our animation and let the player know we're mining
  130. */
  131. player.setAttribute("last_mining_action", System.currentTimeMillis());
  132. player.sendMessage("You swing your pick at the rock...");
  133. /*
  134. * Set our skill active so we can verify this where needed
  135. */
  136. SkillHandler.setSkillActive(player, Player.playerMining, true);
  137.  
  138. /*
  139. * The time it will take before we successfully mine
  140. */
  141. final int delay = getTimer(player, object);
  142.  
  143. /*
  144. * Start our mining skill task
  145. */
  146. player.setSkillTask(new Mining(player, delay, new MinableRock(object, position, experience, item), animation));
  147. return true;
  148. }
  149.  
  150. /**
  151. * Gets the time it will take before the player successfully mines the ore
  152. *
  153. * @param c
  154. * The {@link Player} mining the ore
  155. * @param objectId
  156. * The id of the rock being mined
  157. * @return The time it will take before the player successfully mines the
  158. * ore
  159. */
  160. private static int getTimer(Player c, int objectId) {
  161. /*
  162. * Fetch the delay the rock would normally take
  163. */
  164. int time = (getMineTime(c, objectId) + getTime(c) + playerMiningLevel(c));
  165.  
  166. /*
  167. * If for some reason its less then 3, set it to 3
  168. */
  169. if (time < 3) {
  170. time = 3;
  171. }
  172.  
  173. /*
  174. * Gets a random amount between 3 and time
  175. */
  176. int random = Misc.random(3, time);
  177. return random;
  178. }
  179.  
  180. /**
  181. * Gets the time it takes to mine the ore
  182. *
  183. * @param c
  184. * The {@link Player} mining the ore
  185. * @param object
  186. * The id of the object
  187. * @return The time it takes to mine the ore
  188. */
  189. private static int getMineTime(Player c, int object) {
  190. for (int i = 0; i < data.length; i++) {
  191. if (object == data[i][0]) {
  192. //System.out.println(data[i][4]);
  193. return data[i][4];
  194. }
  195. }
  196. return -1;
  197. }
  198.  
  199. /**
  200. * Gets the players mining level
  201. *
  202. * @param c
  203. * The {@link Player} to get the mining level for
  204. * @return The players mining level
  205. */
  206. private static int playerMiningLevel(Player c) {
  207. return (10 - (int) Math.floor(c.playerLevel[14] / 10));
  208. }
  209.  
  210. /**
  211. * Gets the bonus based on the players pickaxe
  212. *
  213. * @param c
  214. * The [@link Player} to get the bonus for
  215. * @return The bonus for the current pickaxe
  216. */
  217. private static int getTime(Player c) {
  218. for (int i = 0; i < pickaxe.length; i++) {
  219. if (c.playerEquipment[3] == pickaxe[i][0]) {
  220. // we first use our pickaxe that we're wielding
  221. return pickaxe[i][2];
  222. }
  223. if (c.getItems().playerHasItem(pickaxe[i][0])) {
  224. if (c.playerLevel[Player.playerMining] >= pickaxe[i][1]) {
  225. return pickaxe[i][2];
  226. }
  227. }
  228. }
  229. return 10;
  230. }
  231.  
  232. /**
  233. * Gets the time it takes to respawn the rock
  234. *
  235. * @param object
  236. * The id of the object
  237. * @return The time it takes to respawn the rock
  238. */
  239. private static int getRespawnTime(int object) {
  240. for (int i = 0; i < data.length; i++) {
  241. if (object == data[i][0]) {
  242. return data[i][5];
  243. }
  244. }
  245. return -1;
  246. }
  247.  
  248. /**
  249. * Checks if the player has a pickaxe in their inventory or equipped
  250. *
  251. * @param c
  252. * The {@link Player} to check a pickaxe for
  253. * @return If the player has a pickaxe
  254. */
  255. private static boolean hasPickaxe(Player c) {
  256. for (int i = pickaxe.length - 1; i >= 0; i--) {
  257. if (c.playerEquipment[3] == pickaxe[i][0] || c.getItems().playerHasItem(pickaxe[i][0])) {
  258. return SkillHandler.hasRequiredLevel(c, c.playerMining, pickaxe[i][1], "mining", "use this pickaxe");
  259. }
  260. }
  261. c.sendMessage("You don't have a pickaxe to mine these rocks.");
  262. return false;
  263. }
  264.  
  265. /**
  266. * Gets the animation for the provided pickaxe
  267. *
  268. * @param c
  269. * The {@link Player} performing the animation
  270. * @return The animation for the provided pickaxe
  271. */
  272. private static int getAnimation(Player c) {
  273. for (int i = 0; i < animationData.length; i++) {
  274. if (c.playerEquipment[3] == animationData[i][0]) {
  275. return animationData[i][1];
  276. }
  277. if (c.getItems().playerHasItem(animationData[i][0])) {
  278. return animationData[i][1];
  279. }
  280. }
  281. return -1;
  282. }
  283.  
  284. private static int[][] animationData = { { 1275, 624 }, { 1271, 628 }, { 1273, 629 }, { 1269, 627 }, { 1267, 626 }, { 1265, 625 },
  285. { 12797, 335 }, { 11920, 335 }, };
  286. private static int[][] pickaxe = { { 11920, 61, 0 }, // DRAGON
  287. { 12797, 61, 0 }, // DRAGON (OR)
  288. { 1275, 41, 2 }, // RUNE
  289. { 1271, 31, 3 }, // ADDY
  290. { 1273, 21, 4 }, // MITH
  291. { 1269, 6, 5 }, // STEEL
  292. { 1267, 1, 6 }, // IRON
  293. { 1265, 1, 7 }, // BRONZE
  294. };
  295. private static int[][] data = {
  296. { 7484, 436, 1, 18, 1, 5 }, // COPPER
  297. { 7453, 436, 1, 18, 1, 5 }, // COPPER
  298. { 7485, 438, 1, 18, 1, 5 }, // TIN
  299. { 7486, 438, 1, 18, 1, 5 }, // TIN
  300. { 7455, 440, 15, 35, 2, 5 }, // IRON
  301. { 7488, 440, 15, 35, 2, 5 }, // IRON
  302. { 7456, 453, 30, 50, 3, 8 }, // COAL
  303. { 7489, 453, 30, 50, 3, 8 }, // COAL
  304. { 7491, 444, 40, 65, 3, 10 }, // GOLD
  305. { 7458, 444, 40, 65, 3, 10 }, // GOLD
  306. { 7492, 447, 55, 80, 5, 20 }, // MITH
  307. { 7459, 447, 55, 80, 5, 20 }, // MITH
  308. { 7460, 449, 70, 95, 7, 50 }, // ADDY
  309. { 7493, 449, 70, 95, 7, 50 }, // ADDY
  310. //{ 7492, 449, 70, 95, 7, 50 }, // ADDY
  311.  
  312. { 7485, 442, 20, 40, 5, 5 }, // SILVER
  313. { 7486, 442, 20, 40, 5, 5 }, // SILVER
  314.  
  315.  
  316. { 14912, 1436, 20, 40, 0, 5 },
  317.  
  318. { 7419, 451, 85, 125, 40, 100 }, // RUNE
  319. { 7461, 451, 85, 125, 40, 100 }, // RUNE
  320. { 7494, 451, 85, 125, 40, 100 }, // RUNE
  321. { 7418, 451, 85, 125, 40, 100 },// RUNE
  322. };
  323.  
  324. @Override
  325. public void execute() {
  326.  
  327. /*
  328. * If the player is no longer active, terminate the task.
  329. */
  330. if (!getPlayer().isActive()) {
  331. stop();
  332. return;
  333. }
  334.  
  335. /*
  336. * If the player no longer has a pickaxe, terminate the task.
  337. */
  338. if (!hasPickaxe(getPlayer())) {
  339. this.stop();
  340. return;
  341. }
  342. /*
  343. * If the player has no inventory, terminate the task.
  344. */
  345. if (!SkillHandler.noInventorySpace(getPlayer(), "mining")) {
  346. this.stop();
  347. return;
  348. }
  349. /*
  350. * If the rock is depleted, stop.
  351. */
  352. if (MineralContainer.isMineralDepleted(10797, rock.getPosition())) {
  353. getPlayer().sendMessage("This mineral is depleted, wait until it refills.");
  354. getPlayer().write(new SendSoundPacket(431, 0, 0));
  355. this.stop();
  356. return;
  357. }
  358.  
  359. if ((animationDelay <= 1) || (animationDelay == 4) || (animationDelay == 6) || (animationDelay == 11)) {
  360. getPlayer().write(new SendSoundPacket(432, 10, animationDelay == 11 ? 10 : 0));
  361. }
  362.  
  363. /*
  364. * Perform our animation
  365. */
  366. if (animationDelay++ % 15 == 0) {
  367. getPlayer().startAnimation(animation);
  368. }
  369.  
  370. /*
  371. * The time has been reached, add the ore and experience
  372. */
  373. if (incr++ == totalDelay) {
  374.  
  375. /*
  376. * Collect the item id ore
  377. */
  378. getPlayer().getItems().addItem(rock.getItem());
  379.  
  380. /*
  381. * Does the player have a dragon pickaxe? If so, add an extra ore
  382. */
  383. int chance = Misc.random(5);
  384. if (getPlayer().playerEquipment[Player.playerWeapon] == 15813 && chance == 3 && getPlayer().getItems().freeSlots() > 2) {
  385. getPlayer().getItems().addItem(rock.getItem());
  386. getPlayer().sendMessage("You feel your pickaxe hit slightly harder..");
  387. }
  388.  
  389. /*
  390. * Send the message once we've mined the ore
  391. */
  392. getPlayer().sendMessage("You manage to mine some " + rock.getItem().getDefinition().getName() + ".");
  393.  
  394. /*
  395. * Add our experience
  396. */
  397. getPlayer().getPA().addSkillXP(rock.getExperience() * SkillHandler.MINING_XP, Player.playerMining);
  398.  
  399. /*
  400. * If not rune essence, stop mining
  401. */
  402. if (rock.getId() != 14912) {
  403. GemData.get(getPlayer());
  404. if (Player.inResourceArea(getPlayer()) && rock.getId() == 14175) {
  405.  
  406. } else {
  407. MineralContainer.addMineral(new DepletedMineral(10797, rock.getId(), rock.getPosition()));
  408. addExpirableRock(getRespawnTime(rock.getId()));
  409. }
  410. ObjectHandler.createAnObject(getPlayer(), 10797, rock.getPosition().getX(), rock.getPosition().getY());
  411. this.stop();
  412. } else {
  413. /*
  414. * Reset our timer
  415. */
  416. incr = 0;
  417. totalDelay = getTimer(getPlayer(), rock.getId());
  418. }
  419. }
  420. }
  421.  
  422. /**
  423. * Adds a task to expire the rock after the provided delay
  424. *
  425. * @param delay
  426. * The delay before expiring the empty ore
  427. */
  428. private void addExpirableRock(int delay) {
  429. Server.getTaskScheduler().schedule(new Task(delay) {
  430. @Override
  431. public void execute() {
  432. ObjectHandler.createAnObject(getPlayer(), rock.getId(), rock.getPosition().getX(), rock.getPosition().getY());
  433. for (Iterator<DepletedMineral> i = MineralContainer.getDepletedMinerals().iterator(); i.hasNext();) {
  434. DepletedMineral mineral = i.next();
  435. if (mineral.getPosition().equals(rock.getPosition()) && mineral.getRockId() == rock.getId()) {
  436. i.remove();
  437. }
  438. }
  439. this.stop();
  440. }
  441. });
  442. }
  443.  
  444. /**
  445. * Once the event stops, reset the boolean.
  446. */
  447. @Override
  448. public void onStop() {
  449. getPlayer().startAnimation(65535);
  450. SkillHandler.setSkillActive(getPlayer(), Player.playerMining, false);
  451.  
  452. }
  453.  
  454. @Override
  455. public String toString() {
  456. return "Mining task [" + getPlayer() + ", rock=" + rock.getId() + ", position=" + rock.getPosition() + "]";
  457. }
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement