Jump to content

{Beginner} Simple woodcutting script need help


12we12qw12

Recommended Posts

I'm making a woodcutting script but I have ran into an issue, when I get a full inventory of logs my character will walk to the back but will not deposit the logs but instead run back to the trees and try to cut them down. This making my character run back and forth. Script is below, I don't know what to add to fix this. If you guys have any other recommendations to add to the script or tips let me know, Thanks.

@Override
public void onStart() throws InterruptedException {
    super.onStart();


    int wcLvl = getSkills().getStatic(Skill.WOODCUTTING);

    if (getInventory().isEmptyExcept("Bronze axe", "Iron axe", "Steel Axe", "Black axe", "Mithril axe", "Adamant axe", "Rune axe",
            "Dragon axe")) ;


}



    @Override
public int onLoop() throws InterruptedException {


    Player player = myPlayer();

    RS2Object Tree = getObjects().closest(new Filter<RS2Object>() {
        @Override
        public boolean match(RS2Object rs2Object) {
            return rs2Object != null && rs2Object.getName().equals("Tree") && rs2Object.exists() && getMap().canReach(rs2Object);
        }
    });

    if (Banks.VARROCK_WEST.contains(myPosition()) && bankbooth != null && !getBank().isOpen()) {
        getBank().withdraw("Steel axe",1);
    }
    if (Tree != null)
        if (Tree.isVisible()) {
            getWalking().walk(Tree.getPosition());
            getCamera().toEntity(Tree);
            if (Tree.getPosition().distance(myPosition()) > 6) ;
            if (!myPlayer().isAnimating())
                if (Tree.interact("Chop down")) ;
            getMouse().moveOutsideScreen();
            sleep(random(301, 2999));


        } else {

            RS2Object bankBooth = getObjects().closest("Bank booth");

            if (getInventory().isFull()) getWalking().webWalk(BANK_AREA);
           

            log("Inventory full walking to bank");

            if (Banks.VARROCK_WEST.contains(myPosition()) && bankbooth != null && !getBank().isOpen()) {
                getBank().depositAllExcept("Bronze axe", "Iron axe", "Steel axe", "Black axe", "Mithril axe", "Adamant axe", "Rune axe", "Dragon axe");


                log("interacting with bank");
                bankBooth.interact("Bank");
            } else if (getBank().isOpen()) {

                int wcLvl = getSkills().getStatic(Skill.WOODCUTTING);

                log("Depositing Logs");

                getBank().depositAllExcept("Bronze axe", "Iron axe", "Steel axe", "Black axe", "Mithril axe", "Adamant axe", "Rune axe", "Dragon axe");

                if (wcLvl >= 6 && getInventory().contains("Bronze axe", "Iron axe") && getBank().contains("Steel axe")) {

                    log("withdrawing shitty axe");
                    getBank().deposit("Bronze axe", 1);
                    getBank().withdraw("Steel axe", 1);



                }


            }

        }return 0;}}
Link to comment
Share on other sites

I'd also recommend making some methods and take a look at conditional sleeps.

It's running back and forth because both your banking if statement and your chopping if statement can both be valid at the same time.

Also, change the return from 0 to like 600 or so as it'll help reduce CPU usage etc

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

22 minutes ago, Imthabawse said:

Perhaps split the script into 2 methods

Private void chop () {

// chop code here

Private void bank () {

// bank code here

 

Definitely feel this here. The readability of this code should jump out at you. 

 

1 hour ago, 12we12qw12 said:

I'm making a woodcutting script but I have ran into an issue, when I get a full inventory of logs my character will walk to the back but will not deposit the logs but instead run back to the trees and try to cut them down. This making my character run back and forth. Script is below, I don't know what to add to fix this. If you guys have any other recommendations to add to the script or tips let me know, Thanks.


    int wcLvl = getSkills().getStatic(Skill.WOODCUTTING);

    if (getInventory().isEmptyExcept("Bronze axe", "Iron axe", "Steel Axe", "Black axe", "Mithril axe", "Adamant axe", "Rune axe",
            "Dragon axe")) ;
}
.......

    if (Banks.VARROCK_WEST.contains(myPosition()) && bankbooth != null && !getBank().isOpen()) {
        getBank().withdraw("Steel axe",1);
    }
           

I would look into flow control for help with this...

Link to comment
Share on other sites

Study @Explv's Scripting tutorial and study the structure of his example script. This should give you a better idea of how to write your own scripts. Studying some Java syntax wont hurt either. Look into conditional sleeps as well so your bot will sleep until a condition is met rather than trying to execute actions instantaneously. 

Link to comment
Share on other sites

There are many logical errors, go over each line and make sure they make sense. I highly recommend using { } properly, otherwise it will be super confusing, the entire 'if (Tree != null) is a one line if statement extending 50 lines, also format the code it should be more readable, which software are you using for coding?

Split up the logic into two, so you have player_woodcutting, and player_banking:

The woodcutting part isn't done, you gotta do that yourself:

	    @Override
    public int onLoop() throws InterruptedException {
        if (getInventory().isFull()) {
            /*
             * If inventory full, go bank
             */
            if (!Banks.VARROCK_WEST.contains(myPlayer())) {
                getWalking().webWalk(Banks.VARROCK_EAST);
            } else {
                if (!getBank().isOpen()) {
                    getBank().open();
                    return 1;
                }
                if (getBank().depositAllExcept("Bronze axe", "Iron axe", "Steel axe", "Black axe",
                        "Mithril axe", "Adamant axe", "Rune axe", "Dragon axe")) {
                    log("Deposited everything except axe");
                }
            }
        } else {
            /*
             * Inventory not full yet, keep woodcutting.
             */
            RS2Object treeObject = getObjects().closest(a -> a.getName().equalsIgnoreCase("Tree"));
            if (treeObject != null) {
                if (treeObject.interact("Chop-down")) {
                    log("Clicked chop!");
                }
            }
        }
        return 0;
    }
	

Edited by Czar
Link to comment
Share on other sites

Yeah the logic in this code makes no sense. Also, you should almost NEVER return 0.

Please, do yourself a favor and look here: https://www.w3schools.com/java/java_conditions.asp

This will explain how each statement works. After looking this over you should have a better understanding - even without experience. It's easier than you would think 😛

At the bottom of the page there are also exercises for practice.

Good luck.

 

Edited by AsBakedAsCake
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...