Jump to content

Stuck - script won't start


domp

Recommended Posts

Hey forums, 

currently trying to build my first script.  I decided I would attempt to make a script that teleports to each tree patch, cuts the tree, supercomposts, and plant the new sapling.  I am a beginner in my 8th week of Programming & Logic.  Please try not to burn me too much 

 

I am just asking for help on WHY my script won't start.  I am only so far enough to test one case teleporting to varrock and chopping the tree.  I haven't got to the supercomposts or planting a new tree.

Script is NOT complete obviously

Any help would be great

 
 
 

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import org.osbot.rs07.api.Inventory;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.ui.MagicSpell;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Spells;
import org.osbot.rs07.api.ui.Spells.NormalSpells;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.api.util.ExperienceTracker;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "Dp", info = "Tree Farming", logo = "@", name = "TP", version = 0.1)

public class TP extends Script {

	// learn to use areas
	public Position[] vpath = { new Position(3213, 3436, 0), new Position(3225, 3457, 0), new Position(3228, 3457, 0) };
	public Position[] gpath = { new Position(3218, 3451, 0), new Position(3206, 3454, 0), new Position(3199, 3459, 0),
			new Position(3196, 3468, 0), new Position(3196, 3485, 0), new Position(3186, 3494, 0),
			new Position(3186, 3505, 0) };
	public Position[] lpath = { new Position(3204, 3217, 0), new Position(3195, 3218, 0), new Position(3193, 3229, 0) };
	public Position[] tpath = { new Position(2966, 3388, 0), new Position(2963, 3402, 0), new Position(2957, 3417, 0),
			new Position(2949, 3430, 0), new Position(2946, 3444, 0), new Position(2938, 3451, 0),
			new Position(2936, 3441, 0) };
	public Position[] fpath = { new Position(2975, 3379, 0), new Position(2988, 3372, 0), new Position(2998, 3374, 0) };
	//

	final String WILLOW_TREE = "Willow Tree";
	final String WILLOW_LOG = "Willow logs";
	// final Area BANK_AREA = new Area(x,y,topleft,topright);
	Player player = myPlayer();
	Inventory inven = getInventory();
	private ExperienceTracker expTracker;
	private long start;
	State state1 = getState();

	public void onStart() {
		log("starting tree farming!!");

		this.expTracker = getExperienceTracker();
		this.expTracker.start(Skill.FARMING);
		this.start = System.currentTimeMillis();
	}

	private static enum State {
		Varrock, Gnome, Lumby, Falador, Taverly;

		public String toString() {
			String s = name().toLowerCase().replaceAll("_", " ");
			return s.substring(0, 1).toUpperCase().concat(s.substring(1, s.length()));
		}
	}

	// if(player.getPosition() == new Position(3228, 3457, 0)) <----could work
	// for a check
	// if player position = last location
	// teleport to next tree patch return.nextstate
	int check;

	private State getState() {

		// need to fix this to correctly return states...
		if (getInventory().contains(new int[] { 1359, 952, 12791 })) {
			check++;
			return State.Varrock;

		}
		if (check == 1) {
			// or use a check on the amount of empty pots in
			check++; // invent
			return State.Gnome;
		}
		if (check == 2) {

			check++;
			return State.Lumby;
		}
		if (check == 3) { // change int x

			check++;
			return State.Falador;
		}

		return State.Taverly;

	}

	// code in loop
	Entity willow;

	public int onLoop() throws InterruptedException {

		State result = getState();
		log(result);
		switch (result) {

		case Varrock:
			willow = closestObjectForName(WILLOW_TREE); // fixed
			/*
			 * if(!open(Tab.MAGIC)){ open(Tab.MAGIC);
			 * castSpell(Spells.NormalSpells.VARROCK_TELEPORT);// tele
			 * sleep(random(25, 100)); getWalking().webWalk(vpath); }
			 */
			if (willow != null) {
				if (willow.isVisible()) { // basic woodcutter
					if (!player.isAnimating()) { // for future use
						if (!player.isMoving()) {
							willow.interact("Check-health");
							sleep(random(500, 700));
							willow.interact("Chop down");
							sleep(random(25, 75));
						} else {
							if (!willow.isVisible()) {
								if (!player.isAnimating()) {
									if (!getInventory().isFull()) {// add a full
																	// inv
																	// check...
																	// drop
																	// willows
																	// if
																	// inv
																	// reaches
																	// full
										objects.closest("Tree stump").interact("Dig");
									} else {
										objects.closest("Dead tree").interact("Dig");
										sleep(random(500, 700));
										getInventory().dropForNameThatContains("willow");
									}
									if (getInventory().isFull()) {
										getInventory().dropForNameThatContains("willow");
										getInventory().dropForNameThatContains("Empty plant pot");
									} else {
										castSpell(Spells.NormalSpells.VARROCK_TELEPORT);
										sleep(random(25, 100));
										getWalking().webWalk(vpath);
										getCamera().toEntity(willow);
										log(check + "varrock");
									}
								}
							}
						}
					}
				}
			}
			// code using supercompost and planting tree. look up using mouse
			// entity with entity

			break;

		case Gnome:
			// tele
			Entity spirit = closestObjectForName("Spirit tree");

			getWalking().webWalk(gpath);
			getCamera().toEntity(spirit);

			objects.closest("Spirit tree").interact("Travel");

			break;

		case Lumby:
			if (!open(Tab.MAGIC)) {
				open(Tab.MAGIC);
				castSpell(Spells.NormalSpells.LUMBRIDGE_TELEPORT);
				getWalking().webWalk(lpath);
			}

			log(check + "lumbridge");
			break;

		case Falador:
			// tele
			if (!open(Tab.MAGIC)) {
				castSpell(Spells.NormalSpells.FALADOR_TELEPORT);
				getWalking().webWalk(fpath);
			}

			log(check + "fally");
			break;

		case Taverly:
			// tele
			if (!open(Tab.MAGIC)) {
				castSpell(Spells.NormalSpells.FALADOR_TELEPORT);
				getWalking().webWalk(tpath);
			}

			log(check + "taverly");
			break;

		default:
			return (random(500, 1000));

		}
		return (random(500, 1000));
	}

	private boolean open(Tab magic) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unused")
	private void castSpell(MagicSpell spell) {
		// TODO Auto-generated method stub
		NormalSpells vartele = Spells.NormalSpells.VARROCK_TELEPORT;

	}

	private Entity closestObjectForName(String willow_tree2) {
		// TODO Auto-generated method stub
		return null;
	}

	public final boolean toEntity(Entity entity) {

		return false;
	}

	// code executed at the end
	public void onExit() {
		log("Done farming");

	}

	public void onPaint(Graphics2D graphics) {
		Graphics2D g = graphics;
		g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		if (this.expTracker != null) {
			g.setColor(Color.WHITE);
			g.drawString("Time till level " + (getSkills().getStatic(Skill.FARMING) + 1) + ": "
					+ formatTime(this.expTracker.getTimeToLevel(Skill.FARMING)), 10, 250);
			g.drawString("Exp: " + this.expTracker.getGainedXP(Skill.FARMING) + " ("
					+ this.expTracker.getGainedXPPerHour(Skill.FARMING) + " / hour)", 10, 270);
			g.drawString("Level: " + getSkills().getStatic(Skill.FARMING) + " (Gained: "
					+ this.expTracker.getGainedLevels(Skill.FARMING) + ")", 10, 290);
		}
		g.setColor(Color.CYAN);
		g.drawString("Status: " + state1.toString(), 10, 310);
		g.drawString("Runtime: " + (this.start == -1L ? "Idle" : formatTime(System.currentTimeMillis() - this.start)),
				10, 330);
	}

	private String formatTime(long ms) {
		long s = ms / 1000L;
		long m = s / 60L;
		long h = m / 60L;
		s %= 60L;
		m %= 60L;
		h %= 24L;
		return String.format("%02d:%02d:%02d", new Object[] { Long.valueOf(h), Long.valueOf(m), Long.valueOf(s) });
	}

}

 

Edited by domp
Link to comment
Share on other sites

        
	public void onStart() {
	}

	public int onLoop() throws InterruptedException {
		return (random(500, 1000));
	}
	
	public void onExit() {

	}

	public void onPaint(Graphics2D graphics) {
		
	}

 

All require an @Override (forums "tagging" system screw up what i'm trying to write >_>)

like

deead0baae714396ac4e0247b4f9543d.png

Edited by Isolate
Link to comment
Share on other sites

	Player player = myPlayer();
	Inventory inven = getInventory();

you can't make references to script before onStart() and this implicitly use a reference to Script using the 'this' pointer. These lines are the problem.

 

All require an @Override (forums "tagging" system screw up what i'm trying to write >_>)

like

deead0baae714396ac4e0247b4f9543d.png

 

 

Added @override and no change, logger still shows nothing happened

 

Overrides are only annotations and only make a difference for the programmer - not the compiler.

Edited by Imateamcape
Link to comment
Share on other sites

	Player player = myPlayer();
	Inventory inven = getInventory();

you can't make references to script before onStart() and this implicitly use a reference to Script using the 'this' pointer. These lines are the problem.

 

 

 

 

Overrides are only annotations and only make a difference for the programmer - not the compiler.

 

 I deleted both those variables and replaced all references with actual values.  Script still not working.  Changed a few things, still not working

Link to comment
Share on other sites

can you copy and paste your new code?


import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.RenderingHints;

import org.osbot.rs07.api.Inventory;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.api.model.Player;
import org.osbot.rs07.api.ui.MagicSpell;
import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Spells;
import org.osbot.rs07.api.ui.Spells.NormalSpells;
import org.osbot.rs07.api.ui.Tab;
import org.osbot.rs07.api.util.ExperienceTracker;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;

@ScriptManifest(author = "Dp", info = "Tree Farming", logo = "@", name = "TP", version = 0.1)

public class TP extends Script {

	// learn to use areas
	public Position[] vpath = { new Position(3213, 3436, 0), new Position(3225, 3457, 0), new Position(3228, 3457, 0) };
	public Position[] gpath = { new Position(3218, 3451, 0), new Position(3206, 3454, 0), new Position(3199, 3459, 0),
			new Position(3196, 3468, 0), new Position(3196, 3485, 0), new Position(3186, 3494, 0),
			new Position(3186, 3505, 0) };
	public Position[] lpath = { new Position(3204, 3217, 0), new Position(3195, 3218, 0), new Position(3193, 3229, 0) };
	public Position[] tpath = { new Position(2966, 3388, 0), new Position(2963, 3402, 0), new Position(2957, 3417, 0),
			new Position(2949, 3430, 0), new Position(2946, 3444, 0), new Position(2938, 3451, 0),
			new Position(2936, 3441, 0) };
	public Position[] fpath = { new Position(2975, 3379, 0), new Position(2988, 3372, 0), new Position(2998, 3374, 0) };
	//

	final String WILLOW_TREE = "Willow Tree";
	final String WILLOW_LOG = "Willow logs";
	// final Area BANK_AREA = new Area(x,y,topleft,topright);
	private ExperienceTracker expTracker;
	private long start;
	
	
	@[member=Override]
	public void onStart() {
		log("starting tree farming!!");

		this.expTracker = getExperienceTracker();
		this.expTracker.start(Skill.FARMING);
		this.start = System.currentTimeMillis();
	}

	private static enum State {
		Varrock, Gnome, Lumby, Falador, Taverly;

		public String toString() {
			String s = name().toLowerCase().replaceAll("_", " ");
			return s.substring(0, 1).toUpperCase().concat(s.substring(1, s.length()));
		}
	}

	// if(player.getPosition() == new Position(3228, 3457, 0)) <----could work
	// for a check
	// if player position = last location
	// teleport to next tree patch return.nextstate
	State state1 = getState();
	int check;

	private State getState() {

		// need to fix this to correctly return states...
		if (getInventory().contains(new String[] { "Rune axe" })) {
			check++;
			return State.Varrock;

		}
		if (check == 1) {
			// or use a check on the amount of empty pots in
			check++; // invent
			return State.Gnome;
		}
		if (check == 2) {

			check++;
			return State.Lumby;
		}
		if (check == 3) { // change int x

			check++;
			return State.Taverly;
		}

		return State.Falador;

	}

	// code in loop
	Entity willow;
	@[member=Override]
	public int onLoop() throws InterruptedException {

		State result = getState();
		log(result);
		switch (result) {

		case Varrock:
			willow = closestObjectForName(WILLOW_TREE);
			if (getInventory().isFull()) {
				getInventory().dropForNameThatContains("willow");
				getInventory().dropForNameThatContains("Empty plant pot");
			} else {
				castSpell(Spells.NormalSpells.VARROCK_TELEPORT);
				sleep(random(25, 100));
				getWalking().webWalk(vpath);
				getCamera().toEntity(willow);
				log(check + "varrock");
			/*
			 * if(!open(Tab.MAGIC)){ open(Tab.MAGIC);
			 * castSpell(Spells.NormalSpells.VARROCK_TELEPORT);// tele
			 * sleep(random(25, 100)); getWalking().webWalk(vpath); }
			 */
			if (willow != null) {
				if (willow.isVisible()) { // basic woodcutter
					if (!this.myPlayer().isAnimating()) {
						if (!getInventory().isFull()) {// for future use
						if (!this.myPlayer().isMoving()) {
							willow.interact("Check-health");
							sleep(random(500, 700));
							willow.interact("Chop down");
							sleep(random(25, 75));
						} else if (!willow.isVisible()) {
								if (!this.myPlayer().isAnimating()) {
									if (!getInventory().isFull()) {// add a full
																	// inv
																	// check...
																	// drop
																	// willows
																	// if
																	// inv
																	// reaches
																	// full
										objects.closest("Tree stump").interact("Dig");
									} else {
										objects.closest("Dead tree").interact("Dig");
										sleep(random(500, 700));
										getInventory().dropForNameThatContains("willow");
									}

									}
								}
							}
						}
					}
				}
			}
			// code using supercompost and planting tree. look up using mouse
			// entity with entity

			break;

		case Gnome:
			// tele
			Entity spirit = closestObjectForName("Spirit tree");

			getWalking().webWalk(gpath);
			getCamera().toEntity(spirit);

			objects.closest("Spirit tree").interact("Travel");

			break;

		case Lumby:
		{
			if (!open(Tab.MAGIC)) {
				open(Tab.MAGIC);
				castSpell(Spells.NormalSpells.LUMBRIDGE_TELEPORT);
				getWalking().webWalk(lpath);
			}

			log(check + "lumbridge");
		}
			break;

		case Falador:
		{
			// tele
			if (!open(Tab.MAGIC)) {
				castSpell(Spells.NormalSpells.FALADOR_TELEPORT);
				getWalking().webWalk(fpath);
			}

			log(check + "fally");
		}
			break;

		case Taverly:
		{
			// tele
			if (!open(Tab.MAGIC)) {
				castSpell(Spells.NormalSpells.FALADOR_TELEPORT);
				getWalking().webWalk(tpath);
			}

			log(check + "taverly");
		}
			break;

default:
	
	}
		return random(600);
	}

	private boolean open(Tab magic) {
		// TODO Auto-generated method stub
		return false;
	}

	@SuppressWarnings("unused")
	private void castSpell(MagicSpell spell) {
		// TODO Auto-generated method stub
		NormalSpells vartele = Spells.NormalSpells.VARROCK_TELEPORT;

	}

	private Entity closestObjectForName(String willow_tree2) {
		// TODO Auto-generated method stub
		return null;
	}

	public final boolean toEntity(Entity entity) {

		return false;
	}

	// code executed at the end
	@[member=Override]
	public void onExit() {
		log("Done farming");

	}
	@[member=Override]
	public void onPaint(Graphics2D graphics) {
		Graphics2D g = graphics;
		g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		if (this.expTracker != null) {
			g.setColor(Color.WHITE);
			g.drawString("Time till level " + (getSkills().getStatic(Skill.FARMING) + 1) + ": "
					+ formatTime(this.expTracker.getTimeToLevel(Skill.FARMING)), 10, 250);
			g.drawString("Exp: " + this.expTracker.getGainedXP(Skill.FARMING) + " ("
					+ this.expTracker.getGainedXPPerHour(Skill.FARMING) + " / hour)", 10, 270);
			g.drawString("Level: " + getSkills().getStatic(Skill.FARMING) + " (Gained: "
					+ this.expTracker.getGainedLevels(Skill.FARMING) + ")", 10, 290);
		}
		g.setColor(Color.CYAN);
		g.drawString("Status: " + state1.toString(), 10, 310);
		g.drawString("Runtime: " + (this.start == -1L ? "Idle" : formatTime(System.currentTimeMillis() - this.start)),
				10, 330);
	}

	private String formatTime(long ms) {
		long s = ms / 1000L;
		long m = s / 60L;
		long h = m / 60L;
		s %= 60L;
		m %= 60L;
		h %= 24L;
		return String.format("%02d:%02d:%02d", new Object[] { Long.valueOf(h), Long.valueOf(m), Long.valueOf(s) });
	}

}
Link to comment
Share on other sites

You have a lot of dangerous code in there, it could be just about anything.

 

For starters:

int check;

always returns Falador after check > 3, and it's never reset. I'm assuming you wanted your logic to cycle through the farming patches?
 

Anyways, your second state is Varrock (which is should be reaching). You call:
 

willow = closestObjectForName(WILLOW_TREE);

That method returns null every time:

 

private Entity closestObjectForName(String willow_tree2) {
// TODO Auto-generated method stub
return null;
}

So either your script throws an NPE and crashes at:

getCamera().toEntity(willow);

or your null check for the willow tree later down prevents your script from doing anything.

Did you even write this script?

Link to comment
Share on other sites

You have a lot of dangerous code in there, it could be just about anything.

 

For starters:

int check;

always returns Falador after check > 3, and it's never reset. I'm assuming you wanted your logic to cycle through the farming patches?

 

Anyways, your second state is Varrock (which is should be reaching). You call:

 

willow = closestObjectForName(WILLOW_TREE);

That method returns null every time:

 

private Entity closestObjectForName(String willow_tree2) {
// TODO Auto-generated method stub
return null;
}

So either your script throws an NPE and crashes at:

getCamera().toEntity(willow);

or your null check for the willow tree later down prevents your script from doing anything.

Did you even write this script?

Lol yes I wrote this script, I am pretty much self-taught and know very little.  I am fixing what you suggested at the moment.  

I also purposely wanted falador to return if check is greater than 3 because it is the last case and I dont think it really needs a check.

 

Thank you for spotting the return null;

I am also working on seeing if it is an NPE

Link to comment
Share on other sites

you call getState() before onStart() - all fields, regardless of where they are in code, are initialized first. getState() has a reference to Script, meaning that it won't function. Also, @@Alek is right - you should look over this code a few more times before trying to run it, or else you're probably going to crash your client a few times

Edited by Alek
  • Like 2
Link to comment
Share on other sites

you call getState() before onStart() - all fields, regardless of where they are in code, are initialized first. getState() has a reference to Script, meaning that it won't function. Also, @@Alek is right - you should look over this code a few more times before trying to run it, or else you're probably going to crash your client a few times

	@[member=Override]
	public void onPaint(Graphics2D graphics) {
		State state1 = getState();
		Graphics2D g = graphics;
		g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		if (this.expTracker != null) {
			g.setColor(Color.WHITE);
			g.drawString("Time till level " + (getSkills().getStatic(Skill.FARMING) + 1) + ": "
					+ formatTime(this.expTracker.getTimeToLevel(Skill.FARMING)), 10, 250);
			g.drawString("Exp: " + this.expTracker.getGainedXP(Skill.FARMING) + " ("
					+ this.expTracker.getGainedXPPerHour(Skill.FARMING) + " / hour)", 10, 270);
			g.drawString("Level: " + getSkills().getStatic(Skill.FARMING) + " (Gained: "
					+ this.expTracker.getGainedLevels(Skill.FARMING) + ")", 10, 290);
		}
		g.setColor(Color.CYAN);
		g.drawString("Status: " + state1.toString(), 10, 310);
		g.drawString("Runtime: " + (this.start == -1L ? "Idle" : formatTime(System.currentTimeMillis() - this.start)),
				10, 330);
	}

I put State state1 = getState(); in the paint method where it is going to be used instead.

	private Entity closestObjectForName(String wILLOW_TREE2) {
		// TODO Auto-generated method stub
return null;
	}

Any help with this? Im not sure on what to properly configure for null

Link to comment
Share on other sites

	@[member='Override']
	public void onPaint(Graphics2D graphics) {
		State state1 = getState();
		Graphics2D g = graphics;
		g.setRenderingHints(new RenderingHints(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_OFF));
		if (this.expTracker != null) {
			g.setColor(Color.WHITE);
			g.drawString("Time till level " + (getSkills().getStatic(Skill.FARMING) + 1) + ": "
					+ formatTime(this.expTracker.getTimeToLevel(Skill.FARMING)), 10, 250);
			g.drawString("Exp: " + this.expTracker.getGainedXP(Skill.FARMING) + " ("
					+ this.expTracker.getGainedXPPerHour(Skill.FARMING) + " / hour)", 10, 270);
			g.drawString("Level: " + getSkills().getStatic(Skill.FARMING) + " (Gained: "
					+ this.expTracker.getGainedLevels(Skill.FARMING) + ")", 10, 290);
		}
		g.setColor(Color.CYAN);
		g.drawString("Status: " + state1.toString(), 10, 310);
		g.drawString("Runtime: " + (this.start == -1L ? "Idle" : formatTime(System.currentTimeMillis() - this.start)),
				10, 330);
	}

I put State state1 = getState(); in the paint method where it is going to be used instead.

	private Entity closestObjectForName(String wILLOW_TREE2) {
		// TODO Auto-generated method stub
return null;
	}

Any help with this? Im not sure on what to properly configure for null

 

 

Entity x = getClosestObjectForName("Tree");

if(x != null) {

   //do stuff

}

 

That will always return a null, though, you're aware of that, right? you need to implement the code. Also why would you put your states in onPaint() if it doesn't ever use the variable?

Link to comment
Share on other sites

Entity x = getClosestObjectForName("Tree");

if(x != null) {

   //do stuff

}

 

That will always return a null, though, you're aware of that, right? you need to implement the code. Also why would you put your states in onPaint() if it doesn't ever use the variable?

state1 is used to draw the current state instead of using the state I used for my switch, for some reason the first state I used to grab getState(). isnt applicable when I try to draw it.

 

 

Maybe I need a lesson on nulls as I am confuzzzzled.

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