spiglin icon indicating copy to clipboard operation
spiglin copied to clipboard

Implement repeating function with expectations

Open lus opened this issue 5 years ago • 3 comments

A simple way to run an action every 1 * [specified time unit] when declaring an expectation. This can be used to simplify the creation of countdowns.

lus avatar Apr 02 '20 19:04 lus

I'm not sure I fully understand. Could you give an example of the expected behaviour and what it could look like in code?

JohnnyJayJay avatar Apr 03 '20 10:04 JohnnyJayJay

Maybe something like this:

plugin.expect<PlayerMoveEvent>(
    predicate = { it.player == player },
    timeout = 5,
    timeoutUnit = TimeUnit.SECONDS,
    timeoutAction = { player.teleport(location) },
    repeatingAction = { player.sendMessage("Timeout in " + leftTimeout + " seconds.") },
    action = { player.sendMessage("You moved!") }   
)

lus avatar Apr 03 '20 10:04 lus

I see. This could grow rather complex though, because You certainly do not want to execute such an action every time unit in all use cases. I'm not really happy with the "expectation"-mechanics anyway - I might consider rewriting it to be more flexible.

JohnnyJayJay avatar Apr 03 '20 10:04 JohnnyJayJay