How to Make an RSBot Script

23
Steps 1. Script Model 2. GUI Building 3. Player Movement 4. Objects, Menus, and Items 5. NPCs & Player Interactions 6. Player Properties Step 1 – Script Model import java.awt.*; import com.speljohan.rsbot.bot.Bot; import com.speljohan.rsbot.event.listeners.PaintListener; import com.speljohan.rsbot.script.Script; import com.speljohan.rsbot.script.wrappers.*; public class testscript extends Script implements PaintListener { public void onRepaint(Graphics g) { Bot.getEventManager().addListener(PaintListener.class, this); } public double getVersion() { return 1.0; } public String getScriptCategory() { return "Ben's Scripts"; } public String getName() { return "Test Script"; } public String getAuthor() { return "Ben B (Hunterbdb)"; }

description

This guide written by Ben B will show you how to create an RSBot Script for the RSBot.

Transcript of How to Make an RSBot Script

Page 1: How to Make an RSBot Script

Steps 1. Script Model 2. GUI Building 3. Player Movement 4. Objects, Menus, and Items 5. NPCs & Player Interactions 6. Player Properties

Step 1 – Script Model import java.awt.*;

import com.speljohan.rsbot.bot.Bot; import com.speljohan.rsbot.event.listeners.PaintListener; import com.speljohan.rsbot.script.Script; import com.speljohan.rsbot.script.wrappers.*;

public class testscript extends Script implements PaintListener {

public void onRepaint(Graphics g) { Bot.getEventManager().addListener(PaintListener.class, this); } public double getVersion() { return 1.0; } public String getScriptCategory() { return "Ben's Scripts"; } public String getName() { return "Test Script"; } public String getAuthor() { return "Ben B (Hunterbdb)"; }

Page 2: How to Make an RSBot Script

public boolean onStart(String[] args) { Bot.getEventManager().addListener(PaintListener.class, this); Frame myframe = new Frame("Input"); myframe.setVisible(true); return true; }

public void onFinish() { }

public int loop() { return 1000; } }

What you see above is the most simple script you will ever see in Rsbot. Every script will have the minimum of these methods^. Lets review what each method does:

onStart(): this method is called when the script begins. It returns a boolean (true or false statement), which tells the script to either continue (if true) or to halt (if false).

loop(): this is where the scripting code will go. loop() is called repeatedly. it repeats, and repeats, and repeats. This is how a script works. for example: if you want your script to walk to cut some logs and walk to the bank, your loop() method will look like this:

loop(){

if(I have cut logs)

then(walk to the bank)

if(I have not cut logs)

then(cut some logs)

return 100;

}

the return statement is a number of milliseconds you want your program to wait inbetween repetitions. if you say "return 1000;" at the

Page 3: How to Make an RSBot Script

end of the loop(), then your script will execute your code,wait 1000 milliseconds, and then restart itself from the top.

onRepaint(Graphics g): This is a method that allows you to draw on the runescape screen. this method is called a million times a second. With this method, you can show the user progress reports. for example, if you want to show the user how many fish have been caught, you can have this be drawn with the onRepaint(Graphics g) method.

get() methods: these are methods like getVersion(), getAuthor(), etc. these simply return a string or double value, which corresponds to the name. for example, in getAuthor() I will say "return 'Ben B' " because I am the author. Step 2 – GUI Building Alright, so the first thing you ever will want to do in your script is get input from your user. Woah woah woah, what is input, you say? input is when you ask the user for information or specifications. and example of input in Rsbot would be asking "What kind of logs do you want to fletch?". this inquiry is followed by a text box, such as the one below:

���Rsbot uses the Java programming language, which means we need to use java to get input from the user of your script. The way java does this is through a GUI (Generic User Interface). Sounds high-tech, ya? Well, it isn't. Infact, it's so bloody simple I don't even know why I bother explaining it. Java is an application programming language, so it uses windows to display things. a window is anything, like the browser you are using to read this tutorial, or microsoft word. Everything on a computer is in some kind of window. A window in java is also called a frame, and this is where the code starts. Whenever you make a GUI, you will put the GUI code in the onStart() method. Why? Because the onStart() method is called when the script begins.You wouldn't want to ask the user for input after the script is done (duh!), so you ask for it when it starts (duh!).There are

Page 4: How to Make an RSBot Script

two ways to make windows and objects in java. there is swing and there is awt. Rsbot doesn't support swing, and I don't care to explain why, so let's stick to the older, but still equally powerful awt set of classes. To create an awt frame, use this code:

Frame myframe = new Frame("ben's GUI");

Woah woah woah,hold up, so what exactly is happening? simply what you are doing here is telling the computer "hey mate, make a window name myframe which has a title bar that says 'ben's GUI' on it". This is a powerful command, but it only makes an invisible frame. To make it visible, say:

myframe.setVisible(true);

You can also set this command to "false" to make the frame invisible again, but only a fag would do that. replace "myframe" with whatever you named your frame in the first command I showed you^. frame sizes need to be set, or else it will be super small. so, lets say:

myframe.setSize(250,250);

sizes are set by number of pixels, so in this command^ you are telling the computer to set the frame size to 250 pixels wide by 250 pixels downward (lengthwise). Alrighty! We have made a boring, stupid, lame frame! let's put some stuff in it! Frames are spaces where you can put components. a component can be anything, like the textfield at the top of this tutorial^. it can be a label which just displays text, or it can be various kinds of buttons. It can be lots of things! The only things I use on GUIs for Rsbot scripting are buttons and text. Let's make two input boxws, called TextFields:

TextField input1 = new TextField();

TextField input2 = new TextField();

I made two TextFields, but they are invisible. A component becomes visible when you attach it to the frame. Lets do that:

myframe.add(input1);

Page 5: How to Make an RSBot Script

myframe.add(input2);

This is great, but it's not great-looking. The components are smashed together. wouldn't it be great to have a great-looking GUI where components are spaced and good looking? java arranges things on frames using layout managers. there are many kinds of layout managers, but let's stick to the most simple of them all, the FlowLayout. To set the Frame's layout manager, use this code:

myframe.setLayout(new FlowLayout(FlowLayout.CENTER,30,10));

FlowLayout simply aligns components and displays them one after another. That is what the first argument does. The next two arguments (which are 30 and 10) say how much horizontal spacing and how much vertical spacing there should be between components. Yeah, it's pretty basic, but better looking that no layout at all. Remember, you can also say:

FlowLayout lm = new FlowLayout(FlowLayout.CENTER,30,10)); myframe.setLayout(lm);

these commands do the same thing. AND, remember you have options: you can say FlowLayout.LEFT or FlowLayout.RIGHT.

The next things you want are Labels because if you don't have these, how will the user know what to put into your textfields?. Labels display text. to make one of these use this code:

Label lb1 = new Label("Logs type");

Label lb2 = new Label("Bow type");

And always remember to add them to the frame!

The last component you need is a button. Let's create a button!:

Button button = new Button("Begin Script");

Just like the Label, the Button takes a string argument saying what text to display. And always remember to add them to the frame!

Page 6: How to Make an RSBot Script

A helpful thing is to have the "x" button on a frame close the frame when it is clicked. to do this, use this code:

myframe.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ myframe.setVisible(false); }});

Okay! HOld up! What does this do? This code tells the computer "hey mate, add a window listener to your ear to set the frame invisible when the window-closing button is clicked". You don't really ned to understand this. just memorize it.

There is one last thing to do. You need to make the button close the window and start the rsbot script when clicked.

Please take a look at my code:

public boolean onStart(String[] args) { Bot.getEventManager().addListener(PaintListener.class, this); final Frame myframe = new Frame("User Input"); myframe.setLayout(new FlowLayout(FlowLayout.CENTER,30,10)); myframe.setSize(175,175); myframe.setResizable(false); myframe.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ myframe.setVisible(false); }}); Label lb1 = new Label("Logs type"); Label lb2 = new Label("Bow type"); final TextField input1 = new TextField(); final TextField input2 = new TextField(); Button button = new Button("Begin Script"); button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){logtype = input1.getText();bowtype = input2.getText();myframe.setVisible(false);}}); myframe.add(input1);myframe.add(lb1); myframe.add(input2);myframe.add(lb2); myframe.add(button); myframe.setVisible(true); return true; }

Okay, so the text is red tells the computer to add a listener to the button component named "button".. a listener is a "computer thing" that "listens" or waits for something to happen. There are many kinds of listeners: key event listeners,mouse event listeners, etc. However, all you should worry about are actionlisteners which listen for clicks. within the actionPerformed(ActionEven event) method is where you put the code for what you want to happen when the button is clicked. What I told it to do is to close the frame and record the log type and

Page 7: How to Make an RSBot Script

bow type in String variables. You can then use these variables to make your script make any kind of bow you want ( magic, yew, maple, short, long).

Pretty boring, huh? Well that's scripting for ya.

Theres one extra thing I want to share with you. That is, if you want the script to wait until the user has submitted input to you before the script starts, make a global boolean variable and declare it false. Then, in the actionPerformed(actionEvent event) method declare it to be true. Then, in your loop() method you can say if(boolean variable = true), then(do the script).This way, when the "begin script" button is clicked, the script will start. check out what I did:

boolean maybegin = false; public boolean onStart(String[] args) { Bot.getEventManager().addListener(PaintListener.class, this); final Frame myframe = new Frame("User Input"); myframe.setLayout(new FlowLayout(FlowLayout.CENTER,30,10)); myframe.setSize(175,175); myframe.setResizable(false); myframe.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ myframe.setVisible(false); }}); Label lb1 = new Label("Logs type"); Label lb2 = new Label("Bow type"); final TextField input1 = new TextField(); final TextField input2 = new TextField(); Button button = new Button("Begin Script"); button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){logtype = input1.getText();bowtype = input2.getText();myframe.setVisible(false); maybegin = true;}}); myframe.add(input1);myframe.add(lb1); myframe.add(input2);myframe.add(lb2); myframe.add(button); myframe.setVisible(true); return true; }

public int loop() { if(maybegin) { //all my code. } return 1000; }

Here is my full GUI-starter-model script (modify it as you wish!):

import java.awt.*;

import com.speljohan.rsbot.bot.Bot; import

Page 8: How to Make an RSBot Script

com.speljohan.rsbot.event.listeners.PaintListener; import com.speljohan.rsbot.script.Script; import com.speljohan.rsbot.script.wrappers.*; import java.awt.*; import java.awt.event.*;

public class testscript extends Script implements PaintListener { String logtype; String bowtype; public void onRepaint(Graphics g) { Bot.getEventManager().addListener(PaintListener.class, this); } public double getVersion() { return 1.0; } public String getScriptCategory() { return "Ben's Scripts"; } public String getName() { return "Test Script"; } public String getAuthor() { return "Ben B (Hunterbdb)"; } boolean maybegin = false; public boolean onStart(String[] args) { Bot.getEventManager().addListener(PaintListener.class, this); final Frame myframe = new Frame("User Input"); myframe.setLayout(new FlowLayout(FlowLayout.CENTER,30,10)); myframe.setSize(175,175); myframe.setResizable(false); myframe.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent we){ myframe.setVisible(false); }}); Label lb1 = new Label("Logs type"); Label lb2 = new Label("Bow type"); final TextField input1 = new TextField(); final TextField input2 = new TextField(); Button button = new Button("Begin Script"); button.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent event){logtype = input1.getText();bowtype = input2.getText();myframe.setVisible(false); maybegin = true;}}); myframe.add(input1);myframe.add(lb1); myframe.add(input2);myframe.add(lb2); myframe.add(button); myframe.setVisible(true); return true; } public void onFinish() { } public int loop() { if(maybegin) { //all my code. } return 1000; } }

You should see something like this if you use the above script^:

Page 9: How to Make an RSBot Script

Alright, on to more boring stuff! Step 3 – Player Movement In just about any script, you are going to want to move you character. Even in something that is not at first sight a movement-intensive script like a fletching script will at least need to move your character for antiban purposes. Luckily, rsBot provides a means of easily moving your character with programming. Let's take a look.

In runescape, everything has a number value. every scimitar, fish, and animation has a number specific to that item. In runescape, a player's location is recorded by which square tile he is standing on (the ground in rs is made of tiles, by the way). when you click to move

Page 10: How to Make an RSBot Script

your character, you are clicking on a tile location, and character walks to that tile. The first ability you need to have to move your character is the ability to see which tiles your character is on and which ones he needs to move to. To do this, please follow these instructions:

1. Open your rsbot client and log in.

2. In the upper-center of the rsbot client you will see a "debug" tab. Please click this and you will see a form like this:

3. Please click Player position:

Page 11: How to Make an RSBot Script

4. You will now see green text in the upper left hand corner of your runescape window. This is your x and y position in runescape. Try walking around and see how it changes!

Okay, now that you know this, you can learn to move your character with code. To move your character, you need to do two things: make an array (a list) of Runescape tiles that you want to travel to in sequential order, and call a method to make your character walk. Runescape tiles are called RSTiles in rsbot.

To make an array of tiles, try this code:

RSTile[] path = new RSTile[]{new RSTile(1215,1418), new

Page 12: How to Make an RSBot Script

RSTile(1215,1425)};

you can put as many tiles in this array as you want. However, if they are inaccessible through your path, there will be problems. You can also make and array of Runescape tiles like this:

RSTile[] path = new RSTile[2];

path[0] = new RSTile(1215,1418);

path[1] = new RSTile(1215,1425);

The first method of making arrays is much much faster^

Now that you have your array, let's walk! Try this code:

walkPathMM(path,20);

Woah woah woah, HOLD UP! what does the "20" in there mean? this method takes an array of RSTiles and makes your character walk them in sequential order. The second argument is the "maximum distance" that you want your character to travel. Make sure that your character doesn't go too far or he will get lost!

This is all great, but if you walk to same path all the time you're asking to be banned. So why not randomize your path up! Try this code:

walkPathMM(randomizePath(path,2,2),34);

What the hell! there are 3 arguments to randomizePath(RSTile[],int,int)! Chill out, it's not that bad. take a deep breath. have a smoke. The second argument to this method is the maximum "x" deviation (or distance from the original RSTile "x" values) you want to randomize. the third argument is the maximum "y" deviation. for example, in my code, the tile range for path[0] is (1213 to 1217, 1416 to 1420). Why? because I set the x and y maximum deviation in the randomizePath method to 2,2.

Page 13: How to Make an RSBot Script

well, that's about as detailed as I possibly can get on Player movement. I think the next topic will be a bit more fun than this one, but still boring. Step 4 - Menus, Objects, and Items Okay, so far we have learned how to make boring looking GUIs, the model of an rsbot script, and how to make our script move our character. BOOOOOOOOOOOOOORING! What's next?

Now we are going to learn the most important thing: clicking on stuff. Thankfulling Speljohan has made this very easy for us.

Like I said earlier in this tutorial, everything has a number in runescape. Also, every item and building and character is an object (as are all things in java). Objects can be things like bank booths as well. To click on an object (such as a bank booth), you will need to get the number for it. Do the same steps to get numbers of objects as you did with getting RSTile numbers, except instead of selecting to debug "Player position", please debug "Objects" from the debug tab on the RSbot client.

You should see something like this:

Page 14: How to Make an RSBot Script

Look! everything has numbers! That's right. the number above an object is the object's number (duh!)! as you can see, the bank booth is integer 36831.

There are two steps to clicking on an object: find and object, click on the object.

there are a few methods for finding objects:

1. public RSObject getObjectAt(int x, int y)

This method does the same thing as the 4th method below, except it uses integer locations instead of an RSTile.

2. public RSObject findObject(int)

Page 15: How to Make an RSBot Script

This method finds the nearest object with the spcified number.

3. public RSObject findObject(int range, int id)

This method finds the nearest object with the specified number and within the specified maximum distance from your player.

4. public RSObject getObjectAt(RSTile t)

This method gets the object which is at the specified RSTile.

You will use the 2nd method the most for its simplicity. Now, if I want my script in the picture above to use the bank booth , I will use this code:

int id1 = 36831;

RSObject bankbooth = findObject(id1);

atObject(bankbooth,"Use-Quickly");

Wait wait wait! what's this atObject method? This method right-clicks on the object that you found and selects the option specified in the 2nd argument from the menu that appears. Make sure that the 2nd argument String is EXACTLY what you want it to be, or the method will screw up. If the option doesn't exist, it will cause the method to screw up as well.

Items

An RSItem is an object in your inventory. To view the numbers for these in the rsbot cient, debug "inventory".

You should see numbers in your inventory like below:

Page 16: How to Make an RSBot Script

To click on these inventory items, use this code:

atInventoryItem(379,"eat");

This method will right click on my lobster and then select the "eat" option. Yum Yum! You can also use short-cut methods to use an item:

1. public boolean useItem(RSItem item, RSItem targetItem)

This method uses an item on another item.

2. public boolean useItem(RSItem item, RSObject targetObject)

This method uses an item on an object.

Page 17: How to Make an RSBot Script

To create an RSItem object for use in these methods, say:

RSItem item = getInventoryItemByID(1319);

This method^ just made an RSItem for the rune 2h sword in my inventory.

Menus

Okay, so what is a menu? I don't know why I have to explain this, but this tutorial is for the most noobed boober noobers. A menu is any kind of form that pops up in runescape. It can be when you right-click a player, or when you right-click an item, etc. Check out the sweet bank booth menu we clicked in one of our code pieces:

WAHOH! PRETTY COOL MENU, YA?

We need to know how to interact with menus specificly in case we come across a problem where the object and item methods don't do it for us. This is a very rare extreme-case-scenario, but it's something I think is important.

One such scenario is withdrawing from the bank. the bank methods are BROKEN, last time I checked. So, we need to withdraw on our own. I have formulatedsome code for all of you:

Page 18: How to Make an RSBot Script

Okay, so let's talk about this code. Once I have opened the bank, we say bank.searchItem(String) which searches for the specified item in your inventory. then, I use the clickMouse method to click on the point where the object will be after searching for it, which is at the upper left corner. then, i use the clickMouse method again to make a selection.

HOLD UP! there is an easier way to make the selection after right clicking on the item in the inventory. the way to do this is to use the atMenu method.

What I can do is say:

atMenu("Withdraw 100");

Replace the text in the quotes with the appropriate command and the method will select that option from the menu you have open.

Pretty easy, huh?

Well, thats it for objects, items, and menus! I hope I was as least confusing as possible!

Tabs

Tabs are the buttons that let you switch from your inventory, to your prayer screen, to your magic dialog in runescape. to open these tabs in rsbot, use this code:

Page 19: How to Make an RSBot Script

openTab(INTERFACE_TAB_PRAYER);

This code uses global variables. you can open any tab using one of these pre-set variables:

public static final int INTERFACE_TAB_CLAN = 589; public static final int INTERFACE_TAB_IGNORE = 551; public static final int INTERFACE_TAB_FRIENDS = 550; public static final int INTERFACE_TAB_MUSIC = 187; public static final int INTERFACE_TAB_OPTIONS = 261; public static final int INTERFACE_TAB_EMOTES = 464; public static final int INTERFACE_TAB_MAGIC = 192; public static final int INTERFACE_TAB_QUESTS = 274; public static final int INTERFACE_TAB_STATS = 320; public static final int INTERFACE_TAB_COMBAT = 92; public static final int INTERFACE_TAB_EQUIPMENT = 387; public static final int INTERFACE_TAB_PRAYER = 271; public static final int INTERFACE_TAB_LOGOUT = 182;

public static final int INTERFACE_GUI = 548; public static final int INTERFACE_CHAT_BOX = 137; public static final int INTERFACE_TRADE = 279; public static final int INTERFACE_GAME_SCREEN = 549; public static final int INTERFACE_LEVELUP = 740;

You don't HAVE to use the variables. You could very well say openTab(271) to open the prayer tab. The variables just make it easier. You don't have to memorize all those numbers!

Banking

There are a few methods for banking. These ONLY WORK WHEN THE BANK SCREEN IS OPEN! The implemented variable "bank" is used to access bank commands. You can use its methods below:

bank.depositAllExcept(379);

With the bank screen open, this method deposits all items except the items with the specified id number. an array of item ids can be used with this method as well.

Page 20: How to Make an RSBot Script

bank.depositAll();

With the bank screen open, this method deposits all items into the bank. Step 5 - NPCS and Player interaction Let's fly through this as quick as possible. in many scripts you will want to interact with NPCs and other players. Working with NPCs and Players is just like working with objects. The steps are the same (find an object, click on it), but the methods are different. let's take a look at these methods:

1. public RSPlayer getNearestPlayerByLevel(int level)

This does exactly what the name says it does.

2. public RSPlayer public RSPlayer getNearestPlayerByName(String name)

This does exactly what the name says it does.

3. public RSNPC getNearestFreeNPCByName(String... names)

This does exactly what the name says it does.

4. public RSNPC getNearestNPCByName(String... names)

This does exactly what the name says it does.

NPCs in are called RSNPCs and Players are called RSPlayers.So, if I want to find and create an RSNPC object, all I need to do is say:

RSNPC dragon = getNearestNPCByName("Green Dragon");

This code above finds a green dragon^, if there is one.

Page 21: How to Make an RSBot Script

If I want to find a player to kill in the wild, all I need to do is say:

RSPlayer player_to_kill = getNearestPlayerByLevel(95);

This code above finds a a lvl 95 player^, if there is one.

To do something with an NPC object or a Player object, use this method:

atNPC(dragon, "Attack");

This code above attacks the dragon NPC I found^. The second argument is the option of the menu you want to click after you right-click on the npc.

To do something with a Player object, use this method :

ClickCharacter(player_to_kill,"Attack");

This code above attacks the lvl 95 player I found^. This method can be used for a RSNPC as well.

Well, those are the basics to player and NPC interation. It isn't hard. In fact, none of this is very hard. Have fun with your new knowledge!

Step 6 - Player Properties There are alot of properties that come with your player. These are: statistics, inventory details, player location, etc. It is helpful to know these things because they can help you write code that makes smart decisions. There is a very useful method, the getMyPlayer() method which returns yourself. Let's see what we can do with this method:

RSPlayer me = getMyPlayer();

This gets your player. you can use it in place of "getMYPlayer()" for the methods below

RSTile mylocation = getMyPlayer().getLocation();

Page 22: How to Make an RSBot Script

This gets your current RSTile.

RSTile mydestination = getMYPlayer().getDestination();

This gets your current destination while moving. it returns null if you aren't moving, so make sure you call it only when moving.

There is a special variable, skills, which is implemented, that allows you to get current skill levels and experience. check it out:

int index = Skills.getStatIndex("Fishing");

int exp = skills.getCurrentSkillExp(index);

This code gets the index, or number for the fishing stat and then gets the current skill experience for it. You can get any level's stat index with the getStatIndex(String) method.

int level = skills.getCurrentSkillLevel(index);

This code, using the stat index from the code above it, gets the current level.

int xptonextlvl = skills.getXPToNextLevel(STAT_FISHING);

this code doesn't require a stat index. Instead, it uses globabl variables. It's function is to get the experience needed to go up to the next level.You can use any of these variables in the method:

public static final int STAT_ATTACK = 0; public static final int STAT_DEFENSE = 1; public static final int STAT_STRENGTH = 2; public static final int STAT_HITPOINTS = 3; public static final int STAT_RANGE = 4; public static final int STAT_PRAYER = 5; public static final int STAT_MAGIC = 6; public static final int STAT_COOKING = 7; public static final int STAT_WOODCUTTING = 8; public static final int STAT_FLETCHING = 9; public static final int STAT_FISHING = 10; public static final int STAT_FIREMAKING = 11; public static final int STAT_CRAFTING = 12; public static final int STAT_SMITHING = 13; public static final int STAT_MINING = 14; public static final int STAT_HERBLORE = 15; public static final int

Page 23: How to Make an RSBot Script

STAT_AGILITY = 16; public static final int STAT_THIEVING = 17; public static final int STAT_SLAYER = 18; public static final int STAT_FARMING = 19; public static final int STAT_RUNECRAFTING = 20; public static final int STAT_HUNTER = 21; public static final int STAT_CONSTRUCTION = 22; public static final int STAT_SUMMONING = 23;

You don't HAVE to use the variables. You could very well say skills.getXPToNextLevel(10) to get the fishing XP to the next level.

Credits

Created by Ben B Converted to PDF Format by Matt7

RSBot.org © – Copyright 2009