• Log in or sign up for an account here: https://www.sythe.org/login

Reported Post by The Rising Tied

Status
Not open for further replies.

The Rising Tied

Apprentice

Banned
Joined
Jan 24, 2009
Sythe Gold
0
Messages
755
Location
In a cinema near you!
The Rising Tied has reported a post.

Reason:
My first reported post, not sure if I did it right. :)

Reason:
Post: guide how to whrite a rsbot script
Forum: RSBot Help
Assigned Moderators: Pen

Posted by: saesi
Original Content:
announcement: this is not written by me.

credits go to rsbot.org

What to start with

Ok, since your not "pro" scripters yet, there is a "skeleton" to follow. It already has all of the methods needed to run an RSBot script. What you have to do is fill in what you want the script to do inside of the methods. Here is a basic skeleton of a script (You will want to come get this each time you script).

Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;

public class Blank extends Script{	
	
	/*
	 * This is where you place all your variables
	 */
	
	
	public String getName(){
		return "Blank";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Scripts";
	}
	
	public double getVersion(){
		return 1.0;
	}
    
    public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	/*
    	 * This is the main loop that we discussed earlier
    	 * This method will be used over and over.
    	 * The concept behind this method is that it will start at 
    	 * the beginning. When you "return" any number (except -1)
    	 * it will check for randoms, do any antiban you may have
    	 * and start at the beginning of this method again.
    	 */
    	return -1;
    }
}


Understanding each method


Now I know many of you reading through that code above are getting pretty dang confused, so let me break it down for you.

Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;


These are the "imports"



Code:
public class Blank extends Script{

this is your title line. The name of your script will go where the word "Blank" is (this must have no spaces and just don't use numbers). Where it says "extends Script", do not worry about this. Extending is a programming concept you may not understand easily at this stage in learning. Just know that "extends Script" simply tells RSBot that it is a RSBot script and can be run by RSBot.

Code:
public String getName(){
		return "Blank";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Scripts";
	}
	
	public double getVersion(){
		return 1.0;
	}


These are all methods used to determine attributes of your script. Obviously, the name of your script goes in "getName", your personal name goes in "getAuthor". If you have used RSBot, when you click "Run" a window pops up with all these folders on the left and a description on the right. getScriptCategory determines the name of the folder in these categories. getVersion simply returns the version of the script.

These are all used for the description box I mentioned above. Just fill these in and forget them, as the really don't affect what happens in the script. When I fill these in, you may understand more.

Code:
public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	/*
    	 * This is the main loop that we discussed earlier
    	 * This method will be used over and over.
    	 * The concept behind this method is that it will start at 
    	 * the beginning. When you "return" any number (except -1)
    	 * it will check for randoms, do any antiban you may have
    	 * and start at the beginning of this method again.
    	 */
    	return -1;
    }


These are all the methods that will execute your code that you write for your character to use. In theory, you really need to focus on these the most, as they will be controlling the character. Brief descriptions have been added so your memory will be refreshed as to what they do.



Let's begin

Ok in this example, we are going to write a simple autominer for varrock. We are going to mine copper. We are first going to get the variable's we need to write this script. After this, we will begin constructing what we want the program to do. Please fill in the variables below, and start with this outline. It will be saved as CopperMiner.java, since that is what is on the title line where Blank used to be.


Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;

public class CopperMiner extends Script{	
	
	/*
	 * This is where you place all your variables
	 */
	
	
	public String getName(){
		return "Copper Miner";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Mining";
	}
	
	public double getVersion(){
		return 1.0;
	}
	
    public String getScriptDescription() {
    	String html = "";
        return(html);
    }
    
    public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	/*
    	 * This is the main loop that we discussed earlier
    	 * This method will be used over and over.
    	 * The concept behind this method is that it will start at 
    	 * the beginning. When you "return" any number (except -1)
    	 * it will check for randoms, do any antiban you may have
    	 * and start at the beginning of this method again.
    	 */
    	return -1;
    }
}



Gathering the variables

Ok, obviously the first variable we are going to need to gather is the variable of the rock. How we do this is we debug "Objects" on RSBot and we look at the number above the rock. The picture will explain.

2me068.jpg


4kidj6.jpg



Now that we have our number, 11961, we will make that an "integer variable" and place it where the skeleton says "place variables here" like so.

Code:
public class CopperMiner extends Script{	
	
	public int copperRock = 11961;// <---[COLOR="Red"] I added this.[/COLOR]
	
	public String getName(){
		return "Copper Miner";
	}

//etc.


Now what we are going to do, is get the animation of us when we mine rocks.

When your character is performing any type of emote or motion, he does an "animation". These animations are specific to the motion they are performing. Therefore, when we mine copper, our player performs an animation. You can get this like so.

rhnxnl.jpg


1z1r3h5.jpg


So now we add that to our script

Code:
public class CopperMiner extends Script{	
	
	public int copperRock = 11961;
       public int miningAnimation = 625; // <---- [COLOR="Red"]I added this.[/COLOR]
	
	public String getName(){
		return "Copper Miner";
	}

//etc.


After we accomplish these two tasks, we are going to get the ID of our pickaxe, because we need to know what not to drop when we unload our inventory. Here is how

o9lrgz.jpg




Now just update our variables really quick.
Code:
public class CopperMiner extends Script{	
	
	public int copperRock = 11961;
       public int miningAnimation = 625; 
       public int pickaxe = 1265; // <---- [COLOR="Red"]I added this. Add your number there[/COLOR]
	
	public String getName(){
		return "Copper Miner";
	}

//etc.



Writing the loop

Phew! Now my hardest part is done! (uploading images). However, your hardest part remains. This is writing the loop.

Make this a little side-note, anytime you "return" a method, it automatically exits the method, not looking at anymore code past that "return".

Now let's examine our loop. ( I deleted the comments out ).

Code:
public int loop() {
    	return -1;
    }

This is a very empty loop. Anytime you return -1 in the loop it will stop the script. Therefore, the script would take on look at this loop and stop the script. Any number above -1 (including 0) will be a "waiting" time. If you return 300, the script will "wait" 300 milliseconds before looking for randoms, doing antiban, and then starting the loop over again. (just to let you know, the are 1000 milliseconds in 1 second). So, let's clear that line and begin.

First of all, we don't want the script to do anything if our character is walking. We just want it to "wait" until we are done walking. So we will add this to the loop.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	return 300; // Just put this here so the script won't stop.
    }

getMyPlayer() is a method that returns all sorts of things. if you type "getMyPlayer()." (with a period at the end) and then press ctrl + spacebar, most IDE's will come up with a list of things you can use.

34sf8lv.jpg


Now, we also want it to "wait" if our player is already mining a rock. How do we know if he is mining a rock? By his animation of course. And oh yes! We have already found what his animation is if he is mining. So we add this to our code.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() [B][COLOR="Red"]==[/COLOR][/B] miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	return 300;
    }


The "==" operation

This is where many beginners get confused. "=" does not mean the literal defination of equals. "=" is used to set a value, like when we declared our variables.

Code:
public int copperRock [COLOR="Red"][B]=[/B] [/COLOR]11961;

You must use "==" when you are saying the literal definition of "equals". I know it's really confusing, just remember, when you are checking if something equals another thing, you use "==". If you are setting the value of a variable, you use "=";


Back to the loop()

if you did not catch the comment at the end of the last piece of code, go back to the example and look at it.

Now, I know you are all saying "Let's mine a rock!". Wrong! RSBot only does what we tell it to in what order we tell it to. So first, we must make sure the inventory is not full, because if we tell it to mine a rock before checking the inventory, it will always try to mine a rock even if the inventory is full. Make sense? We do not want to drop our pickaxe, so we add this.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() == miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	
    	if(isInventoryFull()){
    		dropAllExcept(pickaxe);
    		return 800;
    	}
    	
    	//if we have gotten here, this means all of the above comments,
    	//and our inventory is not full. This means it's perfect to
    	//mine a rock.
    	
    	return 300;
    }

This piece of code will simply drop our inventory if we have a full inventory.

Now the exciting part (lol). WE GET TO MINE A ROCK. Alright, now if you have gotten this far, you are hanging in there. Remember how we debugged "Objects" to get our rock ID? Ok, well now we have to click the rock (also an object) with the command "Mine"

1t78s9.jpg


So we will use the following code to find and mine a rock.

Code:
       RSObject rock = findObject(copperRock);
    	if(rock == null) return 800;
    	
    	atObject(rock, "Mine");

the first line is used to look for the object. We are simply declaring a variable, just like we would an int.

Code:
RSObject // <-- Type, this is where we had int earlier
rock // <-- our special name for this variable
findObject(copperRock); // <-- what the value will be.


If we cannot find a rock with that ID, the rock will be a value called "null". This means nothing, or we could not find one. This is where most scripters go wrong. They never handle if "rock" does not find the object, therefore, they get a Null Pointer exception. See the word Null? If you ever get this error, this means you didn't handle if something was "null". So in short, if RSBot can't find this object, we will wait 800 milliseconds and try again.

Lastly, we will use the method atObject(); This tells RSbot to go click the object, and then click the word that you put in the second arguement. Therefore our loop looks like this.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() == miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	
    	if(isInventoryFull()){
    		dropAllExcept(pickaxe);
    		return 800;
    	}
    	
    	//if we have gotten here, this means all of the above comments,
    	//and our inventory is not full. This means it's perfect to
    	//mine a rock.
    	
    	RSObject rock = findObject(copperRock);
    	if(rock == null) return 800;
    	
    	atObject(rock, "Mine");
    	
    	return 300;
    }


Conclusion

I know that writing scripts is complicated. This was a simple script, and showing you how to write it made my head hurt. All I can tell you is stick with it. My style of teaching you is different than most others. I do not teach you about "if" statements, I expect you to make that connection and understand what it means. I expect you to mess around with things like "getMyPlayer()." and see what RSbot can tell you about your player.

I tried to make this as simple and straightforward as possible. I promise you that once you start scripting more and have more practice, you will understand the harder parts of it.

If this does make sense to you, try following the same steps to make an auto woodcutter, or an auto fisher. You may have to handle a few more things that are different between mining and fishing (for example if you have bait).

If this makes no sense to you, do not be discouraged. Simply look at Mining, Woodcutting, and Fishing scripts and see what they did. Examine their loop, their onStart, and their onFinish.


Like I said, I did not cover tons of material, like letting the script find the pickaxe on it's own and storing it to a variable, handling if we don't have a pickaxe to stop the script (if you want to add this, use the command if(inventoryContains())), and tons of other stuff.

Don't worry about login and randoms, the script will find those any time you 'return' the loop.

All I can say is good luck to you my friend. Read more tutorials, read this tutorial more than once, examine scripts, search the file YourRSBotFolder.bot.com.speljohan.rsbot.script.methods.java, and have fun with it.

Finished script:

Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;

public class CopperMiner extends Script{	
	
	public int copperRock = 11961;
    public int miningAnimation = 625; 
    public int pickaxe = 1265;
	
	public String getName(){
		return "Copper Miner";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Mining";
	}
	
	public double getVersion(){
		return 1.0;
	}
	
    public String getScriptDescription() {
    	String html = "";
        return(html);
    }
    
    public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() == miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	
    	if(isInventoryFull()){
    		dropAllExcept(pickaxe);
    		return 800;
    	}
    	
    	//if we have gotten here, this means all of the above comments,
    	//and our inventory is not full. This means it's perfect to
    	//mine a rock.
    	
    	RSObject rock = findObject(copperRock);
    	if(rock == null) return 800;
    	
    	atObject(rock, "Mine");    	
    	return 300;
    }
}
[/CENTER]
Post: guide how to whrite a rsbot script
Forum: RSBot Help
Assigned Moderators: SuF

Posted by: saesi
Original Content:
announcement: this is not written by me.

credits go to rsbot.org

What to start with

Ok, since your not "pro" scripters yet, there is a "skeleton" to follow. It already has all of the methods needed to run an RSBot script. What you have to do is fill in what you want the script to do inside of the methods. Here is a basic skeleton of a script (You will want to come get this each time you script).

Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;

public class Blank extends Script{	
	
	/*
	 * This is where you place all your variables
	 */
	
	
	public String getName(){
		return "Blank";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Scripts";
	}
	
	public double getVersion(){
		return 1.0;
	}
    
    public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	/*
    	 * This is the main loop that we discussed earlier
    	 * This method will be used over and over.
    	 * The concept behind this method is that it will start at 
    	 * the beginning. When you "return" any number (except -1)
    	 * it will check for randoms, do any antiban you may have
    	 * and start at the beginning of this method again.
    	 */
    	return -1;
    }
}


Understanding each method


Now I know many of you reading through that code above are getting pretty dang confused, so let me break it down for you.

Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;


These are the "imports"



Code:
public class Blank extends Script{

this is your title line. The name of your script will go where the word "Blank" is (this must have no spaces and just don't use numbers). Where it says "extends Script", do not worry about this. Extending is a programming concept you may not understand easily at this stage in learning. Just know that "extends Script" simply tells RSBot that it is a RSBot script and can be run by RSBot.

Code:
public String getName(){
		return "Blank";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Scripts";
	}
	
	public double getVersion(){
		return 1.0;
	}


These are all methods used to determine attributes of your script. Obviously, the name of your script goes in "getName", your personal name goes in "getAuthor". If you have used RSBot, when you click "Run" a window pops up with all these folders on the left and a description on the right. getScriptCategory determines the name of the folder in these categories. getVersion simply returns the version of the script.

These are all used for the description box I mentioned above. Just fill these in and forget them, as the really don't affect what happens in the script. When I fill these in, you may understand more.

Code:
public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	/*
    	 * This is the main loop that we discussed earlier
    	 * This method will be used over and over.
    	 * The concept behind this method is that it will start at 
    	 * the beginning. When you "return" any number (except -1)
    	 * it will check for randoms, do any antiban you may have
    	 * and start at the beginning of this method again.
    	 */
    	return -1;
    }


These are all the methods that will execute your code that you write for your character to use. In theory, you really need to focus on these the most, as they will be controlling the character. Brief descriptions have been added so your memory will be refreshed as to what they do.



Let's begin

Ok in this example, we are going to write a simple autominer for varrock. We are going to mine copper. We are first going to get the variable's we need to write this script. After this, we will begin constructing what we want the program to do. Please fill in the variables below, and start with this outline. It will be saved as CopperMiner.java, since that is what is on the title line where Blank used to be.


Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;

public class CopperMiner extends Script{	
	
	/*
	 * This is where you place all your variables
	 */
	
	
	public String getName(){
		return "Copper Miner";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Mining";
	}
	
	public double getVersion(){
		return 1.0;
	}
	
    public String getScriptDescription() {
    	String html = "";
        return(html);
    }
    
    public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	/*
    	 * This is the main loop that we discussed earlier
    	 * This method will be used over and over.
    	 * The concept behind this method is that it will start at 
    	 * the beginning. When you "return" any number (except -1)
    	 * it will check for randoms, do any antiban you may have
    	 * and start at the beginning of this method again.
    	 */
    	return -1;
    }
}



Gathering the variables

Ok, obviously the first variable we are going to need to gather is the variable of the rock. How we do this is we debug "Objects" on RSBot and we look at the number above the rock. The picture will explain.

2me068.jpg


4kidj6.jpg



Now that we have our number, 11961, we will make that an "integer variable" and place it where the skeleton says "place variables here" like so.

Code:
public class CopperMiner extends Script{	
	
	public int copperRock = 11961;// <---[COLOR="Red"] I added this.[/COLOR]
	
	public String getName(){
		return "Copper Miner";
	}

//etc.


Now what we are going to do, is get the animation of us when we mine rocks.

When your character is performing any type of emote or motion, he does an "animation". These animations are specific to the motion they are performing. Therefore, when we mine copper, our player performs an animation. You can get this like so.

rhnxnl.jpg


1z1r3h5.jpg


So now we add that to our script

Code:
public class CopperMiner extends Script{	
	
	public int copperRock = 11961;
       public int miningAnimation = 625; // <---- [COLOR="Red"]I added this.[/COLOR]
	
	public String getName(){
		return "Copper Miner";
	}

//etc.


After we accomplish these two tasks, we are going to get the ID of our pickaxe, because we need to know what not to drop when we unload our inventory. Here is how

o9lrgz.jpg




Now just update our variables really quick.
Code:
public class CopperMiner extends Script{	
	
	public int copperRock = 11961;
       public int miningAnimation = 625; 
       public int pickaxe = 1265; // <---- [COLOR="Red"]I added this. Add your number there[/COLOR]
	
	public String getName(){
		return "Copper Miner";
	}

//etc.



Writing the loop

Phew! Now my hardest part is done! (uploading images). However, your hardest part remains. This is writing the loop.

Make this a little side-note, anytime you "return" a method, it automatically exits the method, not looking at anymore code past that "return".

Now let's examine our loop. ( I deleted the comments out ).

Code:
public int loop() {
    	return -1;
    }

This is a very empty loop. Anytime you return -1 in the loop it will stop the script. Therefore, the script would take on look at this loop and stop the script. Any number above -1 (including 0) will be a "waiting" time. If you return 300, the script will "wait" 300 milliseconds before looking for randoms, doing antiban, and then starting the loop over again. (just to let you know, the are 1000 milliseconds in 1 second). So, let's clear that line and begin.

First of all, we don't want the script to do anything if our character is walking. We just want it to "wait" until we are done walking. So we will add this to the loop.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	return 300; // Just put this here so the script won't stop.
    }

getMyPlayer() is a method that returns all sorts of things. if you type "getMyPlayer()." (with a period at the end) and then press ctrl + spacebar, most IDE's will come up with a list of things you can use.

34sf8lv.jpg


Now, we also want it to "wait" if our player is already mining a rock. How do we know if he is mining a rock? By his animation of course. And oh yes! We have already found what his animation is if he is mining. So we add this to our code.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() [B][COLOR="Red"]==[/COLOR][/B] miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	return 300;
    }


The "==" operation

This is where many beginners get confused. "=" does not mean the literal defination of equals. "=" is used to set a value, like when we declared our variables.

Code:
public int copperRock [COLOR="Red"][B]=[/B] [/COLOR]11961;

You must use "==" when you are saying the literal definition of "equals". I know it's really confusing, just remember, when you are checking if something equals another thing, you use "==". If you are setting the value of a variable, you use "=";


Back to the loop()

if you did not catch the comment at the end of the last piece of code, go back to the example and look at it.

Now, I know you are all saying "Let's mine a rock!". Wrong! RSBot only does what we tell it to in what order we tell it to. So first, we must make sure the inventory is not full, because if we tell it to mine a rock before checking the inventory, it will always try to mine a rock even if the inventory is full. Make sense? We do not want to drop our pickaxe, so we add this.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() == miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	
    	if(isInventoryFull()){
    		dropAllExcept(pickaxe);
    		return 800;
    	}
    	
    	//if we have gotten here, this means all of the above comments,
    	//and our inventory is not full. This means it's perfect to
    	//mine a rock.
    	
    	return 300;
    }

This piece of code will simply drop our inventory if we have a full inventory.

Now the exciting part (lol). WE GET TO MINE A ROCK. Alright, now if you have gotten this far, you are hanging in there. Remember how we debugged "Objects" to get our rock ID? Ok, well now we have to click the rock (also an object) with the command "Mine"

1t78s9.jpg


So we will use the following code to find and mine a rock.

Code:
       RSObject rock = findObject(copperRock);
    	if(rock == null) return 800;
    	
    	atObject(rock, "Mine");

the first line is used to look for the object. We are simply declaring a variable, just like we would an int.

Code:
RSObject // <-- Type, this is where we had int earlier
rock // <-- our special name for this variable
findObject(copperRock); // <-- what the value will be.


If we cannot find a rock with that ID, the rock will be a value called "null". This means nothing, or we could not find one. This is where most scripters go wrong. They never handle if "rock" does not find the object, therefore, they get a Null Pointer exception. See the word Null? If you ever get this error, this means you didn't handle if something was "null". So in short, if RSBot can't find this object, we will wait 800 milliseconds and try again.

Lastly, we will use the method atObject(); This tells RSbot to go click the object, and then click the word that you put in the second arguement. Therefore our loop looks like this.

Code:
public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() == miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	
    	if(isInventoryFull()){
    		dropAllExcept(pickaxe);
    		return 800;
    	}
    	
    	//if we have gotten here, this means all of the above comments,
    	//and our inventory is not full. This means it's perfect to
    	//mine a rock.
    	
    	RSObject rock = findObject(copperRock);
    	if(rock == null) return 800;
    	
    	atObject(rock, "Mine");
    	
    	return 300;
    }


Conclusion

I know that writing scripts is complicated. This was a simple script, and showing you how to write it made my head hurt. All I can tell you is stick with it. My style of teaching you is different than most others. I do not teach you about "if" statements, I expect you to make that connection and understand what it means. I expect you to mess around with things like "getMyPlayer()." and see what RSbot can tell you about your player.

I tried to make this as simple and straightforward as possible. I promise you that once you start scripting more and have more practice, you will understand the harder parts of it.

If this does make sense to you, try following the same steps to make an auto woodcutter, or an auto fisher. You may have to handle a few more things that are different between mining and fishing (for example if you have bait).

If this makes no sense to you, do not be discouraged. Simply look at Mining, Woodcutting, and Fishing scripts and see what they did. Examine their loop, their onStart, and their onFinish.


Like I said, I did not cover tons of material, like letting the script find the pickaxe on it's own and storing it to a variable, handling if we don't have a pickaxe to stop the script (if you want to add this, use the command if(inventoryContains())), and tons of other stuff.

Don't worry about login and randoms, the script will find those any time you 'return' the loop.

All I can say is good luck to you my friend. Read more tutorials, read this tutorial more than once, examine scripts, search the file YourRSBotFolder.bot.com.speljohan.rsbot.script.methods.java, and have fun with it.

Finished script:

Code:
import java.awt.*;
import java.util.*;
import com.speljohan.rsbot.script.*;
import com.speljohan.rsbot.bot.*;
import com.speljohan.rsbot.script.wrappers.*;
import com.speljohan.rsbot.event.listeners.*;
import com.speljohan.rsbot.event.events.*;

public class CopperMiner extends Script{	
	
	public int copperRock = 11961;
    public int miningAnimation = 625; 
    public int pickaxe = 1265;
	
	public String getName(){
		return "Copper Miner";
	}
	
	public String getAuthor(){
		return "You";
	}
	
	public String getScriptCategory(){
		return "Mining";
	}
	
	public double getVersion(){
		return 1.0;
	}
	
    public String getScriptDescription() {
    	String html = "";
        return(html);
    }
    
    public boolean onStart(Map<String, String> args) {
    	/*
    	 * What you want to do on the starting of the script
    	 * Goes here. It must return true for the script to run.
    	 * If you return false, it will stop the entire script
    	 * and it will say "Failed to startup";
    	 * This will only be used once, at the beginning
    	 */
       return true;
    }
    
    
    
    public void onFinish(){
    	/*
    	 * This is what you want it to do when the user 
    	 * Stops the script.
    	 * This will only be used once, at the end.
    	 */
        return;
     }
    
    public int loop() {
    	if(getMyPlayer().isMoving()){
    		return 800;
    	}
    	
    	if(getMyPlayer().getAnimation() == miningAnimation){
    		return 800;
    	}
    	
    	//If the loop has gotten this far, we know we are not already
    	//mining a rock and We know we are not moving, therefore
    	//it is safe to mine a rock
    	
    	if(isInventoryFull()){
    		dropAllExcept(pickaxe);
    		return 800;
    	}
    	
    	//if we have gotten here, this means all of the above comments,
    	//and our inventory is not full. This means it's perfect to
    	//mine a rock.
    	
    	RSObject rock = findObject(copperRock);
    	if(rock == null) return 800;
    	
    	atObject(rock, "Mine");    	
    	return 300;
    }
}
[/CENTER]
 
He states right off the bat that he did not create the guide.
 
Status
Not open for further replies.
Runescape Gamblingrunescape gambling
Back
Top