Jump to content

First scripts made, How to use more than one class?


Lexhanatin

Recommended Posts

So I've made my first few scripts, basically just simple tasks all inside a main class.
How can I now call that main class inside another class?

For example:

main class - fills jugs, banks, repeat.

new class - timer class
If time < 1 hour
    main class
else
    end program

Code snippet if it helps.

import org.osbot.rs07.api.model.Character;
import org.osbot.rs07.api.model.Entity;
import org.osbot.rs07.script.Script;
import org.osbot.rs07.script.ScriptManifest;
import org.osbot.rs07.api.map.Position;
import org.osbot.rs07.api.model.NPC;

import java.util.Arrays;
import java.util.List;
import java.awt.*;

@ScriptManifest(author = "Lexhanatin", info = "My first script", name = "Jug Filler", version = 0, logo = "")
public class main extends Script {
		private Position[] path = {
			new Position(3253, 3426, 0), // Outside bank.
			new Position(3246, 3429, 0),
			new Position(3239, 3433, 0) // Fountain
		};
		
		private Position[] path2 = {
				new Position(3239, 3433, 0), // Fountain
				new Position(3246, 3429, 0),
				new Position(3253, 3426, 0) // Outside bank.
			};
		
		List toFill = Arrays.asList(path);
		List toBank = Arrays.asList(path2);
	
		@Override
		public void onStart() {
			log("Welcome to Jug Filler by Lexhanatin.");
			log("If you experience any issues while running this script please report them to me on the forums.");
			log("Enjoy the script!");
		}
	
		private enum State {
			FILL, BANK, WAIT
		};
	
		private State getState() {
			Entity fountain = objects.closest("Fountain");
			NPC banker = npcs.closest("Banker");
			
			if (inventory.getAmount("Jug") == 28 && fountain != null)
				return State.FILL;
			if ((banker != null) && inventory.getAmount("Jug of water") == 28 ||
				(banker != null) && inventory.isEmpty())
				return State.BANK;
			return State.WAIT;
		}
	
		@Override
		public int onLoop() throws InterruptedException {
			switch (getState()) {
			case FILL:
				getWalking().walkPath(toFill);
				sleep(500);
				
				Entity fountain = objects.closest("Fountain");
				if (inventory.getAmount("Jug") == 28 && fountain != null) {
					inventory.interact("Use", "Jug");
					sleep(random(80, 100));
					if (inventory.isItemSelected()) {
						fountain.interact("Use", "Jug -> Fountain");
						sleep(random(80, 100));
						
						Character me = myPlayer();
						if(myPlayer().isAnimating()) {
							sleep(5000);
						}
					}
				}
				sleep(random(5000, 10000));
				break;
			case BANK:
				if (!getBank().isOpen()){
				    getBank().open();
				} 
				if(inventory.isEmpty()){
					getBank().withdraw("Jug", 28);
					sleep(random(500, 800));
					getBank().close();
				 } else if (inventory.isFull()) {
				        getBank().depositAll();
				        sleep(random(500, 800));
				        getBank().withdraw("Jug", 28);
				        sleep(random(500, 800));
				        getBank().close();
				} else {
		        	getBank().close();
		        }
				break;
			case WAIT:
				sleep(random(500, 700));
				break;
			}
			return random(200, 300);
		}

	@Override
	public void onExit() {
		log("Thanks for running my Jug Filler!");
	}

	@Override
	public void onPaint(Graphics2D g) {

	}
} 

 

Link to comment
Share on other sites

//init somewhere in your main class
long startTime;
long playTime;
long breakTime;

public void onStart(){
    startTime = System.currentTimeMillis();
    playTime = TimeUnit.MINUTES.toMillis(60);
    breakTime = startTime + playTime;
}

public int loop(){ //your main loop
  if(breakTime < System.currentTimeMillis()){ //should break?
    log("It's been 60 minutes now, lets stop the script :)");
    stop();
  }else{ //if we shouldnt break
    //run the script as usual
  }
}

If you just want to stop the script after 1 hour you can do this.

 

I just wrote it out of my head right now (on my phone) so might be typos. Keep in mind that there are probably better ways of doing this, but it works.

Edited by Sumo
Link to comment
Share on other sites

To be honest, people suggest using this "task/node" based design but really it offers little to no benefit over a simple switch statement, apart from perhaps non-blocking checks. At least for the script that you've written here, it would just complicate things. For a script such as this one, there's no problem keeping things in one place. (Also i'm not really sure why people call it a 'node' structure in the first place?)

Instead I would focus on more important things like trying to understand exactly what you've written, for example:

					if (inventory.isItemSelected()) {
						fountain.interact("Use", "Jug -> Fountain");
						sleep(random(80, 100));
						
						Character me = myPlayer(); // <-- ??
						if(myPlayer().isAnimating()) {
							sleep(5000);
						}
					}

And also making your script as bullet-proof as possible. For example, what happens if you're standing by the fountain with 28 noted jugs in your inventory, or if both the fountain and banker in your getState() function are null? - Your script should always be in a situation where it can and will do something!

Cheers

-Apa

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