Lol_marcus Posted May 11, 2020 Share Posted May 11, 2020 (edited) 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 May 11, 2020 by Lol_marcus Quote Link to comment Share on other sites More sharing options...
ExtraBotz Posted May 11, 2020 Share Posted May 11, 2020 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. } 1 Quote Link to comment Share on other sites More sharing options...
progamerz Posted May 11, 2020 Share Posted May 11, 2020 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 } 1 Quote Link to comment Share on other sites More sharing options...
Lol_marcus Posted May 11, 2020 Author Share Posted May 11, 2020 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! I'll try to implement this, could be the solution I was looking for! Thanks Quote Link to comment Share on other sites More sharing options...