From f68054d56486727256622612dc250e62e81fae79 Mon Sep 17 00:00:00 2001 From: LeoDog896 Date: Tue, 1 Jun 2021 11:29:40 -0400 Subject: [PATCH] Convenience toMilliseconds, add UpdateOption methods --- .../minestom/server/timer/TaskBuilder.java | 25 +++++++++++++++++++ .../server/utils/time/UpdateOption.java | 9 +++++++ 2 files changed, 34 insertions(+) diff --git a/src/main/java/net/minestom/server/timer/TaskBuilder.java b/src/main/java/net/minestom/server/timer/TaskBuilder.java index 98636ce88..bb7a03121 100644 --- a/src/main/java/net/minestom/server/timer/TaskBuilder.java +++ b/src/main/java/net/minestom/server/timer/TaskBuilder.java @@ -3,6 +3,7 @@ package net.minestom.server.timer; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; import net.minestom.server.extras.selfmodification.MinestomRootClassLoader; import net.minestom.server.utils.time.TimeUnit; +import net.minestom.server.utils.time.UpdateOption; import org.jetbrains.annotations.NotNull; /** @@ -72,6 +73,18 @@ public class TaskBuilder { return this; } + /** + * Specifies that the {@link Task} should delay its execution by the specified amount of time. + * + * @param updateOption the UpdateOption for this builder. + * @return this builder, for chaining + */ + @NotNull + public TaskBuilder delay(UpdateOption updateOption) { + this.delay = updateOption.toMilliseconds(); + return this; + } + /** * Specifies that the {@link Task} should continue to run after waiting for the specified value until it is terminated. * @@ -85,6 +98,18 @@ public class TaskBuilder { return this; } + /** + * Specifies that the {@link Task} should continue to run after waiting for the specified value until it is terminated. + * + * @param updateOption the UpdateOption for this builder. + * @return this builder, for chaining + */ + @NotNull + public TaskBuilder repeat(UpdateOption updateOption) { + this.repeat = updateOption.toMilliseconds(); + return this; + } + /** * Clears the delay interval of the {@link Task}. * diff --git a/src/main/java/net/minestom/server/utils/time/UpdateOption.java b/src/main/java/net/minestom/server/utils/time/UpdateOption.java index c190330fd..7f186bc73 100644 --- a/src/main/java/net/minestom/server/utils/time/UpdateOption.java +++ b/src/main/java/net/minestom/server/utils/time/UpdateOption.java @@ -35,4 +35,13 @@ public class UpdateOption { UpdateOption updateOption = (UpdateOption) o; return Objects.equals(value, updateOption.value) && Objects.equals(timeUnit, updateOption.timeUnit); } + + /** + * Converts this update option to milliseconds + * + * @return the converted milliseconds based on the time value and the unit + */ + public long toMilliseconds() { + return timeUnit.toMilliseconds(value); + } }