Jump to content

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


Botre

Recommended Posts

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