Jump to content

[Script Help Please] Construction script


Lol_marcus

Recommended Posts

Hi all,

Questions:

  • The script runs fine if I start it with the Larder space is empty, but if I start it with something in the larder space, it doesn't do anything. Why?
  • Do I have to declare the RS2Objects over and over again, or will it recognize if I declare it once at the beginning of the script?
package core;

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;


import java.awt.*;


@ScriptManifest(name = "Construction Lite", version = 1, author = "Marcus", logo = "", info = "Creates oak larders")


public class Main extends Script {
	

    @Override
    public void onStart() throws InterruptedException {
        getExperienceTracker().start(Skill.CONSTRUCTION);
    }
    
    

    @Override
    public int onLoop() throws InterruptedException {

        if (hasPlanks()) {
            makeLarder();
        } else if (hasLarder()) {
            removeLarder(); 
        }
        return 700;
    }

    public boolean hasPlanks() {
        return (inventory.contains(8794,2347,995,8779) &&
        	   (inventory.contains(8778) && inventory.getAmount(8778) >= 8));
    }

    public void makeLarder() throws InterruptedException {
    	
		RS2Object larder = getObjects().closest("Larder space");

        if  (larder != null); {
        	 larder.interact("Build");
        	 sleep(random(1600, 2000));
        	 keyboard.typeString("2");
        	 sleep(random(750, 900));
            new ConditionalSleep(1800, 100) {
                @Override
                public boolean condition() throws InterruptedException {
                    return getInventory().isEmptyExcept(8794,2347,995,8779);
                }
            }.sleep();
        }
    }
    
    public boolean hasLarder() {
    	
		RS2Object oaklarder = getObjects().closest("Larder");
		
        return (oaklarder != null);
    }

    public void removeLarder() throws InterruptedException {
    	
		RS2Object oaklarder = getObjects().closest("Larder");

        if  (oaklarder != null); {
        	 oaklarder.interact("Remove");
        	 sleep(random(1600, 2000));
        	 keyboard.typeString("1");
        	 sleep(random(750, 900));
        }
    }
    

    @Override
    public void onPaint(Graphics2D paint) {
        int	cXp = getExperienceTracker().getGainedXP(Skill.CONSTRUCTION);
        int cXpph = getExperienceTracker().getGainedXPPerHour(Skill.CONSTRUCTION);



        super.onPaint(paint);
        paint.drawString("Construction XP: " + cXp, 387, 328);
        paint.drawString("Construction XP/ph: " + cXpph, 387, 313);

    }
}

 

Edited by Lol_marcus
Link to comment
Share on other sites

Quote

The script runs fine if I start it with the Larder space is empty, but if I start it with something in the larder space, it doesn't do anything. Why?

I do not understand your question. What do you mean by "start it with the larder space empty". You need to be more specific when posting large sections of code.

Quote

Do I have to declare the RS2Objects over and over again, or will it recognize if I declare it once at the beginning of the script?

Typically an object should be initialized only within the scope that is needed. You should look into Java Variable Scope which will further explain when to declare variables and how their access works.

Lastly, I noticed you're using methods that return a boolean but never check the return value. You should put methods with return type boolean into an if else in-case they fail.

if(oaklarder.interact("Remove")) {
	// the interaction was sucessfull and now we should do a CONDITIONAL sleep
} else {
	// you don't neccesarily need an else, but you can do error checking here!
	// if you don't use the else the loop will just reiterate and hopefully the interaction will be successful.
}

 

  • Like 1
Link to comment
Share on other sites

3 hours ago, ExtraBotz said:

I do not understand your question. What do you mean by "start it with the larder space empty". You need to be more specific when posting large sections of code.

Typically an object should be initialized only within the scope that is needed. You should look into Java Variable Scope which will further explain when to declare variables and how their access works.

Lastly, I noticed you're using methods that return a boolean but never check the return value. You should put methods with return type boolean into an if else in-case they fail.


if(oaklarder.interact("Remove")) {
	// the interaction was sucessfull and now we should do a CONDITIONAL sleep
} else {
	// you don't neccesarily need an else, but you can do error checking here!
	// if you don't use the else the loop will just reiterate and hopefully the interaction will be successful.
}

 

Let me try to explain better.

So, if the first statement isn't being met, it's shutting down. What I can't figure out is, if the larder has already been made, why isn't it going to the "else" statement? This would happen if for example someone started the script with the larder already made in the house, does that make sense?

    public int onLoop() throws InterruptedException {

        if (hasPlanks()) {
            makeLarder();
        } else if (hasLarder()) {
            removeLarder(); 
24 minutes ago, progamerz said:

RS2Object larder = getObjects().closest(o -> o.getName().equals("Object name here"));
if (larder != null)
    if (larder.hasAction("Build"){
        //add your building code here
    } else if (larder.hasAction("Remove"){
        //add your remove larder code here
    }

Oh wow. I didn't even know there was that function! :o I'll try to implement this, could be the solution I was looking for! Thanks

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