Jump to content

My first woodcutting script: troubleshooting thread


EgoLiberation

Recommended Posts

Hi guys I'm pretty new here but I'm trying to learn how to script. I did some courses online with java and I'm trying to apply this knowledge by making a woodcutting script. 

So far, I've made it be able to cut trees if  1. tree is not null 2. player is within defined area 3. player is not animating 4. player is not moving. 

I tried to add another condition that made it only choose trees that are within the area I've created for it because I found that the player got into a loop where it would target trees outside of the area I specified for it to be in and then would return to the area. After I did this there were a bunch of errors so I commented out the code I had put in and tried to fix the if else logic but I think I broke everything. I press Start (local) in osbot and nothing happens.

Going to try and paste the code in here. Still new to posting on forums in general so I apologize if I butcher this. 

edit: code is really messy, I'm sorry but most of the code I'm using is at the top except for the sleep class at the very bottom.

Quote

 


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

import java.awt.*;
import java.util.function.BooleanSupplier;

@ScriptManifest(author = "EgoLiberation", name = "woodcutter v0.31", info = "Woodcuts normal Trees", version = 0.1, logo = "cut tree and drop for now")
public final class Woodcutter extends Script {
    //initialize variables here

    //Name of tree
    String TREE_NAME = "Tree";

   /* String TREE_NAME = "Tree";
    Entity tree = getObjects().closest(TREE_NAME);
    final Area treeArea = new Area(3112, 3222, 3098, 3210); */
    /* boolean playerNearTrees = treeArea.contains(myPlayer());
    */

    //area in which I want to cut trees
   Area treeArea = new Area(3095, 3210, 3111, 3223);

   //variable that holds the type of tree I want to cut
   RS2Object tree = getObjects().closest(TREE_NAME);

    //code to be executed
    @Override
    public final void onStart() {
        log("Let's begin Woodcutter!");
    }

    @Override
    public final int onLoop() throws InterruptedException {

        if (tree != null && inTreeArea()) {
            log("Tree is available");
            log("Player is in TreeArea");
            if (isNotAnimating()) {
                // if(isTreeInArea()) {
                    log("Player is not animating");
                    tree.interact("Chop down");
                    log("Sleeping until I cut");
                    Sleep.sleepUntil(this::isAnimating, (random(5000, 7000)));
                    log("Cutting Tree");
            } else {
                log("Player is animating, sleeping until animation is finished");
                Sleep.sleepUntil(this::isNotAnimating, (random(5000, 10000)));
            }

        } else if (!inTreeArea()) {
                log("Not in tree area; moving there now");
                walking.walk(treeArea);
                log("SleepWalking");
                Sleep.sleepUntil(this::isNotRunning, (random(1000, 5000)));
            }

        else {
            log("Tree is not available - Cannot cut tree");
        }
        log("Loop Finished Starting over");
        return random(100, 200);
    }

    //checks if tree is in the treeArea

    //error: cant seem to use if else logic properly to make this work
    //script will not start with current setup
   /* private boolean isTreeInArea(){
        return treeArea.contains(tree.getPosition());
    } */

    //checks if player is in the tree area
    private boolean inTreeArea(){
        return treeArea.contains(myPosition());
    }
    //checks if player is animating
    private boolean isAnimating(){
        return !isNotAnimating();
    }
    /*checks if player is not animating...
    I know i can use ! operator
    I cant find out how to sleepUntil a condition is false
    */
    private boolean isNotAnimating(){
        return !myPlayer().isAnimating();
    }
    //checks if player is not running...
    private boolean isNotRunning(){
        return !myPlayer().isMoving();

    }

    public final void onExit() {
        log("Woodcutter is finished!");
    }

    //paint
    @Override
    public final void onPaint(Graphics2D graphics2D) {

    }
}

class Sleep extends ConditionalSleep {
    private final BooleanSupplier condition;

    public Sleep(final BooleanSupplier condition, final int timeout) {
        super(timeout);
        this.condition = condition;
    }

    @Override
    public final boolean condition() throws InterruptedException {
        return condition.getAsBoolean();
    }
    // if someone could explain why sleepUntil is never used?
    public static boolean sleepUntil(final BooleanSupplier condition, final int timeout){
        return new Sleep(condition, timeout).sleep();
    }

}

 

 

Edited by EgoLiberation
Link to comment
Share on other sites

You want to make sure you're assigning the tree variable within your loop and not just inside your class, because otherwise it will never get reassigned.

Also, your code will currently get the closest tree to the player which may not be in the tree area, so one way to only get trees within the tree area is using Filters. Here is an example of an RS2Object filter where I'm filtering for three things:

 


Area draynorArea = new Area(3075, 3249, 3102, 3214);
public static Filter<RS2Object> WILLOW = new Filter<RS2Object >() {
  @Override
    public boolean match(RS2Object object) {
    return object.getName().equals("Willow") && object.hasAction("Chop down") && draynorArea.contains(object);
  }
};

Hopefully it's clear based on the code, but this filters for objects named "Willow" that have the action "Chop down" that are contained in my area variable of draynorArea. Then making use of this would be:

 

RS2Object myTree = objects.closest(WILLOW);

 

Now the variable myTree will only get the closest object that fits our filter. Best of luck!

  • 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...