Jump to content

Welcome to DreamBot!

Download for Free

Supercharge Your Bots

Run unlimited bots today using DreamBot's Covert Mode and
stay more protected.

Upgrade Now
Frequently Asked Questions
  • Are you not able to open the client? Make sure you have Java 8 installed
  • 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 gold? You can purchase vouchers from other users
  • Try asking for help in the chatbox
OSRS Gambling

Interested in advertising your business? Reach out today!

Download the DreamBot client today!
Ericthecmh

Scripting for DreamBot - Part 1 - A simple Chicken Killer

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

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...