Jump to content

Simple Clay Miner


Imthabawse
 Share

Recommended Posts

Made a simple clay miner that mines clay and banks. Running into issues in the onLoop() when using else if's and else. To me when I read it, it makes sense to me but the bot just sits there. When I remove else if's and else works fine. Confused ?‍♂️

 

Any suggestions ??‍♂️

Code I have now: 

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;


@ScriptManifest(name = "Clay", logo = "", version = 1, author = "Imthabawse", info = "Mines clay and banks")


public class Clay extends Script {

    private Area bankArea = new Area(3185, 3437, 3184, 3436);

    private Area clayArea = new Area(3179, 3370, 3181, 3372);

    private void bank() {
        RS2Object bankbooth = getObjects().closest("Bank booth");
        if (bankbooth != null) {
            bankbooth.interact("Bank");
            log("Banking..");
            new ConditionalSleep(2500) {
                @Override
                public boolean condition() {
                    return bank.isOpen();
                }
            }.sleep();
        }
        if (bank.isOpen()) {
            bank.depositAll();
            log("Depositing clay..");
        }
    }


    private void mineClay() {
        RS2Object clay = getObjects().closest(7454, 7487);
        if (!myPlayer().isAnimating()) {
            if (!getInventory().isFull())
                if (clay != null) {
                    clay.interact("Mine");
                    log("Mining clay..");
                    new ConditionalSleep(2500) {
                        @Override
                        public boolean condition() {
                            return clay.exists() && !getInventory().isFull();
                        }
                    }.sleep();
                }
        }
    }


    @Override
    public int onLoop() {
        if (clayArea.contains(myPlayer())) {
            mineClay();
        } if (!clayArea.contains(myPlayer())) { * had an else if here
            getWalking().webWalk(clayArea);
            mineClay();
*had an else here
    } if (getInventory().isFull()) {
                if (bankArea.contains(myPlayer())) {
                    bank();
                }  if (!bankArea.contains(myPlayer())) { *had an else if here
                    getWalking().webWalk(bankArea);
                    bank();
                }
            }

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

I'm not quite sure what your question is haha. Assuming you want it to be neater? The reason your else if's weren't working is likely due to how you had your code ordered.

In regards to neatening it, why not simplify it a little?

if (Inventory.isFull()){ // we can't mine, so bank
	bank();
} else { // all good, we can mine
	mine();
}

private void bank() {
	// bank code
}

private void mine() {
	if !area.contains(player) {
		walk to the area!
	} else {
		we can mine, put mine code here
	}
}

There's obviously much nicer ways than this, but this would be a good starting point for you :)

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

@HeyImJamie Implemented you're suggestion into another project simple iron miner. Thanks again for the feedback!

 

Code: 

private void bank() {
        if (Banks.VARROCK_EAST.contains(myPlayer())) {
            RS2Object banker = getObjects().closest("Bank booth");
            log("In bank.. getting closest banker.");
            if (banker != null) {
                banker.interact("Bank");
                log("Banking..");
                new ConditionalSleep(2000) {
                    @Override
                    public boolean condition() {
                        return bank.isOpen() && getInventory().isEmpty();
                    }
                }.sleep();
            }
            if (bank.isOpen()) {
                bank.depositAll();
            }
        } else {
            if (!Banks.VARROCK_EAST.contains(myPlayer())) {
                getWalking().webWalk(Banks.VARROCK_EAST);
            log("Not in bank.. walking there.");
            }
        }
    }



    private void mineIron() {
        if (ironArea.contains(myPlayer())) {
            if (!myPlayer().isAnimating()) {
                if (!myPlayer().isMoving()) {
                    if (!getInventory().isFull()) {
                        RS2Object ironore = getObjects().closest(7488);
                        log("Getting closest iron ore.");
                        if (ironore != null) {
                            ironore.interact("Mine");
                            log("Mining..");
                            new ConditionalSleep(2000) {
                                @Override
                                public boolean condition() {
                                    return ironore.exists() && !myPlayer().isAnimating();
                                }
                            }.sleep();
                        }
                    }
                }
            }
        }else{
            if(!ironArea.contains(myPlayer())) {
                getWalking().webWalk(ironArea);
            log("Walking to mining area..");
            }
        }
    }



    @Override
    public int onLoop() {
        if(getInventory().isFull()) {
            bank();
        }else{
            mineIron();
        }
        return 1500;
    }
}
Link to comment
Share on other sites

3 minutes ago, HeyImJamie said:

Awesome! Now I'd suggest you start looking at other things, such as better checks on the booleans available to you, and maybe neatening it even more?

For example: You can use && to check for multiple conditions in one line!

Yes I know about the && (and) just like to write it out if's{ the first time cause I'm weird. ?

Link to comment
Share on other sites

3 hours ago, Imthabawse said:

my question was why the else if's and else wasn't working the way it should (well in my head anyway :P)

I recreated your code based on your comments

	@Override
	public int onLoop() {
		if (clayArea.contains(myPlayer())) {
		
		} else if (!clayArea.contains(myPlayer())) {

		} else {
		this will never be executed
		}
		return 1500;
	}

You can either be in the area or not. There is no third option, so the last else block will never get executed.

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

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.

 Share

  • Recently Browsing   0 members

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