Ericthecmh 184 Posted November 21, 2014 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: Script Skeleton onStart and onLoop Idle mouse NPCs and interaction with NPCs Conditional sleeping Random numbers 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
Superman 49 Posted November 21, 2014 Oo nice tutorial, Im sure this will at least make the new learners get interested and learn more about coding.
Nick 19 Posted November 21, 2014 Thanks for the tutorial. Hopefully this will assist others in the future.
Ericthecmh 184 Author Posted November 21, 2014 You get chicken twice I'm aware of that. I just wanted to show how to use filters as well as the plain boring simple way
Chris 154 Posted November 21, 2014 I'm aware of that. I just wanted to show how to use filters as well as the plain boring simple way Just pressing your buttons
Whopper 30 Posted January 12, 2015 Seems simple enough. I'll take a look at the API and I guess we'll go from there. Thanks, .
Recommended Posts
Archived
This topic is now archived and is closed to further replies.