Jump to content

My Code Crashes


kingbutton

Recommended Posts

I'm learning how to interact with items in the inventory. Also trying to learn how to interact with items on the ground.

When I run the code, my OsBot freezes, and I have to task manager to close it. Here's what I have at the moment.

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(author = "Kingbutton", info = "Fire Cooking", logo = "", name = "LumbCook", version = 0)
public class Main extends Script {

	@Override
	public int onLoop() throws InterruptedException {

		if (fireCheck()) {
			log("hello bitch");
		} else {
			makeFire();

		}

		return 50;
	}

	public boolean fireCheck() {
		RS2Object fire = objects.closest("Fire");

		if (fire != null) {
			return true;
		} else {
			return false;
		}
	}

	public void makeFire() {

		if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {

			RS2Object log = objects.closest("Logs");

			if (log.interact("Light")) {
				new ConditionalSleep(random(600, 1200)) {

					@Override
					public boolean condition() throws InterruptedException {
						return myPlayer().isAnimating();
					}
				}.sleep();
			}
		}
	}

}

 

Link to comment
Share on other sites

11 minutes ago, Chris said:

VNxpXer.png

Not 100% what you meant by this, But I took my guess.

This is the change I made.

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(author = "Kingbutton", info = "Fire Cooking", logo = "", name = "LumbCook", version = 0)
public class Main extends Script {

	@Override
	public int onLoop() throws InterruptedException {

		makeFire();

		return 50;
	}

	public void makeFire() {

		RS2Object fire = objects.closest("Fire");
		RS2Object log = objects.closest("Logs");

		if (fire != null) {
			if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {
				if (log.interact("Light")) {
					new ConditionalSleep(random(600, 1200)) {

						@Override
						public boolean condition() throws InterruptedException {
							return myPlayer().isAnimating();
						}
					}.sleep();
				}
			}
		}

	}

}

It's not freezing anymore but my code isn't doing anything. So am I using the wrong api to interact with the log that's on the ground?

Edited by kingbutton
Link to comment
Share on other sites

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(author = "Kingbutton", info = "Fire Cooking", logo = "", name = "LumbCook", version = 0)
public class Main extends Script {

	@Override
	public int onLoop() throws InterruptedException {

		if (fireCheck()) {
			log("hello bitch");
		} else {
			makeFire();

		}

		return 50;
	}

	public boolean fireCheck() {
		RS2Object fire = objects.closest("Fire");

		return fire != null;
	}

	public void makeFire() {

		if (!myPlayer().isAnimating() && !myPlayer().isMoving()) {

			GroundItem log = getGroundItems().closest("Logs");

			if (log != null && log.interact("Light")) {
				new ConditionalSleep(3000) {

					@Override
					public boolean condition() throws InterruptedException {
						return myPlayer().isAnimating();
					}
				}.sleep();
			}
		}
	}

}

 

Link to comment
Share on other sites

3 hours ago, kingbutton said:

Ight thanks MAN!

Can you explain to me why in the fireCheck() method I have to return fire != null.

Like why can't i do it in the makeFire() method?

You can do it in the makeFire() method, For example something like this:

public void makeFire() {
		RS2Object fire = objects.closest("Fire");
		if (!myPlayer().isAnimating() && !myPlayer().isMoving() && fire != null) {

			GroundItem log = getGroundItems().closest("Logs");

			if (log != null && log.interact("Light")) {
				new ConditionalSleep(3000) {

					@Override
					public boolean condition() throws InterruptedException {
						return myPlayer().isAnimating();
					}
				}.sleep();
			}
		}
	}

It's mostly down to preference, however the way you had it before makes it clearer (to me) when you want the script to light a fire.

Also at the moment you're trying to interact with logs on the ground. If you want to interact with logs in your inventory do this:

Item log = getInventory().getItem("Logs");

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