Jump to content

Oak larders


Imthabawse

Recommended Posts

Been working on creating a free Oak larder script for the community to use but time and time again I run into a problem with getting the "Servant" to successfully un-note planks and have the bot continue building. So I'm going to take a break from this adventure and further my knowledge on Java to better structure my scripts in the future. For now I will post the code that works so someone in the community can get use/learn from it. 

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.ConditionalSleep;

@ScriptManifest(name = "LardersV2", logo = "", version = 1, author = "Imthabawse", info = "Builds Oak Larders")

public class Oaklarders extends Script {


    @Override
    public int onLoop() throws InterruptedException {

        if (getInventory().getAmount(8778) <= 8) { // If inventory contains 8 or equal to 8 oak planks
        }
        Entity larderspace = getObjects().closest("Larder space"); // Get closest Larder space
        if (larderspace != null && larderspace.interact("Build")) {  // If Larder space exists then Build
        }
        new ConditionalSleep(4000) {
            @Override
            public boolean condition() {
                return !larderspace.exists();
            }
        }.sleep();

        RS2Widget buildlarder = getWidgets().get(458, 5, 4); // Oak larder widget
        if (buildlarder != null && buildlarder.interact("Build")) { // If exists then build larder
        }
        new ConditionalSleep(4000) {
            @Override
            public boolean condition() {
                return myPlayer().isAnimating();
            }
        }.sleep();

        Entity larder = getObjects().closest("Larder"); // Get closest larder
        if (larder != null && larder.interact("Remove")) { // If exists remove it
        }
        new ConditionalSleep(4000) {
            @Override
            public boolean condition() {
                return !larder.exists();
            }
        }.sleep();

        RS2Widget remove = getWidgets().getWidgetContainingText("Yes");
        if (remove != null && remove.interact()) {
        }
        new ConditionalSleep(4000) {
            @Override
            public boolean condition() {
                return myPlayer().isAnimating();
            }
        }.sleep();

        return 1000;
    }
}

 

Link to comment
Share on other sites

I cleaned up your code a little bit. You have some really glaring problems, the first of which is the fact that all of your if(){} statements are empty. Frankly speaking this is unacceptable.

I've incorporated a pared-down version of @Explv's Sleep utility which makes your code much more readable. I think if you use this version you'll have an easier time figuring out your bugs. One other thing I did fix was the fact that you're reusing Entities. Changing the state of an entity in-game will require you to reacquire that entity again to retrieve its updated properties. In other words, if you get an Object, and then destroy that object in-game (by removing a larder), that first object's properties won't be updated. You have to reacquire it.

import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.ui.RS2Widget;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.utility.Condition;
import org.osbot.rs07.utility.ConditionalSleep;

import java.util.function.BooleanSupplier;

@ScriptManifest(name = "LardersV2", logo = "", version = 1, author = "Imthabawse", info = "Builds Oak Larders")

public class Oaklarders extends Script {
    private final int ID_OAK_PLANK = 8778;

    private final int WIDGET_LARDER_ROOT = 458;
    private final int WIDGET_LARDER_CHILD = 5;
    private final int WIDGET_LARDER_SUBCHILD = 4;

    private final int MIN_OAK_PLANKS = 8;

    @Override
    public int onLoop() {
        if (getInventory().getAmount(ID_OAK_PLANK) <= MIN_OAK_PLANKS) { // If inventory contains 8 or equal to 8 oak planks
            Entity larderspace = getObjects().closest("Larder space"); // Get closest Larder space
            
            if (larderspace != null && larderspace.interact("Build")) {  // If Larder space exists then Build
                Sleep.until(() -> !getObjects().closest(f -> f.getPosition().equals(larderspace.getPosition())).exists(), 400, 500);
            }

            RS2Widget buildlarder = getWidgets().get(WIDGET_LARDER_ROOT, WIDGET_LARDER_CHILD, WIDGET_LARDER_SUBCHILD); // Oak larder widget

            if (buildlarder != null && buildlarder.interact("Build")) { // If exists then build larder
                Sleep.until(() -> myPlayer().isAnimating(), 4000, 500);
            }

            Entity larder = getObjects().closest("Larder"); // Get closest larder
            if (larder != null && larder.interact("Remove")) { // If exists remove it
                Sleep.until(() -> !getObjects().closest(f -> f.getPosition().equals(larder.getPosition())).exists(), 400, 500);
            }

            RS2Widget remove = getWidgets().getWidgetContainingText("Yes");
            if (remove != null && remove.interact()) {
                Sleep.until(() -> myPlayer().isAnimating(), 4000, 500);
            }
        }
        return 1000;
    }

    protected static class Sleep extends ConditionalSleep {
        private final BooleanSupplier condition;

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

        @Override
        public boolean condition() {
            return condition.getAsBoolean();
        }

        public static boolean until(final BooleanSupplier condition, int timeout, int interval) {
            return new Sleep(condition, timeout, interval).sleep();
        }
    }
}

 

  • Like 1
Link to comment
Share on other sites

  • 2 weeks later...

Been playing around with the code and came up with this... Been running it no problems so far. 

Improvements: 

- Don't need planks to get script going will call butler at start if no planks in inventory

- Will interact if butlers next to you instead of going through widgets every time

- Script will stop n logout if no planks in bank 

Things to improve:

- Would like to add random movement of camera at random times 

- Informative paint exp gained, lvls gained, time ran etc

- Better sleeps 

- Script will break sometimes and just exit most likely due to my fucked up sleeps and improper use of shit ?

 

Still learning just having fun messing around with the whole thing... pointers and bashing welcome. 

 

Scripters when they see this: :facep: :???:

*CODE: 

public class Autolarders extends Script {

    private void onstartCheck() throws InterruptedException {
        RS2Widget settings = getWidgets().get(548, 35);
        if (settings != null) {
            settings.interact();
            sleep(random(1000, 1500));
            RS2Widget house = getWidgets().get(261, 99);
            if (house != null) {
                house.interact();
                sleep(random(1000, 1500));
                RS2Widget servant = getWidgets().get(370, 19, 0);
                if (servant != null) {
                    servant.interact();
                    log("Calling servant...");
                    sleep(random(1000, 1500));
                    RS2Widget fetch = getWidgets().get(219, 1, 1);
                    if (fetch != null) {
                        fetch.interact();
                        log("Un-noting planks.. please wait...");
                        sleep(random(1500, 2000));
                        RS2Widget inven = getWidgets().get(548, 51);
                        if (inven != null) {
                            inven.interact();
                            sleep(random(800,1000));
                            getMouse().moveOutsideScreen();
                            log("Returning to inventory...");
                            sleep(random(7300, 7500));

                        }
                    }
                }
            }
        }
    }

    private void build() throws InterruptedException {
        RS2Object larderspace = getObjects().closest("Larder space");
        if (larderspace != null) {
            larderspace.interact("Build");
            sleep(random(800, 1000));
            RS2Widget buildlarder = getWidgets().get(458, 5, 4);
            if (buildlarder != null) {
                buildlarder.interact();
                log("Building larder..");
                new ConditionalSleep(800) {
                    @Override
                    public boolean condition() {
                        return myPlayer().isAnimating();
                    }
                }.sleep();
            }
        }
    }
    private void remove() throws InterruptedException {
        RS2Object larder = getObjects().closest("Larder");
        if (larder != null) {
            larder.interact("Remove");
            sleep(random(800, 1000));
            RS2Widget removelarder = getWidgets().getWidgetContainingText("Yes");
            if (removelarder != null) {
                removelarder.interact();
                log("Removing larder..");
                new ConditionalSleep(800) {
                        @Override
                        public boolean condition() {
                            return myPlayer().isAnimating();
                        }
                    }.sleep();
                }
            }
        }

        private void butler() throws InterruptedException {
            NPC demonbutler = getNpcs().closest("Demon butler");
            if (demonbutler.isVisible()) {
                demonbutler.interact("Talk-to");
                log("Interacting with butler..");
                sleep(random(800, 1000));
                RS2Widget un_note = getWidgets().get(219, 1, 1);
                if (un_note != null) {
                    un_note.interact();
                    log("Un-noting planks.. please wait...");
                    sleep(random(800, 1000));
                    getMouse().moveOutsideScreen();
                    sleep(random(7300, 7500));
                }
            } else {
                RS2Widget settings = getWidgets().get(548, 35);
                if (settings != null) {
                    settings.interact();
                    sleep(random(1000, 1500));
                    RS2Widget house = getWidgets().get(261, 99);
                    if (house != null) {
                        house.interact();
                        sleep(random(1000, 1500));
                        RS2Widget servant = getWidgets().get(370, 19, 0);
                        if (servant != null) {
                            servant.interact();
                            log("Calling servant...");
                            sleep(random(1000, 1500));
                            RS2Widget fetch = getWidgets().get(219, 1, 1);
                            if (fetch != null) {
                                fetch.interact();
                                log("Un-noting planks.. please wait...");
                                sleep(random(1500, 2000));
                                RS2Widget inven = getWidgets().get(548, 51);
                                if (inven != null) {
                                    inven.interact();
                                    sleep(random(800,1000));
                                    getMouse().moveOutsideScreen();
                                    log("Returning to inventory...");
                                    sleep(random(7300, 7500));

                                }
                            }
                        }
                    }
                }
            }
        }

        private void hoverlarderspace() {
            if (getInventory().getAmount("Oak plank") >= 8) {
                RS2Object hoverlarderspace = getObjects().closest("Larder space");
                if (hoverlarderspace != null) {
                    hoverlarderspace.hover();
                }
            }
        }

        @Override
    public void onStart() throws InterruptedException {
        log("Welcome to Last Larders.. let's get started.");
        if(!getInventory().contains("Oak plank")) {
            log("No Oak Planks found.. calling butler...");
            onstartCheck();
        }
    }

    @Override
    public void onExit(){
        log("Thanks for using Last Larders.. enjoy the EXP!");
    }

    @Override
    public int onLoop() throws InterruptedException {
        if(getInventory().getAmount("Oak plank") >= 8) {
            build();
                remove();
                hoverlarderspace();
            } else if(getInventory().getAmount("Oak plank") < 8) {
            butler();
        }


        return 2000;
    }
}
Edited by Imthabawse
Link to comment
Share on other sites

Code is a hell of a lot cleaner now and been running for awhile now looking good.  *Edited above code

Still want to add: Random camera movement(although not necessary just anti ban type feature), random checking of EXP gained widget that pops up when you build a larder, informative paint lvls gained, exp gained etc...

Been a fun project so far and have come a long way since I started dicking around with it.

Looking for feedback from other scripters or anyone that actually takes this code and uses it.

LOG: 

[INFO][Bot #1][02/24 05:16:57 PM]: Talking to butler..
[INFO][Bot #1][02/24 05:16:59 PM]: Un-noting planks.. please wait...
[INFO][Bot #1][02/24 05:17:09 PM]: Interacting with larder..
[INFO][Bot #1][02/24 05:17:10 PM]: Removing larder...
[INFO][Bot #1][02/24 05:17:12 PM]: Interacting with larder space..
[INFO][Bot #1][02/24 05:17:13 PM]: Building larder...
[INFO][Bot #1][02/24 05:17:17 PM]: Interacting with larder..

 

Edited by Imthabawse
Link to comment
Share on other sites

Re-edited code above trying to find a way to stop script after all oak planks are gone and butler says a certain phrase but when I try to implement it I get errors any suggestions?

Edit: I got the script to logout when out of planks but not sure it's full proof yet.

Snippet: 

if (!myPlayer().isMoving()) {
    log("Not enough planks to build.. logging out.");
    if (getInventory().getAmount("Oak plank") < 5) {
        stop();
Edited by Imthabawse
Link to comment
Share on other sites

On 2/10/2019 at 9:19 PM, toincow said:

Do not put everything in the onLoop(). If OSbot for whatever reason misclicks (lagg, disconnect, breaks) it will not be able to correct itself and continue finishing the onloop again and again. This will make your bot act strangely.

Make methods for each action the bot does.

As long as the code runs in the same thread there is no difference between containing everything in one method and splitting the actions into different smaller methods.

The only advantages for spitting code are increased modularity, readability and ease of maintenance. Otherwise the VM couldn't give less of a shit if the code is delivered in a single method instead of 10.

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