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
  • Try asking for help in the chatbox
  • DreamBot 3 Miner Tutorial, Part 1


    Pandemic
     Share

    Recommended Posts

    Hello everyone, this will be a series of short tutorials expanding on a miner for the DreamBot 3 client, starting with a simple script that just mines and drops, eventually compounding to a fully complete miner that has antiban methods, banking, a GUI, and whatever else I think of :)

    This initial tutorial will be very brief, I won't be explaining every line of code, however future parts may be more in depth.

    Prerequisites

    This is meant to be for people who are somewhat familiar with Java or similar languages, and new to DreamBot or OSRS bot making. I won't be discussing setting up Java or your development software for building the script's output. This will assume you already have a basic knowledge of Java programming and an IDE ready to go. If you need help with that, check out this guide to get a basic setup ready:

    Project Layout

    To start, create a new project in your IDE and call it anything you'd like. Inside your project folder, create a folder layout and create the three starting classes, it should look like this:

    layout.png

    The 'src' folder is important if you ever decide to upload a script to our SDN.

    The Script

    Let's start with what the script should be able to do by the end of Part 1:

    • Mine any rocks, just whatever is closest
    • If the inventory is full, drop all of the ore
    • Show a basic paint letting us know how much experience we've gained

    For this script, I've decided to go with a node or task setup. Basically we'll make simple, self contained tasks that the client will run whenever they're ready to be ran. We can optionally set a priority so more important tasks will run first, but we'll save that for a later tutorial.

    Miner.java, the script's main class

    For this basic setup, here's the simple starting class for our miner:

    Spoiler
    package src;
    
    import org.dreambot.api.methods.skills.Skill;
    import org.dreambot.api.methods.skills.SkillTracker;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.script.impl.TaskScript;
    import src.tasks.DropTask;
    import src.tasks.MiningTask;
    
    import java.awt.*;
    
    // Every script needs a ScriptManifest so it can be seen in the script manager
    @ScriptManifest(category = Category.MINING, name = "Miner", description = "Mines anything, and drops inventory when full.", author = "Pandemic", version = 1.0)
    public class Miner extends TaskScript {
    
        @Override
        public void onStart() {
            // Start DreamBot's skill tracker for the mining skill, so we can later see how much experience we've gained
            SkillTracker.start(Skill.MINING);
    
            // Now add our two tasks so the client knows what to do
            addNodes(new MiningTask(), new DropTask());
        }
    
        @Override
        public void onPaint(Graphics g) {
            String experienceGainedText = String.format(
                    "Mining Experience: %d (%d per hour)", // The paint's text format. '%d' will be replaced with the next two arguments.
                    SkillTracker.getGainedExperience(Skill.MINING),
                    SkillTracker.getGainedExperiencePerHour(Skill.MINING)
            );
    
            // Now we'll draw the text on the canvas at (5, 35). (0, 0) is the top left of the canvas.
            g.drawString(experienceGainedText, 5, 35);
        }
    
    }

     

    Points of Interest:

    • @ScriptManifest: This is an annotation that helps us show your script in the script manager.
    • extends TaskScript: TaskScript is a subclass of AbstractScript that handles the basic task/node logic so we can focus on creating discrete tasks.
    • onStart(): This method is called whenever someone starts the script through DreamBot.
    • onPaint(Graphics g): This method is called on every frame render, usually 60 times per second. For performance reasons, you want to minimize the amount of processing you do in this method.

    The Tasks

    Now that the script's main class is added, let's make the mining task.

    Mining

    Now we need to figure out how to know which rocks are mine-able. Load into the game and walk by any rocks you can mine.

    Now you can enable the Developer Mode in the settings panel:

    settings.png

    Then in the menu bar that shows up, open the Game Explorer:

    explorer.png

     

    Go to the "Game Objects" tab and choose the closest rock to you so we can see if anything changes when a rock is mined:

    unmined.png

    After mining the rock, press refresh and select the same rock again:

    mined.png

    As you can see, a few things did change: the rock's ID, model colors, modified colors, and index. To avoid hardcoding any specific rock ids, since we don't care what type of rock it is, I think we should just check for non-null model colors.

    MiningTask.java

    Spoiler
    package src.tasks;
    
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.container.impl.Inventory;
    import org.dreambot.api.methods.interactive.GameObjects;
    import org.dreambot.api.script.TaskNode;
    import org.dreambot.api.wrappers.interactive.GameObject;
    
    public class MiningTask extends TaskNode {
    
        @Override
        public boolean accept() {
            // If our inventory isn't full and we're not mining, we should start
            return !Inventory.isFull() && !isMining();
        }
    
        @Override
        public int execute() {
            GameObject rock = getClosestRock();
    
            // If there aren't any available rocks near us, we should just wait until one's available
            if (rock == null) return Calculations.random(500, 1000);
    
            if (rock.interact("Mine")) { // If we successfully click on the rock
                sleepUntil(this::isMining, 2500); // Wait until we're mining, with a max wait time of 2,500ms (2.5 seconds)
            }
    
            return Calculations.random(500, 1000);
        }
    
        /**
         * Finds the closest acceptable rock that we can mine
         *
         * @return The closest GameObject that has the name 'Rocks', with the action 'Mine', and non-null model colors
         */
        private GameObject getClosestRock() {
            return GameObjects.closest(object -> object.getName().equalsIgnoreCase("Rocks") && object.hasAction("Mine") && object.getModelColors() != null);
        }
    
        /**
         * For part 1, we'll consider our player doing any animation as mining
         * This will be improved/changed in a future part
         *
         * @return true if the player is mining, otherwise false
         */
        private boolean isMining() {
            return getLocalPlayer().isAnimating();
        }
    
    }

     

    Points of Interest:

    • accept(): This is used by DreamBot to decide if it should run this task's execute method.
    • execute(): This is the method called whenever the task runs.

    Dropping the Inventory

    With the mining task finished, it'll mine until the inventory is full, then just sit around waiting. If you enable input and drop a few ore, the script will pick right back up where it was and mine until it's full again.

    Dropping our entire inventory is pretty easy to do, so our task's class file is really simple:

    DropTask.java

    Spoiler
    package src.tasks;
    
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.container.impl.Inventory;
    import org.dreambot.api.script.TaskNode;
    
    public class DropTask extends TaskNode {
    
        @Override
        public boolean accept() {
            // If our inventory is full, we should execute this task
            return Inventory.isFull();
        }
    
        @Override
        public int execute() {
            // This method will drop any items in our inventory that have 'ore' anywhere in its name
            Inventory.dropAll(item -> item.getName().contains("ore"));
    
            return Calculations.random(300, 600);
        }
    }

     

     

    We're Done!

    With that, everything should now compile. Make sure your script's output goes to the (USER_HOME)/DreamBot/Scripts/ folder, then run the script in the client and it should start working, paint and all:

    paint.png

     

     

    You can find all of the source files attached in case you had trouble setting it up correctly :)

    If you have any questions or have anything you'd like to see included for the end result, please post it below.

    Thanks for reading!

    Miner.zip

    Link to comment
    Share on other sites

    Very good tutorial and code! 

    Little suggestion to add for tutorial, allow bot to only mine rocks which it can depending on level. 
     

    Get rock to mine.

    GameObject rockToMine = getGameObjects().closest(new Filter<GameObject>() {
                @Override
                public boolean match(GameObject rock) {
                    return rock != null && rock.hasAction("Mine") &&
                            rock.getName().contains("Rocks") &&
                            rock.getModelColors() != null &&
                            Rock.getAllowedRocksByLevel(getSkills().getRealLevel(Skill.MINING)).
                                    anyMatch(rockEnum -> contains(rock.getModelColors(), rockEnum.getColour()));
                }
            });

    And then do filtering for colors in Rock enum. 

        public static Stream<Rock> getAllowedRocksByLevel(int level) {
            return Rock.stream()
                    .filter(p -> p.getLevel() <= level);
        }


    I wrote this bot code yesterday so please give some feedback about it :D 

    Rock.java MinerNode.java

    Link to comment
    Share on other sites

    6 minutes ago, Bumble said:

    Very good tutorial and code! 

    Little suggestion to add for tutorial, allow bot to only mine rocks which it can depending on level. 
     

    Get rock to mine.

    GameObject rockToMine = getGameObjects().closest(new Filter<GameObject>() {
                @Override
                public boolean match(GameObject rock) {
                    return rock != null && rock.hasAction("Mine") &&
                            rock.getName().contains("Rocks") &&
                            rock.getModelColors() != null &&
                            Rock.getAllowedRocksByLevel(getSkills().getRealLevel(Skill.MINING)).
                                    anyMatch(rockEnum -> contains(rock.getModelColors(), rockEnum.getColour()));
                }
            });

    And then do filtering for colors in Rock enum.  Credits LogicSoup for basic Rock enum structure.    

        public static Stream<Rock> getAllowedRocksByLevel(int level) {
            return Rock.stream()
                    .filter(p -> p.getLevel() <= level);
        }


    I wrote this bot code yesterday so please give some feedback about it :D 

    Rock.java 863 B · 0 downloads MinerNode.java 1.9 kB · 0 downloads

     

    Link to comment
    Share on other sites

    • 2 weeks later...

    Thanks for the tutorial. This was very helpful.

    Any chance we can get a tutorial on making/adding a custom web node?

    Link to comment
    Share on other sites

    Thanks, it helped me to start writing agian. Hopefully I can do a suggestion for the next step; Add a banking task (BankTask.java) and show how to select DropTask or BankTask in the miner.java (Exemple: make it markable to use banking and default to drop.)

    Link to comment
    Share on other sites

    EDIT:
    Hey i managed to compile and run the script i made using your tutorial, works wonders. Only problem im having atm is with onPaint, apparently it doesnt print anything to the client script.

    Awesome tutorial!

    Edited by aeonee1
    Link to comment
    Share on other sites

    Create an account or sign in to comment

    You need to be a member in order to leave a comment

    Create an account

    Sign up for a new account in our community. It's easy!

    Register a new account

    Sign in

    Already have an account? Sign in here.

    Sign In Now
     Share

    ×
    ×
    • 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.