Jump to content

[Snippet] Event Decorators: time out & break condition decorations


Recommended Posts

Posted (edited)

Example:

// Event will break after 10 seconds.
execute(EventDecorator.withTimeout(new WalkingEvent(leArea), 10)); 

// Event will break when player is animating (a bit random but ok..)
execute(EventDecorator.withBreakCondition(new InteractionEvent(goblin, "Attack"), () -> myPlayer().isAnimating())); 

Code:

package org.botre.osbot.event;

import java.util.function.BooleanSupplier;

import org.dreamstream.moka.main.time.FetchingTimer;
import org.dreamstream.moka.main.time.Timer;
import org.osbot.rs07.event.Event;

public final class EventDecorator {

	private EventDecorator() {
	
	}
	
	public static final Event withBreakCondition(Event event, BooleanSupplier breakCondition) {
		return new Event() {
			@Override
			public void onStart() {
				try {
					super.onStart();
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				event.setAsync();
				execute(event);
			}
			@Override
			public int execute() throws InterruptedException {
				boolean failed = breakCondition.getAsBoolean();
				if(failed) {
					event.setFailed();
					setFailed();
				}
				return 100;
			}
			@Override
			public boolean hasFailed() {
				boolean failed = breakCondition.getAsBoolean();
				if(failed) {
					event.setFailed();
					setFailed();
				}
				return failed || event.hasFailed();
			}
			@Override
			public boolean hasFinished() {
				return event.hasFinished();
			}
		};
	}
	
	public static final Event withTimeout(Event event, int timeoutSeconds) throws IllegalArgumentException {
		if(timeoutSeconds < 0) throw new IllegalArgumentException();
		Timer timer = FetchingTimer.milliseconds();
		return withBreakCondition(event, () -> {
			return timer.getElapsedSeconds() > timeoutSeconds;	
		});
	}
	
}
Edited by Botre
  • Like 4

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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