Botre Posted March 29, 2016 Posted March 29, 2016 (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 April 3, 2016 by Botre 4
Botre Posted April 3, 2016 Author Posted April 3, 2016 Updated to use BooleanSupplier instead of Predicate, also added parameter checking for timeout decorator.