Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

First script - please give me feedback!

Joined
Nov 6, 2015
Messages
583
Hey, I have started to work on this essense miner for like 6-8 hours, it runs nicely now but it still needs some improvements.

Things which I can't fix:

  1. Open a wrong door(Used the dooropener to open the door to get to Aubury)
  2. Will be updated.
[V1]

Code:
package com.remco.bots.EssenseMiner;

import com.runemate.game.api.hybrid.entities.GameObject;
import com.runemate.game.api.hybrid.entities.Npc;
import com.runemate.game.api.hybrid.entities.Player;
import com.runemate.game.api.hybrid.local.Camera;
import com.runemate.game.api.hybrid.local.hud.interfaces.Bank;
import com.runemate.game.api.hybrid.local.hud.interfaces.Inventory;
import com.runemate.game.api.hybrid.location.Area;
import com.runemate.game.api.hybrid.location.Coordinate;
import com.runemate.game.api.hybrid.location.navigation.Path;
import com.runemate.game.api.hybrid.location.navigation.Traversal;
import com.runemate.game.api.hybrid.location.navigation.basic.BresenhamPath;
import com.runemate.game.api.hybrid.location.navigation.web.WebPath;
import com.runemate.game.api.hybrid.region.GameObjects;
import com.runemate.game.api.hybrid.region.Npcs;
import com.runemate.game.api.hybrid.region.Players;
import com.runemate.game.api.script.Execution;
import com.runemate.game.api.script.framework.LoopingScript;
import com.runemate.game.api.script.framework.listeners.ChatboxListener;
import com.runemate.game.api.script.framework.listeners.events.MessageEvent;

/**
* PowerMiner for RuneMate tutorial
* Created by SlashnHax
*/
public class EssenceMiner extends LoopingScript implements ChatboxListener {

    private final static Player player = Players.getLocal();
    private final static Area bankArea = new Area.Rectangular(new Coordinate(3249, 3415, 0), new Coordinate(3257, 3424, 0));
    private final static Area teleportArea = new Area.Rectangular(new Coordinate(3255, 3399, 0), new Coordinate(3252, 3404, 0));

    private static boolean isInBank() {
        return bankArea.contains(player);
    }
    private static boolean isAtAubury() {
        Npc aubury = Npcs.newQuery().filter(Npcs.getNameFilter("Aubury")).results().nearest();
        if (aubury != null) {
            return true;
        }
        return false;
    }
    private boolean DoorClosed = false;
    GameObject rocks;

    private enum State{
        MINE, Banking, WAIT, WalkingToBank, WalkingToMine, TeleportingToMine, OpenDoor;
    }


    @Override
    public void onStart(String... args){
        setLoopDelay(2000, 3000);
    }

    @Override
    public void onLoop() {
        switch(getCurrentState()){
            case MINE:
                rocks = GameObjects.newQuery().names("Rune Essence").results().nearest();
                if(rocks != null && rocks.getDefinition() != null){
                    if(!rocks.isVisible()){
                        Camera.turnTo(rocks);
                    }
                    if(rocks.interact("Mine")){
                        Execution.delayUntil(()->Players.getLocal().getAnimationId() != -1, 500, 5000);
                    }
                }
                break;
            case OpenDoor:

            break;
            case WalkingToBank:
                Npc exitPortal = Npcs.newQuery().filter(Npcs.getNameFilter("Portal")).results().nearest();
                if (exitPortal != null) {
                    if (!exitPortal.isVisible()) {
                        Camera.turnTo(exitPortal);
                    }
                    Path p = BresenhamPath.buildTo(exitPortal);
                    if (p != null)
                    {
                        p.step();
                        Camera.turnTo(exitPortal);
                        Execution.delay(3000, 4000);
                    }

                    if (exitPortal.click())
                    {
                        System.out.println("Left the mining area!");
                        Execution.delay(6000, 7000);
                    }
                    else
                    {
                        System.out.println("We failed leaving the mining area!");
                        exitPortal.click();
                        Execution.delay(6000, 7000);
                    }

                    GameObject door = GameObjects.newQuery().names("Door").visible().actions("Open").results().nearest();
                    if (door != null) {
                        Camera.turnTo(door);
                        if (!door.isVisible()) {
                            Camera.turnTo(door);
                        } else if (door.interact("Open")) {
                            System.out.println("Opened the door!");
                            Execution.delay(2000, 3000);
                        }
                    }

                    Coordinate coord = new Coordinate(3253, 3420, 0);
                    for(int i = 0; i < 2; i++) {
                        System.out.println("Walking to the bank!");
                        final WebPath path = Traversal.getDefaultWeb().getPathBuilder().buildTo(coord);
                        path.step();
                        Execution.delay(7500, 9000);
                    }

                }
                break;
            case WAIT:
                break;

            case Banking:
                System.out.println("Banking!");
                if (Bank.isOpen()) {
                    Bank.depositInventory();
                    Bank.close();
                }
                else {
                    Bank.open();
                    Bank.depositInventory();
                    Bank.close();
                }

                break;
                case WalkingToMine:
                    Coordinate coord = new Coordinate(3253, 3401, 0);
                    for(int i = 0; i < 2; i++) {
                        System.out.println("Walking to the mine!");
                        final WebPath path = Traversal.getDefaultWeb().getPathBuilder().buildTo(coord);
                        path.step();
                        Execution.delay(8000, 10000);
                    }



                break;
                case TeleportingToMine:
                    Npc aubury = Npcs.newQuery().filter(Npcs.getNameFilter("Aubury")).results().nearest();
                    if (aubury != null) {
                        Camera.turnTo(aubury);

                        GameObject door = GameObjects.newQuery().names("Door").visible().actions("Open").results().nearest();
                        if (door != null) {
                            Camera.turnTo(door);
                            if (!door.isVisible()) {
                                Camera.turnTo(door);
                            } else if (door.interact("Open")) {
                                System.out.println("Opened the door!");
                                Execution.delay(2000, 3000);
                            }
                        }

                        if (!aubury.isVisible()) {
                            Camera.turnTo(aubury);
                        }
                        if (!aubury.isVisible()) {
                            Path p = BresenhamPath.buildTo(aubury);
                            if (p != null)
                                p.step();
                        }
                        if (aubury.interact("Teleport")) {
                            System.out.println("Went to the mining area!");
                        } else {
                            System.out.println("We failed entering the mining area!");
                            aubury.interact("Teleport");
                        }
                    }
                break;
        }
    }

    @Override
    public void onStop(){

    }

    public void onMessageReceived(MessageEvent message) {
        /*

        Not working!


        String msg = message.getMessage();
        System.out.println(msg);
        if (message.getSender().equals("") && msg.contains("You can't reach that")) {
            DoorClosed = true;
        }
        */
    }


    private State getCurrentState() {
        System.out.println(DoorClosed);
        if (isInBank() && !Inventory.isEmpty()) {
            return State.Banking;
        }

        else if (DoorClosed) { // Unused!
            return State.OpenDoor;
        }

        else if (isAtAubury() && Inventory.isEmpty())
        {
            System.out.println("Called tp to mine");
            return State.TeleportingToMine;
        }
        else if (isInBank() && Inventory.isEmpty()) {
            return State.WalkingToMine;
        }
        else if(Inventory.isFull() && !isInBank()){
            return State.WalkingToBank;
        } else if (Players.getLocal().getAnimationId() == -1 || rocks == null || !rocks.isValid()) {
            return State.MINE;

        } else {
            return State.WAIT;
        }
    }
}

[V2]

Code:
package com.remco.bots.EssenseMiner;

import com.runemate.game.api.client.paint.PaintListener;
import com.runemate.game.api.hybrid.RuneScape;
import com.runemate.game.api.hybrid.entities.GameObject;
import com.runemate.game.api.hybrid.entities.Npc;
import com.runemate.game.api.hybrid.entities.Player;
import com.runemate.game.api.hybrid.entities.definitions.ItemDefinition;
import com.runemate.game.api.hybrid.local.Camera;
import com.runemate.game.api.hybrid.local.Skill;
import com.runemate.game.api.hybrid.local.hud.interfaces.Bank;
import com.runemate.game.api.hybrid.local.hud.interfaces.InterfaceWindows;
import com.runemate.game.api.hybrid.local.hud.interfaces.Inventory;
import com.runemate.game.api.hybrid.location.Area;
import com.runemate.game.api.hybrid.location.Coordinate;
import com.runemate.game.api.hybrid.location.navigation.Path;
import com.runemate.game.api.hybrid.location.navigation.Traversal;
import com.runemate.game.api.hybrid.location.navigation.basic.BresenhamPath;
import com.runemate.game.api.hybrid.location.navigation.web.WebPath;
import com.runemate.game.api.hybrid.region.GameObjects;
import com.runemate.game.api.hybrid.region.Npcs;
import com.runemate.game.api.hybrid.region.Players;
import com.runemate.game.api.hybrid.util.StopWatch;
import com.runemate.game.api.script.Execution;
import com.runemate.game.api.script.framework.LoopingScript;
import com.runemate.game.api.script.framework.listeners.InventoryListener;
import com.runemate.game.api.script.framework.listeners.SkillListener;
import com.runemate.game.api.script.framework.listeners.events.ItemEvent;
import com.runemate.game.api.script.framework.listeners.events.MessageEvent;
import com.runemate.game.api.script.framework.listeners.events.SkillEvent;

import java.awt.*;
import java.util.Random;

/**
* PowerMiner for RuneMate tutorial
* Created by SlashnHax
*/
public class EssenceMiner extends LoopingScript implements PaintListener, InventoryListener, SkillListener {

    private final static Player player = Players.getLocal();
    private final static Area bankArea = new Area.Circular(new Coordinate(3253, 3421, 0), 5);
    private final static Area teleportArea = new Area.Rectangular(new Coordinate(3255, 3399, 0), new Coordinate(3252, 3404, 0));
    private final static Area doorArea = new Area.Circular(new Coordinate(3253, 3401, 0), 6);

    private final static StopWatch runtime = new StopWatch();

    private static boolean isInBank() {
        return bankArea.contains(player);
    }
    private static boolean isNearDoor() {
        return doorArea.contains(player);
    }
    private static boolean isAtAubury() {
        Npc aubury = Npcs.newQuery().filter(Npcs.getNameFilter("Aubury")).results().nearest();
        if (aubury != null) {
            return true;
        }
        return false;
    }

    public static int toHour(double detail){
        long difference = System.currentTimeMillis() - runtime.getRuntime();
        double xpH = (3600000 * detail) / difference;
        return (int)xpH;
    }

    private String Status = "Starting up";
    private boolean DoorClosed = false;


    private static int oreCount = 0;
    private static int orePrice = 0;
    private static int levelsGained = 0;

    private boolean isIdle() {
        return player.getAnimationId() == -1 && !player.isMoving();
    }

    GameObject rocks;

    private enum State{
        MINE, Banking, WAIT, WalkingToBank, WalkingToMine, TeleportingToMine, OpenDoor, ExitingMine;
    }


    @Override
    public void onPaint(Graphics2D g){
        Color transBlack = new Color(0, 0, 0, 150);
        //Draw trans rect
        g.setColor(transBlack);
        g.fillRect(0, 0, 215, 115);
        //Draw border of rect
        g.setColor(Color.red);
        g.drawRect(0, 0, 215, 115);
        //Draw green underline under title
        g.drawLine(5, 20, 195, 20);
        //Draw text
        g.setColor(Color.white);
        // Text
        g.drawString("Essence Miner", 80, 15);
        g.drawString("Run time: " + runtime.getRuntimeAsString(), 5, 35);
        if (RuneScape.isLoggedIn()) {
            g.drawString("Mining level: " + Skill.MINING.getCurrentLevel() + " + " + levelsGained, 5, 50);
            g.drawString("Experience to level: " + Skill.MINING.getExperienceToNextLevel(), 5, 65);
            g.drawString("Ores mined: " + oreCount + " (" + toHour(oreCount) + "/H)", 5, 80);
            g.drawString("Money gained: " + orePrice * oreCount + "(" + toHour(oreCount) + "/H)", 5, 95);
            g.drawString("Status: " + Status, 5, 110);
        }
    }




    @Override
    public void onStart(String... args){
        setLoopDelay(2000, 3000);
        getEventDispatcher().addListener(this);
        runtime.start();
    }

    public static int randInt(int min, int max) {


        Random rand = new Random();
        int randomNum = rand.nextInt((max - min) + 1) + min;

        return randomNum;
    }

    @Override
    public void onLoop() {
        switch(getCurrentState()){
            case MINE:
                int antibanType = randInt(0, 5);
                rocks = GameObjects.newQuery().names("Rune Essence").results().nearest();
                if(rocks != null && rocks.getDefinition() != null){
                    if(!rocks.isVisible()){
                        Path p = BresenhamPath.buildTo(rocks);
                        if (p != null)
                        {
                            p.step();
                            Camera.turnTo(rocks);
                            Execution.delayUntil(()->Players.getLocal().getAnimationId() != -1, 3000, 3500);
                        }
                    }
                    if(rocks.interact("Mine")){
                        Status = "Mining!";
                        Execution.delayUntil(()->Inventory.isFull(), 20000, 25000);
                        // Bit antiban

                        switch(antibanType) {
                            case 0:
                                Status = "Some anticheat";
                                if (!InterfaceWindows.getInventory().isOpen()) {
                                    InterfaceWindows.getInventory().open();
                                    if (!Inventory.isEmpty()) {

                                    }
                                }
                                else
                                {
                                    InterfaceWindows.getSkills().open();

                                }

                            break;
                            case 1:

                            break;
                        }
                    }
                }
                break;
            case OpenDoor:
                GameObject door = GameObjects.newQuery().names("Door").within(doorArea).actions("Open").results().nearest();
                if (door != null) {
                    System.out.println("Door selected");
                    Camera.turnTo(door);
                    if (!door.isVisible()) {
                        Camera.turnTo(door);
                    } else if (door.interact("Open")) {
                        Status = "Opened the closed door!";
                        Execution.delay(2000, 3000);
                    }
                }
            break;
            case ExitingMine:
                Npc exitPortal = Npcs.newQuery().filter(Npcs.getNameFilter("Portal")).results().nearest();
                if (exitPortal != null) {
                    Camera.turnTo(exitPortal);
                    Path p = BresenhamPath.buildTo(exitPortal);
                    if (p != null) {
                        p.step();
                        Camera.turnTo(exitPortal);
                        Execution.delayUntil(() -> Players.getLocal().getAnimationId() != -1, 3000, 3500);
                    }

                    if (exitPortal.interact("Enter")) {
                        Status = "Left the mine through the portal.";
                        Execution.delayUntil(() -> isNearDoor(), 8000);
                    } else {
                        Status = "We seem to have failed leaving the mine.";
                        Path pa = BresenhamPath.buildTo(exitPortal);
                        if (pa != null) {
                            pa.step();
                            Camera.turnTo(exitPortal);
                            Execution.delayUntil(() -> Players.getLocal().getAnimationId() != -1, 4000, 4500);
                        }
                        exitPortal.click();
                        Execution.delayUntil(() -> isNearDoor(), 8000);
                    }
                }
                break;
            case WalkingToBank:
                    GameObject backdoor = GameObjects.newQuery().names("Door").within(doorArea).actions("Open").results().nearest();
                    if (backdoor != null) {
                        Status = "Door is locked! Time to unlock it!";
                        Camera.turnTo(backdoor);
                        if (!backdoor.isVisible()) {
                            Camera.turnTo(backdoor);
                        } else if (backdoor.interact("Open")) {
                            Status = "Door has been opened";
                            Execution.delayUntil(()->Players.getLocal().getAnimationId() != -1, 2000, 3000);
                        }
                    }


                    Status = "Walking to the Bank";
                    Coordinate coord = new Coordinate(3254, 3423, 0);
                    for(int i = 0; i < 2; i++) {

                        final WebPath path = Traversal.getDefaultWeb().getPathBuilder().buildTo(coord);
                        path.step();
                        Execution.delay(5000, 6000);
                    }

                    if (!isInBank()) {
                        Path pa = BresenhamPath.buildTo(coord);
                        if (pa != null) {
                            pa.step();
                            Execution.delayUntil(() -> isInBank(), 5000);
                        }
                    }


                break;
            case WAIT:
                break;
            case Banking:
                Status = "Banking";

                if (Bank.isOpen()) {
                    Execution.delayUntil(Bank::isOpen, 250, 2000);
                    Bank.depositInventory();
                    Bank.close();
                }
                else {
                    Bank.open();
                    Execution.delayUntil(Bank::isOpen, 250, 2000);
                    Bank.depositInventory();
                    Bank.close();
                }

                break;
                case WalkingToMine:
                    Coordinate coordToMine = new Coordinate(3253, 3398, 0);

                    for(int i = 0; i < 2; i++) {
                        Status = "Walking to the Mine";
                        final WebPath path = Traversal.getDefaultWeb().getPathBuilder().buildTo(coordToMine);
                        path.step();
                        Execution.delayUntil(() -> isIdle(), 7000);
                    }



                break;
                case TeleportingToMine:
                    Npc aubury = Npcs.newQuery().filter(Npcs.getNameFilter("Aubury")).results().nearest();
                    if (aubury != null && Inventory.isEmpty()) {
                        Camera.turnTo(aubury);



                        if (!aubury.isVisible()) {
                            Camera.turnTo(aubury);
                        }
                        if (!aubury.isVisible()) {
                            Path p = BresenhamPath.buildTo(aubury);
                            if (p != null)
                                p.step();
                        }
                        if (aubury.interact("Teleport")) {
                            Status = "Speaking to Aubury";
                            Execution.delayUntil(() -> isIdle(), 2000, 3000);
                        } else {
                            Status = "Failed talking to Aubury, let's try again.";
                            aubury.interact("Teleport");
                            Execution.delayUntil(() -> isIdle(), 2000, 3000);
                        }
                    }
                break;
        }
    }

    @Override
    public void onStop(){

    }

    @Override
    public void onItemAdded(ItemEvent event) {
        ItemDefinition definition = event.getItem().getDefinition();

        if (definition != null && definition.getName().equals("Rune essence") || definition.getName().equals("Pure essence")) {
            oreCount++;
        }
    }



    @Override
    public void onLevelUp(SkillEvent event) {
        if (event.getSkill().equals(Skill.MINING)) {
            ++levelsGained;
        }
    }

    public void onMessageReceived(MessageEvent message) {
        /*

        Not working!


        String msg = message.getMessage();
        System.out.println(msg);
        if (message.getSender().equals("") && msg.contains("You can't reach that")) {
            DoorClosed = true;
        }
        */
    }


    private State getCurrentState() {
        System.out.println(isNearDoor());
        if (isInBank() && !Inventory.isEmpty()) {
            return State.Banking;
        }

        else if (isNearDoor() && Inventory.isFull()) { // Unused!
            GameObject backdoor = GameObjects.newQuery().names("Door").within(doorArea).actions("Open").results().nearest();
            if (backdoor != null && Inventory.isFull()) {
                return State.OpenDoor;
            }
            else
            {
                return State.WalkingToBank;
            }

        }
        else if (isNearDoor() && Inventory.isEmpty()) {
            GameObject backdoor = GameObjects.newQuery().names("Door").within(doorArea).actions("Open").results().nearest();
            if (backdoor != null) {
                return State.OpenDoor;
            }
            else
            {
                return State.TeleportingToMine;
            }
        }

        else if (isNearDoor() && Inventory.isEmpty())
        {
            System.out.println("Called tp to mine");
            return State.TeleportingToMine;
        }
        else if (isInBank() && Inventory.isEmpty()) {
            return State.WalkingToMine;
        }
        else if(Inventory.isFull() && isNearDoor()) {
            return State.WalkingToBank;
        }
        else if(Inventory.isFull() && !isInBank()){
            return State.ExitingMine;
        }
         else if (Players.getLocal().getAnimationId() == -1 || rocks == null || !rocks.isValid()) {
            return State.MINE;

        } else {
            return State.WAIT;
        }
    }
}
'
Updated to V2.
 
Last edited:
Top