Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Mining extends SkillTask {
- /**
- * The rock being mined
- */
- private final MinableRock rock;
- /**
- * The time before we successfully mine the ore
- */
- private int incr;
- /**
- * The total delay before the rock is mined
- */
- private int totalDelay;
- /**
- * The delay for the animation
- */
- private int animationDelay;
- /**
- * The animation we are performing for our pickaxe
- */
- private final int animation;
- /**
- * Constructs a new {@link Mining} skill task
- *
- * @param player
- * The {@link Player} mining
- * @param delay
- * The delay before the rock is mined
- * @param rock
- * The {@link MinableRock} being mined
- * @param animation
- * The animation for the pickaxe the player is wielding
- */
- public Mining(Player player, int delay, MinableRock rock, int animation) {
- super(player, 1, ActionPolicy.NO_WALK, ActionPolicy.NO_STACK);
- this.rock = rock;
- this.totalDelay = delay;
- this.animation = animation;
- }
- /**
- * Fetches the mining data
- *
- * @param id
- * The id of the rock to check
- * @return The data for the rock
- */
- private static int[] fetchData(int id) {
- for (int[] i : data) {
- if (i[0] == id) {
- return i;
- }
- }
- return null;
- }
- /**
- * Attempts to mine a rock
- *
- * @param player
- * The {@link Player} attempting to mine a rock
- * @param object
- * The id of the object clicked
- * @param position
- * The position of the rock
- * @return If the rock can be mined
- */
- public static boolean attemptMining(final Player player, final int object, final Position position) {
- int[] data = fetchData(object);
- if (data == null) {
- // data is null so we aren't mining
- return false;
- }
- if (System.currentTimeMillis() - player.getAttribute("last_mining_action", 0L) < 2500) {
- return false;
- }
- /*
- * Does the player have inventory space if not, reset Mining status.
- */
- if (!SkillHandler.noInventorySpace(player, "mining")) {
- return false;
- }
- /*
- * Fetch our data for the rock
- */
- final Item item = new Item(data[1]);
- final double experience = data[3];
- final int animation = getAnimation(player);
- final int requirement = data[2];
- /*
- * Does the player have the required level to mine this rock?
- */
- if (!SkillHandler.hasRequiredLevel(player, 14, requirement, "mining", "mine " + "here")) {
- return false;
- }
- /*
- * Does the player have the correct pickaxe.
- */
- if (!hasPickaxe(player))
- return false;
- /*
- * Is the rock depleted?
- */
- if (MineralContainer.isMineralDepleted(10797, position)) {
- player.sendMessage("This mineral is depleted, wait until it refills.");
- return false;
- }
- player.turnPlayerTo(player.objectX, player.objectY);
- player.getMovementHandler().stopMovement();
- /*
- * Start our animation and let the player know we're mining
- */
- player.setAttribute("last_mining_action", System.currentTimeMillis());
- player.sendMessage("You swing your pick at the rock...");
- /*
- * Set our skill active so we can verify this where needed
- */
- SkillHandler.setSkillActive(player, Player.playerMining, true);
- /*
- * The time it will take before we successfully mine
- */
- final int delay = getTimer(player, object);
- /*
- * Start our mining skill task
- */
- player.setSkillTask(new Mining(player, delay, new MinableRock(object, position, experience, item), animation));
- return true;
- }
- /**
- * Gets the time it will take before the player successfully mines the ore
- *
- * @param c
- * The {@link Player} mining the ore
- * @param objectId
- * The id of the rock being mined
- * @return The time it will take before the player successfully mines the
- * ore
- */
- private static int getTimer(Player c, int objectId) {
- /*
- * Fetch the delay the rock would normally take
- */
- int time = (getMineTime(c, objectId) + getTime(c) + playerMiningLevel(c));
- /*
- * If for some reason its less then 3, set it to 3
- */
- if (time < 3) {
- time = 3;
- }
- /*
- * Gets a random amount between 3 and time
- */
- int random = Misc.random(3, time);
- return random;
- }
- /**
- * Gets the time it takes to mine the ore
- *
- * @param c
- * The {@link Player} mining the ore
- * @param object
- * The id of the object
- * @return The time it takes to mine the ore
- */
- private static int getMineTime(Player c, int object) {
- for (int i = 0; i < data.length; i++) {
- if (object == data[i][0]) {
- //System.out.println(data[i][4]);
- return data[i][4];
- }
- }
- return -1;
- }
- /**
- * Gets the players mining level
- *
- * @param c
- * The {@link Player} to get the mining level for
- * @return The players mining level
- */
- private static int playerMiningLevel(Player c) {
- return (10 - (int) Math.floor(c.playerLevel[14] / 10));
- }
- /**
- * Gets the bonus based on the players pickaxe
- *
- * @param c
- * The [@link Player} to get the bonus for
- * @return The bonus for the current pickaxe
- */
- private static int getTime(Player c) {
- for (int i = 0; i < pickaxe.length; i++) {
- if (c.playerEquipment[3] == pickaxe[i][0]) {
- // we first use our pickaxe that we're wielding
- return pickaxe[i][2];
- }
- if (c.getItems().playerHasItem(pickaxe[i][0])) {
- if (c.playerLevel[Player.playerMining] >= pickaxe[i][1]) {
- return pickaxe[i][2];
- }
- }
- }
- return 10;
- }
- /**
- * Gets the time it takes to respawn the rock
- *
- * @param object
- * The id of the object
- * @return The time it takes to respawn the rock
- */
- private static int getRespawnTime(int object) {
- for (int i = 0; i < data.length; i++) {
- if (object == data[i][0]) {
- return data[i][5];
- }
- }
- return -1;
- }
- /**
- * Checks if the player has a pickaxe in their inventory or equipped
- *
- * @param c
- * The {@link Player} to check a pickaxe for
- * @return If the player has a pickaxe
- */
- private static boolean hasPickaxe(Player c) {
- for (int i = pickaxe.length - 1; i >= 0; i--) {
- if (c.playerEquipment[3] == pickaxe[i][0] || c.getItems().playerHasItem(pickaxe[i][0])) {
- return SkillHandler.hasRequiredLevel(c, c.playerMining, pickaxe[i][1], "mining", "use this pickaxe");
- }
- }
- c.sendMessage("You don't have a pickaxe to mine these rocks.");
- return false;
- }
- /**
- * Gets the animation for the provided pickaxe
- *
- * @param c
- * The {@link Player} performing the animation
- * @return The animation for the provided pickaxe
- */
- private static int getAnimation(Player c) {
- for (int i = 0; i < animationData.length; i++) {
- if (c.playerEquipment[3] == animationData[i][0]) {
- return animationData[i][1];
- }
- if (c.getItems().playerHasItem(animationData[i][0])) {
- return animationData[i][1];
- }
- }
- return -1;
- }
- private static int[][] animationData = { { 1275, 624 }, { 1271, 628 }, { 1273, 629 }, { 1269, 627 }, { 1267, 626 }, { 1265, 625 },
- { 12797, 335 }, { 11920, 335 }, };
- private static int[][] pickaxe = { { 11920, 61, 0 }, // DRAGON
- { 12797, 61, 0 }, // DRAGON (OR)
- { 1275, 41, 2 }, // RUNE
- { 1271, 31, 3 }, // ADDY
- { 1273, 21, 4 }, // MITH
- { 1269, 6, 5 }, // STEEL
- { 1267, 1, 6 }, // IRON
- { 1265, 1, 7 }, // BRONZE
- };
- private static int[][] data = {
- { 7484, 436, 1, 18, 1, 5 }, // COPPER
- { 7453, 436, 1, 18, 1, 5 }, // COPPER
- { 7485, 438, 1, 18, 1, 5 }, // TIN
- { 7486, 438, 1, 18, 1, 5 }, // TIN
- { 7455, 440, 15, 35, 2, 5 }, // IRON
- { 7488, 440, 15, 35, 2, 5 }, // IRON
- { 7456, 453, 30, 50, 3, 8 }, // COAL
- { 7489, 453, 30, 50, 3, 8 }, // COAL
- { 7491, 444, 40, 65, 3, 10 }, // GOLD
- { 7458, 444, 40, 65, 3, 10 }, // GOLD
- { 7492, 447, 55, 80, 5, 20 }, // MITH
- { 7459, 447, 55, 80, 5, 20 }, // MITH
- { 7460, 449, 70, 95, 7, 50 }, // ADDY
- { 7493, 449, 70, 95, 7, 50 }, // ADDY
- //{ 7492, 449, 70, 95, 7, 50 }, // ADDY
- { 7485, 442, 20, 40, 5, 5 }, // SILVER
- { 7486, 442, 20, 40, 5, 5 }, // SILVER
- { 14912, 1436, 20, 40, 0, 5 },
- { 7419, 451, 85, 125, 40, 100 }, // RUNE
- { 7461, 451, 85, 125, 40, 100 }, // RUNE
- { 7494, 451, 85, 125, 40, 100 }, // RUNE
- { 7418, 451, 85, 125, 40, 100 },// RUNE
- };
- @Override
- public void execute() {
- /*
- * If the player is no longer active, terminate the task.
- */
- if (!getPlayer().isActive()) {
- stop();
- return;
- }
- /*
- * If the player no longer has a pickaxe, terminate the task.
- */
- if (!hasPickaxe(getPlayer())) {
- this.stop();
- return;
- }
- /*
- * If the player has no inventory, terminate the task.
- */
- if (!SkillHandler.noInventorySpace(getPlayer(), "mining")) {
- this.stop();
- return;
- }
- /*
- * If the rock is depleted, stop.
- */
- if (MineralContainer.isMineralDepleted(10797, rock.getPosition())) {
- getPlayer().sendMessage("This mineral is depleted, wait until it refills.");
- getPlayer().write(new SendSoundPacket(431, 0, 0));
- this.stop();
- return;
- }
- if ((animationDelay <= 1) || (animationDelay == 4) || (animationDelay == 6) || (animationDelay == 11)) {
- getPlayer().write(new SendSoundPacket(432, 10, animationDelay == 11 ? 10 : 0));
- }
- /*
- * Perform our animation
- */
- if (animationDelay++ % 15 == 0) {
- getPlayer().startAnimation(animation);
- }
- /*
- * The time has been reached, add the ore and experience
- */
- if (incr++ == totalDelay) {
- /*
- * Collect the item id ore
- */
- getPlayer().getItems().addItem(rock.getItem());
- /*
- * Does the player have a dragon pickaxe? If so, add an extra ore
- */
- int chance = Misc.random(5);
- if (getPlayer().playerEquipment[Player.playerWeapon] == 15813 && chance == 3 && getPlayer().getItems().freeSlots() > 2) {
- getPlayer().getItems().addItem(rock.getItem());
- getPlayer().sendMessage("You feel your pickaxe hit slightly harder..");
- }
- /*
- * Send the message once we've mined the ore
- */
- getPlayer().sendMessage("You manage to mine some " + rock.getItem().getDefinition().getName() + ".");
- /*
- * Add our experience
- */
- getPlayer().getPA().addSkillXP(rock.getExperience() * SkillHandler.MINING_XP, Player.playerMining);
- /*
- * If not rune essence, stop mining
- */
- if (rock.getId() != 14912) {
- GemData.get(getPlayer());
- if (Player.inResourceArea(getPlayer()) && rock.getId() == 14175) {
- } else {
- MineralContainer.addMineral(new DepletedMineral(10797, rock.getId(), rock.getPosition()));
- addExpirableRock(getRespawnTime(rock.getId()));
- }
- ObjectHandler.createAnObject(getPlayer(), 10797, rock.getPosition().getX(), rock.getPosition().getY());
- this.stop();
- } else {
- /*
- * Reset our timer
- */
- incr = 0;
- totalDelay = getTimer(getPlayer(), rock.getId());
- }
- }
- }
- /**
- * Adds a task to expire the rock after the provided delay
- *
- * @param delay
- * The delay before expiring the empty ore
- */
- private void addExpirableRock(int delay) {
- Server.getTaskScheduler().schedule(new Task(delay) {
- @Override
- public void execute() {
- ObjectHandler.createAnObject(getPlayer(), rock.getId(), rock.getPosition().getX(), rock.getPosition().getY());
- for (Iterator<DepletedMineral> i = MineralContainer.getDepletedMinerals().iterator(); i.hasNext();) {
- DepletedMineral mineral = i.next();
- if (mineral.getPosition().equals(rock.getPosition()) && mineral.getRockId() == rock.getId()) {
- i.remove();
- }
- }
- this.stop();
- }
- });
- }
- /**
- * Once the event stops, reset the boolean.
- */
- @Override
- public void onStop() {
- getPlayer().startAnimation(65535);
- SkillHandler.setSkillActive(getPlayer(), Player.playerMining, false);
- }
- @Override
- public String toString() {
- return "Mining task [" + getPlayer() + ", rock=" + rock.getId() + ", position=" + rock.getPosition() + "]";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement