55import org .bukkit .scheduler .BukkitRunnable ;
66import org .bukkit .scheduler .BukkitTask ;
77
8+ import java .util .Iterator ;
9+ import java .util .concurrent .CompletableFuture ;
10+ import java .util .function .BiConsumer ;
811import java .util .function .Supplier ;
912
1013@ UtilityClass
@@ -30,17 +33,23 @@ public static BukkitTask repeat(Runnable task, long delay, long period) {
3033 return Bukkit .getScheduler ().runTaskTimer (XUtils .getPlugin (), task , delay , period );
3134 }
3235
33- public static void repeatTimes (Runnable task , long delay , long period , int times ) {
36+ public static CompletableFuture <Void > repeatTimes (Runnable task , long delay , long period , int times ) {
37+ CompletableFuture <Void > future = new CompletableFuture <>();
38+
3439 new BukkitRunnable () {
3540 int remaining = times ;
41+
3642 @ Override
3743 public void run () {
3844 task .run ();
3945 if (--remaining == 0 ) {
46+ future .complete (null );
4047 cancel ();
4148 }
4249 }
4350 }.runTaskTimer (XUtils .getPlugin (), delay , period );
51+
52+ return future ;
4453 }
4554
4655 public static BukkitTask repeatAsync (Runnable task , long delay , long period ) {
@@ -90,4 +99,33 @@ public static void cancelAll() {
9099 Bukkit .getScheduler ().cancelTasks (XUtils .getPlugin ());
91100 }
92101
93- }
102+ public static <T > CompletableFuture <Void > forEach (Iterable <T > iterable , BiConsumer <Integer , T > consumer , long delay , long period ) {
103+ CompletableFuture <Void > future = new CompletableFuture <>();
104+ Iterator <T > iterator = iterable .iterator ();
105+
106+ new BukkitRunnable () {
107+ int index = 0 ;
108+
109+ @ Override
110+ public void run () {
111+ if (!iterator .hasNext ()) {
112+ future .complete (null );
113+ cancel ();
114+ return ;
115+ }
116+
117+ T item = iterator .next ();
118+ consumer .accept (index ++, item );
119+ }
120+ }.runTaskTimer (XUtils .getPlugin (), delay , period );
121+
122+ return future ;
123+ }
124+
125+ public static CompletableFuture <Void > delay (long ticks ) {
126+ CompletableFuture <Void > future = new CompletableFuture <>();
127+ Bukkit .getScheduler ().runTaskLater (XUtils .getPlugin (), () -> future .complete (null ), ticks );
128+ return future ;
129+ }
130+
131+ }
0 commit comments