Jump to content

trying to create a woodcutting script


heervangrijsburg

Recommended Posts

Hey guys,

I'm trying to learn how to make scripts.

The first script that I'm trying to make is a woodcutting script and this is what I have so far :

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;




@ScriptManifest(author = "Heervangrijsburg", logo = "", info ="Will cut trees" , version = 1.0 , name = "WC tree")
public class Main extends Script {

    @Override

    public void onStart(){

    }
    public void onExit(){

    }
    public int onLoop() throws InterruptedException {
        if(!getInventory().isFull()){
            Area woodcutting = new Area(3086, 3263, 3074, 3272);
            Entity tree = objects.closest("tree");
            if(!woodcutting.contains(myPlayer())){
                getWalking().walk(woodcutting);
            }else{
                if(tree != null) {
                    if(!myPlayer().isAnimating()) {
                        if(!myPlayer().isMoving()) {
                            tree.interact("Chop Down");
                        }
                    }
                }
            }
        }else{
                Area Bank = new Area(3092, 3241, 3093, 3242);
                    if(!Bank.contains(myPlayer())){
                        getWalking().walk(Bank);
                            }else if(!bank.isOpen()){
                                        bank.open();
                                    }else{
                                        bank.depositAll("Logs");
                                            bank.close();
                                }
        }return 1000;

    }
}

But sometimes it seems to get stuck and when I start the script when the inventory is full the script doesn't work at all.

So my questiopn is whether someone who has more experience with this could take a look at it  and give me some tips?

 

 

Link to comment
Share on other sites

you are creating a new area everytime you loop even though you are going to chop at the same area. 

also you did not define what happens when the inventory is full since you just nested your IFs

 

Spoiler

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;




@ScriptManifest(author = "Heervangrijsburg", logo = "", info ="Will cut trees" , version = 1.0 , name = "WC tree")
public class Main extends Script {
 final Area woodcutting = new Area(3086, 3263, 3074, 3272);
    @Override

    public void onStart(){

    }
    public void onExit(){

    }
    public int onLoop() throws InterruptedException {
    Entity tree = objects.closest("tree");       //isn't T in capital? i'd say add a null check and canReach method here
if(woodcutting.contains(myPlayer())){     
 if(!getInventory().isFull()){     
                if(tree != null) {                      // why not use && Operator? to check multiple conditions
                    if(!myPlayer().isAnimating() &&!myPlayer().isMoving()) {
                            tree.interact("Chop Down");
                        }        // where are you Sleeps?
                                 //What if there player is animating and moving? script should ideally sleep
                    }  // what to do if tree is null?// search a new tree?
                } //this is where you define what to do when inventory is full
            }  // this is where you define if it should go to the tree area
            getWalking.walk(woodcutting); // use webwalker if you are not using custom walking method/ if your bank is far
        }   else{
                Area Bank = new Area(3092, 3241, 3093, 3242); // define this at the beginning. like i did with your wc area
                    if(!Bank.contains(myPlayer())){       // define what to do if in bank first. ideally by now you should get an idea about fixing your issue
                        getWalking().walk(Bank);       //i'd suggest you introduce Conditonal Sleeps to your script, you are telling the script to do all the actions at once.   
                            }else if(!bank.isOpen()){  // "Hint" use booleans, and make methods if you know about them.
                                        bank.open();  // This should be enough to help you out.
                                    }else{
                                        bank.depositAll("Logs");
                                            bank.close();
                                }
        }return 1000;

    }
}
 

 

Edited by JioMy
Link to comment
Share on other sites

Move the defined Areas outside of your loop. You're using a WalkingEvent instance, which can only travel so far, you're probably better off looking into the WebWalker until you understand the difference.

Other things you should do: 
 - Add Conditional Sleeps
 - Check if the tree is within your woodcutting area, otherwise it might try cut a tree outside the area, then just walk back into the area.

Link to comment
Share on other sites

Hi guys, I think I did almost everything you guys suggested. the script does not get stuck anymore and I can start the script with a complete inventory.
However, I have not made any methods yet, but maybe someone already wants to take a look at the script and tell me what I can improve on it.
Special thx to @JioMy for his detailed report on everthing I could improve on every line.

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;




@ScriptManifest(author = "Heervangrijsburg", logo = "", info ="Will cut trees" , version = 1.2 , name = "WC tree")
public class Main extends Script {
    private Area Woodcutting = new Area(3088, 3262, 3070, 3273);
    private Area Bank = new Area(3092, 3241, 3093, 3242);

    @Override

    public void onStart() {

    }

    public void onExit() {

    }

    public int onLoop() throws InterruptedException{
        if (!getInventory().isFull()){
            if (Woodcutting.contains(myPlayer())){
                Entity Tree = objects.closest("tree");
                    if (Tree != null && Woodcutting.contains(Tree)){
                            Tree.interact("Chop Down");
                                new ConditionalSleep(5000){
                                    public boolean condition() throws InterruptedException{
                                        return !myPlayer().isAnimating() && !myPlayer().isMoving();
                                }
                            }.sleep();

                    }else{
                        sleep(200);
                    }
            }else{
                getWalking().webWalk(Woodcutting);
                    new ConditionalSleep(5000){
                        public boolean condition() throws InterruptedException{
                            return Woodcutting.contains(myPlayer());
                        }
                    }.sleep();
            }

        }else{
            if (Bank.contains(myPlayer())){
                if (bank.isOpen()){
                        bank.depositAll("Logs");
                            bank.close();
                                sleep(200);
            } else {
                bank.open();
                    sleep(200);
            }
            } else {
                getWalking().webWalk(Bank);
                new ConditionalSleep(5000){
                    public boolean condition() throws InterruptedException{
                        return Bank.contains(myPlayer());
                    }
                }.sleep();
            }

            }return 0;
    }
}

 

 

Link to comment
Share on other sites

Spoiler

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;




@ScriptManifest(author = "Heervangrijsburg", logo = "", info ="Will cut trees" , version = 1.2 , name = "WC tree")
public class Main extends Script {
    private Area Woodcutting = new Area(3088, 3262, 3070, 3273);
    private Area Bank = new Area(3092, 3241, 3093, 3242);

    @Override

    public void onStart() {

    }

    public void onExit() {

    }

    public int onLoop() throws InterruptedException{
        if (!getInventory().isFull()){   
            if (Woodcutting.contains(myPlayer())){ 
                Entity Tree = objects.closest("tree");      //what if you don't have an axe?
              if (Tree != null && Woodcutting.contains(Tree)){  //use a filter to get trees only from ur tree area
                       Tree.interact("Chop Down"); 
                                new ConditionalSleep(5000){  
                                    public boolean condition() throws InterruptedException{
                                        return !myPlayer().isAnimating() && !myPlayer().isMoving();
                                }
                            }.sleep();

                    }else{
                        sleep(200); // point of this?
                    }
            }else{
                getWalking().webWalk(Woodcutting); 
                    new ConditionalSleep(5000){
                        public boolean condition() throws InterruptedException{
                            return Woodcutting.contains(myPlayer());
                        }
                    }.sleep();
            }

        }else{
            if (Bank.contains(myPlayer())){  
                if (bank.isOpen()){ 
                        bank.depositAll("Logs"); // you might want to add a Conditional Sleep here as well, what if you lag and don't deposit
                            bank.close();        // it will close the bank and go to wc area with full inventory and return just because u lagged
                                sleep(200); // This is not Conditional Sleep as well
            } else {
                bank.open();
                    sleep(200); //This is not conditional sleep
            }
            } else {
                getWalking().webWalk(Bank);
                new ConditionalSleep(5000){
                    public boolean condition() throws InterruptedException{
                        return Bank.contains(myPlayer());
                    }
                }.sleep();  //?????  Consider Lambdas?
            }

            }return 0; // Looping TOO FAST.
    }
}
 
 

Hi, now that you've got this far how about making this progressive and switch axes as well?

Edited by JioMy
  • Like 1
Link to comment
Share on other sites

This will probably work.

Spoiler

import java.awt.Graphics2D;

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.model.RS2Object;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;


@ScriptManifest(author = "Heervangrijsburg(not)", logo = "", info ="Will cut trees" , version = 1.2 , name = "WC tree")
public class wcFake extends Script {

	private Area Woodcutting, Bank;
	RS2Object Tree;
	
	private long runTime;
	public String state;

	@Override
	public void onStart() {
		Woodcutting = new Area(3088, 3262, 3070, 3273);
		Bank = new Area(3092, 3241, 3093, 3242);
		experienceTracker.start(Skill.WOODCUTTING);
	}

	public void onExit() {
		log("Gained: " + experienceTracker.getGainedXP(Skill.WOODCUTTING) + "xp");
		log("Gained: " + experienceTracker.getGainedLevels(Skill.WOODCUTTING) + " levels.");
	}


	@SuppressWarnings("unchecked")
	public int onLoop() throws InterruptedException{
		Tree = getObjects().closest(obj -> obj.getName().equals("Tree"));
		if (!inventory.isFull()) {
			if (Woodcutting.contains(myPlayer())) {
				if (Tree != null && Woodcutting.contains(Tree) && map.canReach(Tree) && !isAnimating()){
					state = "Cutting";
					Tree.interact("Chop down"); 
					new ConditionalSleep(20000){  
						public boolean condition() throws InterruptedException{
							return !isAnimating();
						}
					}.sleep();
				}

			} else if (!Woodcutting.contains(myPlayer())) {
				state = "Walking to trees";
				getWalking().webWalk(Woodcutting); 
			}

		} else {
			if (Bank.contains(myPlayer())){
				if (!bank.isOpen()) {
					state = "Banking";
					bank.open();
					new ConditionalSleep(5000) {
						@Override
						public boolean condition() throws InterruptedException {
							return bank.isOpen();
						}
					}.sleep();
				}

				if (bank.isOpen()) {
					state = "Depositing";
					bank.depositAll("Logs"); 
					new ConditionalSleep(5000) {
						@Override
						public boolean condition() throws InterruptedException {
							return !inventory.contains("Logs");
						}
					}.sleep();
					bank.close();
					new ConditionalSleep(5000) {
						@Override
						public boolean condition() throws InterruptedException {
							return !bank.isOpen();
						}
					}.sleep();
				}
			}
			
			if (!Bank.contains(myPlayer())) {
				state = "Walking to bank";
				getWalking().webWalk(Bank);
			}


		}
		return 600;
	}

	public boolean isAnimating() throws InterruptedException{
		for(int i = 0; i < 5; i++){
			if(myPlayer().isAnimating() || players.myPlayer().isMoving())
				return true;
			else
				state = "Sleeping";
				sleep(100);
		}
		return false;
	}
	
	public final String formatTime(final long ms){
		long s = ms / 1000, m = s / 60, h = m / 60;
		s %= 60; m %= 60; h %= 24;
		return String.format("%02d:%02d:%02d", h, m, s);
	}

	
	@Override
	public void onPaint(Graphics2D g) {
		g.drawString("State: " + state, 550, 275);
		g.drawString("Runtime: " + formatTime(runTime), 550, 290);
		g.drawString("Exp gained: " + experienceTracker.getGainedXP(Skill.WOODCUTTING), 550, 305);
		g.drawString("Exp/Hour: " + experienceTracker.getGainedXPPerHour(Skill.WOODCUTTING), 550, 320);
		g.drawString("Levels gained: " + experienceTracker.getGainedLevels(Skill.WOODCUTTING), 550, 335);
	}
}

 

 

Edited by Sonysi
Link to comment
Share on other sites

Hi, I'm trying to make the script that it changes from axe´s when he gets to a certain level. For this I use the switch statement, but in any case i get the error message "Constant expression required". can someone pls tell me what I´m doing wrong?

int Woodcuttinglevel = skills.getDynamic(Skill.WOODCUTTING);
            switch (true) {
                case (Woodcuttinglevel >= 5):
                    break;
                case (Woodcuttinglevel >= 10):
                    break;
                case (Woodcuttinglevel >= 20):
                    break;
                case (Woodcuttinglevel >= 30):
                    break;
                case (Woodcuttinglevel >= 40):
                    break;
                default:
                    break;

 

Link to comment
Share on other sites

1 hour ago, heervangrijsburg said:

Hi, I'm trying to make the script that it changes from axe´s when he gets to a certain level. For this I use the switch statement, but in any case i get the error message "Constant expression required". can someone pls tell me what I´m doing wrong?


int Woodcuttinglevel = skills.getDynamic(Skill.WOODCUTTING);
            switch (true) {
                case (Woodcuttinglevel >= 5):
                    break;
                case (Woodcuttinglevel >= 10):
                    break;
                case (Woodcuttinglevel >= 20):
                    break;
                case (Woodcuttinglevel >= 30):
                    break;
                case (Woodcuttinglevel >= 40):
                    break;
                default:
                    break;

 

You shouldn't be using switch statements for condition checking. Use else if statements instead.

String suitableAxe;

if(woodcuttingLevel < 5) {

suitableAxe = "Iron axe";

else if(woodcuttingLevel < 10) {

suitableAxe = "Steel axe";

}

and so on.

 

Switch statements are useful in certain situations but not here. Read this: https://docs.oracle.com/javase/tutorial/java/nutsandbolts/switch.html

Edited by Jammer
  • Like 1
Link to comment
Share on other sites

Hi so i used ´else if´ statements but now my bot is not able to withdraw anything from the bank could someone take a look at my code ?

thanks by the way for all the support.

import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import task.Woodcutting;


@ScriptManifest(author = "Heervangrijsburg", logo = "", info ="Will cut trees" , version = 1.2 , name = "WC tree")
public class Main extends Script {
    private Area Bank = new Area(3180, 3446, 3183, 3435);
    private String Axetouse = "";
    @Override

    public void onStart() {

    }

    public void onExit() {

    }

    public int onLoop() throws InterruptedException {
        int Woodcuttinglevel = skills.getDynamic(Skill.WOODCUTTING);
        if (Woodcuttinglevel <= 5){
            Axetouse = "Bronze axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 10){
            Axetouse = "Steel axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 20){
            Axetouse = "Black axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 30){
            Axetouse = "Mithril axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 40){
            Axetouse = "Adamant axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 99){
            Axetouse = "Rune axe";
            sleep(200);
        }
        if (inventory.contains(Axetouse)){
            new Woodcutting(this);
        } else {
            if (Bank.contains(myPlayer())) {
                    if (bank.isOpen()) {
                            bank.withdraw(Axetouse, 1);
                                bank.close();
                                    sleep(200);
                    } else {
                        bank.open();
                        sleep(200);
                    }
            } else {
                getWalking().webWalk(Bank);
                    new ConditionalSleep(5000) {
                        public boolean condition() throws InterruptedException {
                            return Bank.contains(myPlayer());
                    }
                }.sleep();
            }
        }
        return 1000;
    }
}

 

Link to comment
Share on other sites

13 hours ago, heervangrijsburg said:

Hi so i used ´else if´ statements but now my bot is not able to withdraw anything from the bank could someone take a look at my code ?

thanks by the way for all the support.


import org.osbot.rs07.api.map.Area;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;
import task.Woodcutting;


@ScriptManifest(author = "Heervangrijsburg", logo = "", info ="Will cut trees" , version = 1.2 , name = "WC tree")
public class Main extends Script {
    private Area Bank = new Area(3180, 3446, 3183, 3435);
    private String Axetouse = "";
    @Override

    public void onStart() {

    }

    public void onExit() {

    }

    public int onLoop() throws InterruptedException {
        int Woodcuttinglevel = skills.getDynamic(Skill.WOODCUTTING);
        if (Woodcuttinglevel <= 5){
            Axetouse = "Bronze axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 10){
            Axetouse = "Steel axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 20){
            Axetouse = "Black axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 30){
            Axetouse = "Mithril axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 40){
            Axetouse = "Adamant axe";
            sleep(200);
        } else if (Woodcuttinglevel <= 99){
            Axetouse = "Rune axe";
            sleep(200);
        }
        if (inventory.contains(Axetouse)){
            new Woodcutting(this);
        } else {
            if (Bank.contains(myPlayer())) {
                    if (bank.isOpen()) {
                            bank.withdraw(Axetouse, 1);
                                bank.close();
                                    sleep(200);
                    } else {
                        bank.open();
                        sleep(200);
                    }
            } else {
                getWalking().webWalk(Bank);
                    new ConditionalSleep(5000) {
                        public boolean condition() throws InterruptedException {
                            return Bank.contains(myPlayer());
                    }
                }.sleep();
            }
        }
        return 1000;
    }
}

 

because you have not given parameters to your String 

  • Like 1
Link to comment
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.
Note: Your post will require moderator approval before it will be visible.

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.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...