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
  • Scripting for DreamBot - Part 1 - A simple Chicken Killer


    Ericthecmh
     Share

    Recommended Posts

    Hey guys,

     

    With the beta releasing soon, I decided to take some time to make several tutorials to show the basics of scripting for DreamBot. Hopefully this will help our beta scripters and future scripters get familiar with our API faster.

     

    I'll start off with a simple chicken killer script. Covered in this tutorial:

    1. Script Skeleton
    2. onStart and onLoop
    3. Idle mouse
    4. NPCs and interaction with NPCs
    5. Conditional sleeping
    6. Random numbers
    7. Paint

    Code:

    import java.awt.*;
     
    import org.dreambot.api.methods.Calculations;
    import org.dreambot.api.methods.event.EventHandler;
    import org.dreambot.api.methods.event.PaintEvent;
    import org.dreambot.api.script.AbstractScript;
    import org.dreambot.api.script.Category;
    import org.dreambot.api.script.ScriptManifest;
    import org.dreambot.api.utilities.impl.Condition;
    import org.dreambot.api.utilities.impl.Filter;
    import org.dreambot.api.wrappers.interactive.NPC;
     
    // You must have @ScriptManifest in your main script class. This is how the client knows what your main class is
    @ScriptManifest(author="Ericthecmh", category= Category.COMBAT, name="Chicken Killer", version=0.1, description="Simple tutorial script showing how to kill chickens")
    // All scripts should extend AbstractScript
    public class ChickenKiller extends AbstractScript{
     
        // This method is called at the start of the script
        // Note that this method will run if you're logged out (before the login handler kicks in)
        // This method may or may not be overridden
        public void onStart(){
            log("Welcome to a boring Chicken Killer script!"); //Use log to print to the console
        }
     
        // onLoop is the main logic method. It will be called over and over again by the script manager. The return value
        // for onLoop should be the amount of time to wait between two consecutive onLoop calls. 
        //Returning < 0 will stop the script
        @Override
        public int onLoop() throws InterruptedException {
            getClient().disableIdleMouse(); // Idle mouse is a special antiban that Chris wrote. It'll monitor your script's *This could go in onStart to prevent unnecessary calls
            // mouse and afk when your script releases control. This is good for things like wcing which real people tend to
            // afk. I disabled it because nobody afks at chickens
            if (!getLocalPlayer().isInCombat()) {
                // If I'm not in combat...
     
                // Get chicken
                NPC chicken = getNpcs().getClosest("Chicken");
     
                // Uses a filter to get the closest NPC (to check for HP and other things)
                chicken = getNpcs().getClosest(new Filter<NPC>() {
                    @Override
                    public boolean match(NPC npc) {
                        return npc != null && npc.getName().equals("Chicken") && npc.getActions().length > 0 && !npc.isInCombat();
                    }
                });
     
                if(chicken == null)
                    return Calculations.random(300,600);
                // Attack the damn chicken
                if (chicken.interact("Attack")) {
                    // Wait a bit for player to be in combat
                    // This is a conditional sleep. This will cause the script to sleep until either the condition becomes
                    // true (ie, verify returns true), or the timeout is reached. The timeout is the second argument.
                    sleepUntil(new Condition() {
                        @Override
                        public boolean verify() {
                            return getPlayers().myPlayer().isInCombat();
                        }
                    },1000);
                }
            }
            return Calculations.random(300, 600); // Calculations.random to get random numbers
        }
     
        // We use an event bus for painting, so you must use @EventHandler to signal to the event bus
        @EventHandler
        public void onPaint(PaintEvent e){
            Graphics g = e.getGraphics();
            g.setColor(new Color(247, 148, 230));
            g.drawString("DreamBot is awesome!", 100, 100);
        }
    }
    
    

    *Edited by Nezz cuz Eric is a noob sometimes

    Link to comment
    Share on other sites

    • 1 month later...
    • 3 weeks later...

    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.