Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

Sign up now!

Resource evaluate an action for a specific amount of time

Joined
Nov 7, 2015
Messages
31
If you want to evaluate an action for a specific amount of time.
For example, the player is not moving for at least 400ms.

Code:
MinimumAction<Boolean> notMoving = new MinimumAction<>(player::isMoving, (a, b) -> !a && !b, 400);

Code:
import com.runemate.game.api.script.Execution;

import java.util.function.BiPredicate;
import java.util.function.Supplier;

public class MinimumAction<T> {

    private static final long DEFAULT_INTERVAL = 100;

    private final Supplier<T> supplier;
    private final BiPredicate<T, T> predicate;
    private final long delta;
    private final long interval;

    public MinimumAction(Supplier<T> supplier, BiPredicate<T, T> predicate, long delta) {
        this(supplier, predicate, delta, DEFAULT_INTERVAL);
    }

    public MinimumAction(Supplier<T> supplier, BiPredicate<T, T> predicate, long delta, long interval) {
        this.supplier = supplier;
        this.predicate = predicate;
        this.delta = delta;
        this.interval = interval;
    }

    public boolean evaluate() {
        long start = System.currentTimeMillis();
        T previousAction = supplier.get();

        while (System.currentTimeMillis() - start < delta) {
            if (!predicate.test(previousAction, supplier.get())) {
                return false;
            }
            Execution.delay(interval);
        }
        return predicate.test(previousAction, supplier.get());
    }

}
 
Java Warlord
Joined
Nov 17, 2014
Messages
4,906
Is there any difference to Execution.delayWhile(localPlayer::isMoving, 400)?
 
Top