Jump to content

OSBot2 Event (Node) Driven Script Skeleto


exuals

Recommended Posts

Since there is a tutorial for the legacy I figured I'd throw up what I've been using to develop OSB2 scripts.

 

So, before I copy and paste code, an event is similar to a 'node' with the addition of useful features such as;

 

  • EventMode
  • EventStatus

EventMode determines when to run the code in the event. With concurrency changes in OSB2 you can run events in 'ASYNC' mode, AKA another thread that runs this event in sync with another already running event.

 

For example say you have an autofighter, you can start a new event in async mode that checks prayer points and when low; drinks a dose of prayer restore. 

 

You can also set an event to run in BLOCKING mode, simply stating that when the current event is finished, run this one next.

 

 

EventStatus determines what the event is currently doing, this is useful for managing multiple events.

 

EventStatus can be: 

  • Queued
  • Working
  • Finished
  • Failed

Back to the autofighter example, some of the events could be:

 

  • AttackMonster, this can be run in BLOCKING mode as we don't want to fight multiple monsters. When this event is executed, if there is another event currently running, it will be queued. When this event is Working it will be in combat with a monster, when the event is Finished, the monster should be dead and Failed would be if the monster is unable to be killed or killed us. This is incredibly useful as there is functionality to run code while event is queued, hence, if you queued an AttackMonster event it could right click the monster and wait for the current to be killed.
  •  
  • EatFood, this would be run in ASYNC mode as we want to eat when we are low and not wait for the current event to finish. EatFood can be queued to indicate that you want to eat 3 or 4 pieces of food in a row. When the event is finished, determine that our health increased to complete the event.

 

 

Now, our core script class:

import java.awt.Graphics2D;
import java.util.ArrayList;

import org.osbot.script.Script;
import org.osbot.script.event.Event;

import Events.EventTest;

public class TestScript extends Script {
	private ArrayList<Event> allEvents = new ArrayList<Event>();
	
	@Override
	public int onLoop() throws InterruptedException {
		for (Event e : allEvents) {
			execute(e);
		}
		return 1000;
	}

	@Override
	public void onStart() throws InterruptedException {
		allEvents.add(new EventTest());
		super.onStart();
	}
}

Looks similar to the node framework right?

 

Simple enough to read, we have an ArrayList of all of our events , onStart we add what events we want to use.

 

During the onLoop for this example we simply execute all of the events in the arraylist.

 

Note: If you have events that run in async make sure to place them before non async events when adding to the arraylist.

 

 

And here is an example event:

package Events;

import org.osbot.legacy.script.MethodProvider;
import org.osbot.rs07.api.model.NPC;
import org.osbot.script.event.Event;

public class FishSharks extends Event {
	private static final String FISHING = "Fishing";
	private static final String HARPOON = "Harpoon";
	private static final int _618 = 618;
	private static final int _2906 = 2906;
	
	@Override
	public int execute() throws InterruptedException {
		if (inventory.isFull())
			setFinished();
		
		if (!(myPlayer().getAnimation() == _618))
			startFishing();
			
		return 0;
	}

	private void startFishing() throws InterruptedException {
		NPC fishingspot = npcs.closest(_2906);
		
		if (fishingspot.exists()) {
			camera.toEntity(fishingspot);
			fishingspot.interact(HARPOON);
			sleep(MethodProvider.gRandom(1000, 200));
		}
	}

	@Override
	public String toString() {
		return FISHING;
	}
}

This event simply catches sharks if it is not animating.

Since the event hasn't stated a EventMode it is automatically set to BLOCKING.

 

Here is an example of a ASYNC event;

package Events;

import org.osbot.rs07.api.ui.Skill;
import org.osbot.rs07.api.ui.Spell;
import org.osbot.script.MethodProvider;
import org.osbot.script.event.Event;


public class AntiBan extends Event {

	private static final String ANTIBAN = "Antiban";

	public AntiBan() {
		setAsync();
	}
	
	@Override
	public int execute() throws InterruptedException {
		antiBan();
		return 0;
	}

	private void antiBan() throws IllegalArgumentException, InterruptedException {
		int rand = MethodProvider.random(0, 4);
		
		switch (rand) {
		case 0:
			antiBan.hoverSkill(Skill.FISHING);
			break;
			
		case 1:
			antiBan.hoverSpell(Spell.HOME_TELEPORT);
			break;
			
		case 2:
			antiBan.moveMouseOutsideScreen();
			break;
			
		case 3:
			camera.movePitch(99);
			break;
		}
		
		sleep(MethodProvider.gRandom(6000, 6000));
	}

	@Override
	public String toString() {
		return ANTIBAN;
	}
}

Because this Event is run in ASYNC (Notice we set this in the constructor) the antiban will run concurrently with the other two events.

 

And finally, the core of this example fishing script:

import java.awt.Color;
import java.awt.Graphics2D;
import java.util.ArrayList;
import java.util.Collections;

import org.osbot.script.Script;
import org.osbot.script.ScriptManifest;
import org.osbot.script.event.Event;

import Events.AntiBan;
import Events.BankSharks;
import Events.FishSharks;

@ScriptManifest(author = "exuals", info = "I don't even", logo = "what", name = "exFishy", version = 0)
public class exSharksAPI2 extends Script {
	private ArrayList<Event> allEvents = new ArrayList<Event>();
	
	@Override
	public int onLoop() throws InterruptedException {
		for (Event e : allEvents) {
			execute(e);
		}
		return 1000;
	}

	@Override
	public void onPaint(Graphics2D arg0) {
		super.onPaint(arg0);
		
		int x, y, width, height;
		
		x = 20;
		y = 50;
		width = 100;
		height = 25;
		
		for (Event e : allEvents) {
			
			if (e.isWorking()) {
				arg0.setColor(Color.green);
				arg0.fillRect(x, y, width, height);
			} else if (e.isQueued()) {
				arg0.setColor(Color.yellow);
				arg0.fillRect(x, y, width, height);
			} else if (e.hasFailed()) {
				arg0.setColor(Color.red);
				arg0.fillRect(x, y, width, height);
			}
			
			arg0.setColor(Color.white);
			arg0.drawRect(x, y, width, height);
			arg0.setColor(Color.black);
			arg0.drawString(e.toString(), x + 15, y + 17);
			y += 30;
		}
	}

	@Override
	public void onStart() throws InterruptedException {
		Collections.addAll(allEvents, new AntiBan(), new FishSharks(), new BankSharks());
		super.onStart();
	}
}

Edited by exuals
Link to comment
Share on other sites

Just saying...

 

  • AttackMonster, this can be run in BLOCKING mode as we don't want to fight multiple monsters. When this event is executed, if there is another event currently running, it will be queued. When this event is Working it will be in combat with a monster, when the event is Finished, the monster should be dead and Failed would be if the monster is unable to be killed or killed us. This is incredibly useful as there is functionality to run code while event is queued, hence, if you queued an AttackMonster event it could right click the monster and wait for the current to be killed.

I haven't scripted for OSBot 2 but its seems you should be setting that Event to be competed when you're attacking the monster, not when it's dead. Otherwise you'll be stuck in the AttackMonster event 'til it's dead.

Edited by Deffiliate
Link to comment
Share on other sites

Just saying...

 

  • AttackMonster, this can be run in BLOCKING mode as we don't want to fight multiple monsters. When this event is executed, if there is another event currently running, it will be queued. When this event is Working it will be in combat with a monster, when the event is Finished, the monster should be dead and Failed would be if the monster is unable to be killed or killed us. This is incredibly useful as there is functionality to run code while event is queued, hence, if you queued an AttackMonster event it could right click the monster and wait for the current to be killed.

I haven't scripted for OSBot 2 but its seems you should be setting that Event to be competed when you're attacking the monster, not when it's dead. Otherwise you'll be stuck in the AttackMonster event 'til it's dead.

 

It's intentional, otherwise it'd attack a monster and immediately attempt to attack another when you're in combat.

 

It's an example to demonstrate how to do the right click next monster for max efficiency shit.

Link to comment
Share on other sites

Since there is a tutorial for the legacy I figured I'd throw up what I've been using to develop OSB2 scripts.

 

So, before I copy and paste code, an event is similar to a 'node' with the addition of useful features such as;

 

  • EventMode
  • EventStatus

EventMode determines when to run the code in the event. With concurrency changes in OSB2 you can run events in 'ASYNC' mode, AKA another thread that runs this event in sync with another already running event.

 

For example say you have an autofighter, you can start a new event in async mode that checks prayer points and when low; drinks a dose of prayer restore. 

 

You can also set an event to run in BLOCKING mode, simply stating that when the current event is finished, run this one next.

 

 

EventStatus determines what the event is currently doing, this is useful for managing multiple events.

 

EventStatus can be: 

  • Queued
  • Working
  • Finished
  • Failed

Back to the autofighter example, some of the events could be:

 

  • AttackMonster, this can be run in BLOCKING mode as we don't want to fight multiple monsters. When this event is executed, if there is another event currently running, it will be queued. When this event is Working it will be in combat with a monster, when the event is Finished, the monster should be dead and Failed would be if the monster is unable to be killed or killed us. This is incredibly useful as there is functionality to run code while event is queued, hence, if you queued an AttackMonster event it could right click the monster and wait for the current to be killed.
  •  
  • EatFood, this would be run in ASYNC mode as we want to eat when we are low and not wait for the current event to finish. EatFood can be queued to indicate that you want to eat 3 or 4 pieces of food in a row. When the event is finished, determine that our health increased to complete the event.

 

 

Now, our core script class:

import java.awt.Graphics2D;
import java.util.ArrayList;

import org.osbot.script.Script;
import org.osbot.script.event.Event;

import Events.EventTest;

public class TestScript extends Script {
	private ArrayList<Event> allEvents = new ArrayList<Event>();
	
	@Override
	public int onLoop() throws InterruptedException {
		for (Event e : allEvents) {
			execute(e);
		}
		return 1000;
	}

	@Override
	public void onStart() throws InterruptedException {
		allEvents.add(new EventTest());
		super.onStart();
	}
}

Looks similar to the node framework right?

 

Simple enough to read, we have an ArrayList of all of our events , onStart we add what events we want to use.

 

During the onLoop for this example we simply execute all of the events in the arraylist.

 

 

*Continuation soon after I write an example script

While your code is ok, It is highly unoptimized. I suggest the following changes :

Don't declare inner class variables private. Since it won't be accessed by other classes, you can just leave it as default.

Make your ArrayList final because first, you are not changing it anywhere else in your class, second, the JVM reads it first through early binding, so make it

final ArrayList<Event> allEvents = new ArrayList<Event>();

You can also make your Event E final too, so the loop can be:

for(final Event e : all evens){

blbalblabla

}

Link to comment
Share on other sites

While your code is ok, It is highly unoptimized. I suggest the following changes :

Don't declare inner class variables private. Since it won't be accessed by other classes, you can just leave it as default.

Make your ArrayList final because first, you are not changing it anywhere else in your class, second, the JVM reads it first through early binding, so make it

final ArrayList<Event> allEvents = new ArrayList<Event>();

You can also make your Event E final too, so the loop can be:

for(final Event e : all evens){

blbalblabla

}

Thanks, my IDE does it automatically when I refactor const's though.

Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...