Jump to content
Frequently Asked Questions
  • Are you not able to open the client? Try following our getting started guide
  • Still not working? Try downloading and running JarFix
  • Help! My bot doesn't do anything! Enable fresh start in client settings and restart the client
  • How to purchase with PayPal/OSRS/Crypto gold? You can purchase vouchers from other users
  • Need some scripting help (state usage)


    infallible

    Recommended Posts

    Alright, I've been developing this miner script for a few days now and I'm making it more of an aio. I've recently made a bunch of changes to make the script look prettier as requested by someone and now the bot has too many issues and I just can't solve them... Please help me by taking a look at my code. Every time I start the bot now, it automatically gives "incompatible". I have no clue why as the states are pretty self-explanatory.

    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.map.Tile;
    import org.dreambot.api.methods.skills.Skill;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.listener.PaintListener;
    import org.dreambot.api.utilities.Timer;
    import org.dreambot.api.wrappers.interactive.GameObject;
    
    import java.awt.*;
    
    /**
     * Created by Hitsuu on 11/14/2015.
     */
    
    @ScriptManifest(author = "Hitsuu", name = "Advanced Miner Script", category = Category.MINING, version = 1.10)
    
    public class stateminer extends AbstractScript implements PaintListener{
    
        private long startTime = System.currentTimeMillis();
        private int oresmined = 0;
        public state state;
        private final Tile startTile = getStartTile();
        public enum state{
            MINE, BANK, GET_PICKAXE, WALK_MINE, NO_STATE
        }
        @Override
        public void onStart()
        {
            getCamera().rotateToPitch(383);
            getSkillTracker().start(Skill.MINING);
            startTime = System.currentTimeMillis();
    
    
    
        }
        public state getState()
        {
            GameObject myORE = getGameObjects().closest(ore -> ore.getID() == 13445);
            if (myORE.isOnScreen() && !getInventory().isFull() && getInventory().contains(item -> item.getName().contains("pickaxe")))
            {
                return state.MINE;
            } else if (getInventory().isFull())
            {
                return state.BANK;
            } else if(!getInventory().contains(item -> item.getName().contains("pickaxe")))
            {
                return state.GET_PICKAXE;
            } else if (!myORE.isOnScreen() && !getInventory().isFull() && getInventory().contains(item -> item.getName().contains("pickaxe")))
            {
                return state.WALK_MINE;
            }
            return state.NO_STATE;
        }
    
        private String stateParser() {
            if (state == state.MINE) {
                return "Mining";
            } else if (state == state.BANK) {
                return "Banking";
            } else if (state == state.WALK_MINE) {
                return "Walking to Mine";
            } else if (state == state.GET_PICKAXE) {
                return "Getting Pickaxe";
            } else if (state == state.NO_STATE) {
                return "No idea";
            }
            else return "incompatible";
        }
    
        @Override
        public int onLoop() {
            GameObject myORE = getGameObjects().closest(ore -> ore.getID() == 13445);
            state = getState();
            switch(state){
                case MINE:
                    mine(myORE);  //WALK_MINE runs to the mine, but the bot doesn't go to the rock and mine it.... Sits in "Mining" state. MUST START AT MINE LOCATION
                    break;
                case BANK:
                    bank();
                    break;
                case GET_PICKAXE:
                    get_pickaxe();
                    break;
                case WALK_MINE:
                    walk_mine();
                    break;
                case NO_STATE:
                    break;
            }
            return 500;
        }
    
        @Override
        public void onExit()
        {
        }
    
        private double progressBar()
        {
            return ((double) (getSkills().getExperience(Skill.MINING) - getSkills().getExperienceForLevel(getSkills().getRealLevel(Skill.MINING)))
                    / (getSkills().getExperienceForLevel(getSkills().getRealLevel(Skill.MINING) + 1) - getSkills().getExperienceForLevel(getSkills().getRealLevel(Skill.MINING)))) * 100;
        }
    
        @Override
        public void onPaint(Graphics g)
        {
    
            g.setColor(new Color(48, 48, 48, 180));
            g.fillRect(5, 240, 370, 99);
            g.setFont(new Font(Font.MONOSPACED, Font.ROMAN_BASELINE, 14));
            g.setColor(Color.WHITE);
            g.drawString("Private Miner   -", 10, 260);
            g.drawString("Runtime: " + Timer.formatTime(System.currentTimeMillis() - startTime), 220, 260);
            g.drawString("Exp/hr: " + getSkillTracker().getGainedExperiencePerHour(Skill.MINING) , 10, 280);
            g.drawString("Exp gained: " + getSkillTracker().getGainedExperience(Skill.MINING), 10, 300);
            g.drawString("Levels gained: " + getSkillTracker().getGainedLevels(Skill.MINING) , 220, 320);
            g.drawRect(10, 310, 100, 15);
            g.fillRect(10, 310, (int) progressBar(), 15);
            g.drawString("Ores mined: " + oresmined, 220, 300);
            g.drawString("State: " + stateParser(), 220, 280);
        }
    
        public void mine(GameObject myORE) {
        int oreID = 440;
            if (getInventory().contains(i -> i.getName().contains("pickaxe"))  && !getInventory().isFull()) {
                if (getLocalPlayer().distance(myORE) >3) {
                    getWalking().walk(myORE);
                    sleepUntil(() ->!getLocalPlayer().isMoving(), 300);
                }else if (!getLocalPlayer().isAnimating() && !getLocalPlayer().isMoving() && myORE.exists() && myORE.isOnScreen()) { //&& myORE != null
                    int oreCount = getInventory().count(438);
                    myORE.interact("Mine");
                        if(getLocalPlayer().isAnimating()) {
                            sleepUntil(() -> getInventory().count(oreID) > oreCount, Calculations.random(10000, 15000));
                            oresmined++;
                        }
                }
            } else if (getInventory().isFull()) {
                state = state.BANK;
            }
        }
    
        private void bank() {
            if (getInventory().isFull()) {
                if (getLocalPlayer().distance(getBank().getClosestBankLocation().getCenter()) > 10) {
                    getWalking().walk(getBank().getClosestBankLocation().getCenter());
                    sleepUntil(() -> !getLocalPlayer().isMoving()
                                    || getLocalPlayer().distance(getClient().getDestination()) < 8
                            , Calculations.random(1500, 3500));
                    if(getBank().isOpen()) {
                        sleepUntil(() -> getBank().isOpen(), Calculations.random(800, 1000));
                        getBank().depositAllExcept(f -> f.getName().contains("pickaxe"));
                        getBank().close();
                        sleepUntil(() -> !getBank().isOpen(), Calculations.random(800, 1000));
                        state = state.WALK_MINE;
                    }
                    else if (getBank().open()) {
                        sleepUntil(() -> getBank().isOpen(), Calculations.random(800, 1000));
                        getBank().depositAllExcept(f -> f.getName().contains("pickaxe"));
                        getBank().close();
                        sleepUntil(() -> !getBank().isOpen(), Calculations.random(2000, 800));
                        state = state.WALK_MINE;
                    }
                } else if (getBank().isOpen()) {
                    sleepUntil(() -> getBank().isOpen(), Calculations.random(800, 1000));
                    getBank().depositAllExcept(f -> f.getName().contains("pickaxe"));
                    getBank().close();
                    sleepUntil(() -> !getBank().isOpen(), Calculations.random(800, 1000));
                    state = state.WALK_MINE;
                } else if(getBank().open()){
                    sleepUntil(() -> getBank().isOpen(), Calculations.random(800, 1000));
                    getBank().depositAllExcept(f -> f.getName().contains("pickaxe"));
                    getBank().close();
                    sleepUntil(() -> !getBank().isOpen(), Calculations.random(2000, 800));
                       state = state.WALK_MINE;
                }
            }
        }
    
    
        private void walk_mine() {
            if (getLocalPlayer().distance(startTile) > 10 && !getInventory().isFull() && getInventory().contains(item -> item.getName().contains("pickaxe"))) {
                getWalking().walk(startTile);
                sleepUntil(() -> getLocalPlayer().getTile().equals(startTile), Calculations.random(500, 1000));
                if (getLocalPlayer().distance(startTile) < 5) {
                    state = state.MINE;
                }
            }
        }
    
        private void get_pickaxe(){
            if (getInventory().contains(i -> i.getName().contains("pickaxe"))) {
                state = state.WALK_MINE;
            } else {
                if (getLocalPlayer().distance(getBank().getClosestBankLocation().getCenter()) > 5) {
                    if (getWalking().walk(getBank().getClosestBankLocation().getCenter())) {
                        sleepUntil(() -> !getLocalPlayer().isMoving()
                                || getClient().getDestination().distance(getLocalPlayer()) > 8, Calculations.random(4500, 6500));
                    }
                } else {
                    if (getBank().isOpen()) {
                        if (getBank().withdraw(i -> i.getName().contains("pickaxe"))) {
                            sleep(Calculations.random(400, 800));
                            getBank().close();
                            state = state.WALK_MINE;
                        }
                    } else {
                        getBank().open();
                        sleepUntil(() -> getBank().isOpen(), Calculations.random(2000, 2500));
                        if (getBank().withdraw(i -> i.getName().contains("pickaxe"))) {
                            sleep(Calculations.random(400, 800));
                            getBank().close();
                            state = state.WALK_MINE;
                        }
                    }
                }
            }
        }
    }
    
    Link to comment
    Share on other sites

    I might've found your issue in this. In your stateParser() you try to compare the entire enum (state) to one of the states inside the enum, which is why it returns the incompatible string.

     

    To compare them correctly, try something like 

    if(getState() == state.MINE){
    return "Mining";
    }
    

    Not sure if this works as I haven't tested it, but I believe it should. :-)

    Link to comment
    Share on other sites

    Well, the first thing that I'd suggest is please fix the format of the code as

    heoyFTA.png

    Is really, really difficult to read.

     

    Second, use some java conventions, specifically make the enum State, as enums and classes start with capital letters.

     

    I would guess it's a conflict in the "state == state.MINING" kind of thing, since the enum is state, and your state is state, it doesn't know if you're comparing your enum state or your state state.

    Which is where conventions come in handy, if your enum is named State then

    state == State.MINING makes a whole lot more sense both visually and programmatically.

    Link to comment
    Share on other sites

    Well, the first thing that I'd suggest is please fix the format of the code as

    heoyFTA.png

    Is really, really difficult to read.

     

    Second, use some java conventions, specifically make the enum State, as enums and classes start with capital letters.

     

    I would guess it's a conflict in the "state == state.MINING" kind of thing, since the enum is state, and your state is state, it doesn't know if you're comparing your enum state or your state state.

    Which is where conventions come in handy, if your enum is named State then

    state == State.MINING makes a whole lot more sense both visually and programmatically.

    I will test this when I get home. Also, what makes it difficult to read? This is my first time creating a script and, really, even writing code. To be honest I'm focusing more on learning how to use the API than writing nicely, but I can see how writing the code so it's easier to read can help me learn more. So please, tell me what to change? Is it the closed squiggly brackets not being next to the method?

    I might've found your issue in this. In your stateParser() you try to compare the entire enum (state) to one of the states inside the enum, which is why it returns the incompatible string.

     

    To compare them correctly, try something like 

    if(getState() == state.MINE){
    return "Mining";
    }
    

    Not sure if this works as I haven't tested it, but I believe it should. :-)

    I don't think this is the case because since I called state = getState(); isn't everything the same? if(getState() == state.MINE) = if(state == state.MINE?

    Link to comment
    Share on other sites

    Is there a way to set the start location of the local player when the bot is started? I think I've used StartTile correctly, but it's not acting right, and I would like to use an area anyways instead of a specific tile.

     

    Anyways, for the StartTile I have private final Tile startTile = getStartTile(); at the top of my script as a declaration.

    Link to comment
    Share on other sites

    Is there a way to set the start location of the local player when the bot is started? I think I've used StartTile correctly, but it's not acting right, and I would like to use an area anyways instead of a specific tile.

     

    Anyways, for the StartTile I have private final Tile startTile = getStartTile(); at the top of my script as a declaration.

    You can't assign Dreambot specific objects in their declaration. Try setting it in your onStart method.

     

    Edit: At least you can't assign anything that has to be grabbed from the game, afaik.

    Link to comment
    Share on other sites

    You can't assign Dreambot specific objects in their declaration. Try setting it in your onStart method.

     

    Edit: At least you can't assign anything that has to be grabbed from the game, afaik.

    When I set it in the start method, anything else that calls for the startTile is greyed out and it gives me an error. Is there no way to create a storage location of where the bot was set so it can return to that point after banking? Or am I just missing another super simple API method like getMiningArea().closest <-- lol

    Link to comment
    Share on other sites

    Archived

    This topic is now archived and is closed to further replies.

    ×
    ×
    • Create New...

    Important Information

    We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.