You are viewing our Forum Archives. To view or take place in current topics click here.
How to write a RSBot Script --Beginner Level--
Posted:

How to write a RSBot Script --Beginner Level--Posted:

8ight
  • Resident Elite
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Content:
1) RSBot --Beginner--
2) RSBot --Hard--[0%]
3) RSBot --Advanced--[0%]


Tools:
[ Register or Signin to view external links. ]
[ Register or Signin to view external links. ]
[ Register or Signin to view external links. ]


Scripts:
Script Template
//Imports
import java.awt.*;
import java.util.*;
import java.util.List;
import java.util.logging.Level;
import javax.accessibility.*;
import javax.swing.*;
import org.rsbot.bot.Bot;
import org.rsbot.script.*;
import org.rsbot.script.wrappers.*;
import org.rsbot.accessors.*;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.event.listeners.ServerMessageListener;
import org.rsbot.event.events.ServerMessageEvent;
//Description
@ScriptManifest(authors = { "NameHere" }, category = "ScriptCategory", name = "Script Name", version = 1.00, description = "<html><head></head><body>Script description here.</body></html\n")
//Name of script
public class YourScriptName extends Script implements PaintListener {
//Start
public boolean onStart(Map<String, String> args) {
return true;
}
//Loop
public int loop() {
return 100;
}
//Paint
public void onRepaint(Graphics g) {
if(isLoggedIn()) {


}
}
//Finish
public void onFinish() {
log("Your Script Name has finished");
}
}




Step 1:
Run RSBot so that it creates the directory 'RSBot' in your My Documents folder. Open that directory, and then open the Scripts folder. Here is where you create your new script.

Step 2:
Create a new java file called TestScript.java, and open it.

Now this tutorial is the most basic of them all, and the only features it will have are:
    - A simple paint (GUI) with run time
    - Correct start up and personalization
    - Taking screenshots

So lets get started!

All RSBot scripts need to import a few things in order to work. One is the Map class, because the starting method for a script uses it as arguments. The other two are for the GUI: 'Color' and 'Graphics'. After those, there are a few things that come with RSBot that you need to import, for things such as reading server messages, finding objects, interfaces etc.[list]

So this is what we have so far:

import java.awt.Color; //For the GUI
import java.awt.Graphics; //For the GUI
import java.util.Map;
import org.rsbot.event.events.ServerMessageEvent; //To read server messages
import org.rsbot.event.listeners.PaintListener; //For the GUI
import org.rsbot.event.listeners.ServerMessageListener; //To read server messages
import org.rsbot.script.Script; //The main Script template
import org.rsbot.script.ScriptManifest; //The main Script template, used to customize the look in the 'Run Script' dialog box
import org.rsbot.util.ScreenshotUtil; //To take screenshots

Now we change the script Manifest,such as the author and description. Here's how it works:

@ScriptManifest(authors ="YOURNAME", /* The author of the script */
category = "Test Scripts", /* Script category (normally a skill, such as Cooking or Fletching) */
name = "Test Script", /* The name of your script (does not need to match the Class name) */
version = 1.0, /* Current script version*/
description = "<h1>This is our first script!</h1>" /* The script description. You can use HTML in here! */

Now put both of them together it should look like this:
import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;
import org.rsbot.event.events.ServerMessageEvent;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.event.listeners.ServerMessageListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.util.ScreenshotUtil;

@ScriptManifest(authors ="Davidi2", category = "Test Scripts", name = "Test Script", version = 1.0,  description = "<h1>This is our first script!</h1>"


Now we can move on to some real scripting! After we have finished our imports and manifest, we need to start the class. here is what almost all of your classes should look like:

public class TestScript extends Script implements PaintListener, ServerMessageListener {
public long startTime;

The class name NEEDS to match the name you gave your Java file!

Step 3:
Now there are a few methods you need to add to your script for it to work.
I will explain each method here, and then give you the code.
    - onStart() - This is the most important of all the methods. It is called when someone 'runs' your script from the 'Start Script' dialog. This is where you do any initialization for your script.
    - onFinish() - Called when you stop the script
    - loop() - Called with each loop of the script, this is where you do the processing
    - logOut() - Called when you want to stop the script and log the character out
    - getMouseSpeed() - A protected method that gives the client your mouse speed
    - onRepaint() - The loop for the GUI


Now here is all those methods in action:

public boolean onStart(Map<String, String>args) {
startTime = System.currentTimeMillis();
return true;
}

public void onFinish(){
log("Thanks for using this Test Script!");
}

@Override
public int loop() {
return color=pink]1[/color];
[color=green}[/color]

public void logOut() {
ScreenshotUtil.takeScreenshot(/color][color=blue]true);
moveMouse(754, 10, color=pink]10[/color], 10);
clickMouse(true);
moveMouse(642, 378, 20, 15);
clickMouse(true);
wait[color=green([/color]random(2000, 3000));
stopScript();
}

@Override
protected int getMouseSpeed() {
return 6;
}

public void onRepaint(Graphics g) {
if(!isLoggedIn())
return;
long runTime=0, seconds=0, minutes=0, hours=0, expGained=0, levelsGained=0;
runTime = System.currentTimeMillis() - startTime;
seconds = runTime / 1000;
if (seconds >= 60) {
minutes = seconds / 60;
seconds -= (minutes * 60);
}

if (minutes >= 60 [color=green) {[/color]
hours = minutes / 60;
minutes -= (hours * [color=pink60[/color]);
}

g.setColor(new color=blue]Color[/color](0, 0, 0[/color, [color=pink]50) );
g.fillRoundRect([color=pink3[/color], 180, 155, 130, 5, 5);
g.setColor( Color.WHITE );
g.drawString("Run time: " + sH + ":" + sM + ":" + sS, 12, 200);
}


@Override
public void serverMessageRecieved(ServerMessageEvent arg0) {
String serverString = arg0.getMessage();
if (serverString.contains("<col=ffff00>System update in")) {
log("There will be a system update soon, so we logged out"[color=green);[/color]
logout();
}
}



Now here's our entire script:

import java.awt.Color;
import java.awt.Graphics;
import java.util.Map;
import org.rsbot.event.events.ServerMessageEvent;
import org.rsbot.event.listeners.PaintListener;
import org.rsbot.event.listeners.ServerMessageListener;
import org.rsbot.script.Script;
import org.rsbot.script.ScriptManifest;
import org.rsbot.util.ScreenshotUtil;

@ScriptManifest(authors ="Davidi2", category = "Test Scripts", name = "Test Script", version = 1.0,  description = "<h1>This is our first script!</h1>");

public class TestScript extends Script implements PaintListener, ServerMessageListener {

     public long startTime;

     public boolean onStart(Map<String, String>args) {
          startTime = System.currentTimeMillis();
          return true;
     }
     
     public void onFinish(){
          log("Thanks for using this Test Script!");
     }
     
     @Override
     public int loop() {
          return 1;
     }
     
     public void logOut() {
          ScreenshotUtil.takeScreenshot(true);
          moveMouse(754, 10, 10, 10);
          clickMouse(true);
          moveMouse(642, 378, 20, 15);
          clickMouse(true);
          wait(random(2000, 3000));
          stopScript();
     }
     
     @Override
     protected int getMouseSpeed() {
           return 6;
     }

     public void onRepaint(Graphics g) {
          if(!isLoggedIn())
               return;
          long runTime=0, seconds=0, minutes=0, hours=0;
          runTime = System.currentTimeMillis() - startTime;
          seconds = runTime / 1000;
          if (seconds >= 60) {
               minutes = seconds / 60;
               seconds -= (minutes * 60);
          }
          if (minutes >= 60 ) {
               hours = minutes / 60;
               minutes -= (hours * 60);
          }
          g.setColor(new Color(0, 0, 0, 50) );
          g.fillRoundRect(3, 180, 155, 130, 5, 5);
          g.setColor( Color.WHITE );
          g.drawString("Run time: " + hours + ":" + minutes + ":" + seconds, 12, 200);
     }

      @Override
     public void serverMessageRecieved(ServerMessageEvent arg0) {
          String serverString = arg0.getMessage();
          if (serverString.contains("<col=ffff00>System update in")) {
               log("There will be a system update soon, so we logged out");
               logout();
          }
     }
}


Now save that and close it. Go up one folder, and run 'Compile-Scripts'.
Run RSBot.jar and click Run Script. Scroll down until you find the folder 'Test Scripts', and click on 'Test Script'. Run it (make sure you log in first).


Please thank topic, Rep is optional. Hope this tutorial helped
[ Register or Signin to view external links. ]
- 8ight
Davidi2 originally done this on Moparscape. These tutorials that I provide will be compiled into one giving people the necessary link & help on how to make a RSBot script.


Last edited by 8ight ; edited 13 times in total

The following 7 users thanked 8ight for this useful post:

Cez (10-17-2011), No1 (10-17-2011), iJarH3ad (10-16-2011), TTG-JasonV (10-15-2011), carnagexrsps (10-14-2011), Old (10-14-2011), TDK (10-14-2011)
#2. Posted:
Wubstep
  • TTG Addict
Status: Offline
Joined: Jul 17, 201013Year Member
Posts: 2,703
Reputation Power: 116
Status: Offline
Joined: Jul 17, 201013Year Member
Posts: 2,703
Reputation Power: 116
I have no idea what any of that means or does but you obviously put alot of effort in to it and in my opinion it deserves a sticky
#3. Posted:
8ight
  • Resident Elite
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
-UKF- wrote I have no idea what any of that means or does but you obviously put alot of effort in to it and in my opinion it deserves a sticky


Thanks man took ages... :L sat for 2 hours with beer keeping me going :L
#4. Posted:
Fashion
  • TTG Addict
Status: Offline
Joined: Jul 07, 201112Year Member
Posts: 2,421
Reputation Power: 103
Status: Offline
Joined: Jul 07, 201112Year Member
Posts: 2,421
Reputation Power: 103
Amazing tut i already know how but sick helps teh noobies
#5. Posted:
8ight
  • Resident Elite
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Weapons wrote Amazing tut i already know how but sick helps teh noobies
#

Thanks mate Hopefully gets a sticky & helps users out
#6. Posted:
TDK
  • Ultimate Gifter
Status: Offline
Joined: Nov 14, 201013Year Member
Posts: 3,058
Reputation Power: 162
Status: Offline
Joined: Nov 14, 201013Year Member
Posts: 3,058
Reputation Power: 162
amazing tutorial bro keep up the awesome work and i hope they make this a Sticky for you it derserves to be.

thanked and repped
#7. Posted:
8ight
  • Resident Elite
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
The_Gold_Legend wrote amazing tutorial bro keep up the awesome work and i hope they make this a Sticky for you it derserves to be.

thanked and repped


Thanks man Appreciate it
#8. Posted:
Fashion
  • TTG Addict
Status: Offline
Joined: Jul 07, 201112Year Member
Posts: 2,421
Reputation Power: 103
Status: Offline
Joined: Jul 07, 201112Year Member
Posts: 2,421
Reputation Power: 103
i have a Hunting Script
a Agility Script
and Combat Script and A Hunting script all ready
#9. Posted:
8ight
  • Resident Elite
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Status: Offline
Joined: Aug 11, 201112Year Member
Posts: 264
Reputation Power: 12
Weapons wrote i have a Hunting Script
a Agility Script
and Combat Script and A Hunting script all ready



Niceee Im making all mine from scratch
#10. Posted:
Extazc
  • TTG Senior
Status: Offline
Joined: Aug 17, 201112Year Member
Posts: 1,444
Reputation Power: 75
Status: Offline
Joined: Aug 17, 201112Year Member
Posts: 1,444
Reputation Power: 75
nice tut and wouldnt this go in the programming section and also mention what language it is(c#)
Jump to:
You are viewing our Forum Archives. To view or take place in current topics click here.