11package me .catcoder .sidebar ;
22
33import com .google .common .base .Preconditions ;
4+ import com .tcoded .folialib .FoliaLib ;
5+ import com .tcoded .folialib .wrapper .task .WrappedBukkitTask ;
6+ import com .tcoded .folialib .wrapper .task .WrappedTask ;
47import lombok .AccessLevel ;
58import lombok .Getter ;
69import lombok .NonNull ;
@@ -39,14 +42,17 @@ public class Sidebar<R> {
3942 private final ScoreboardObjective <R > objective ;
4043
4144 private TextIterator titleText ;
42- private BukkitTask titleUpdater ;
45+ private WrappedTask titleUpdater ;
4346
44- final Set <Integer > taskIds = new HashSet <>();
47+ final Set <WrappedTask > tasks = new HashSet <>();
4548 final TextProvider <R > textProvider ;
4649
4750 @ Getter
4851 private final Plugin plugin ;
4952
53+ @ Getter
54+ private final FoliaLib foliaLib ;
55+
5056 /**
5157 * Construct a new sidebar instance.
5258 *
@@ -55,6 +61,7 @@ public class Sidebar<R> {
5561 */
5662 Sidebar (@ NonNull R title , @ NonNull Plugin plugin , @ NonNull TextProvider <R > textProvider ) {
5763 this .plugin = plugin ;
64+ this .foliaLib = new FoliaLib (plugin );
5865 this .textProvider = textProvider ;
5966 this .objective = new ScoreboardObjective <>(OBJECTIVE_PREFIX + RandomString .generate (3 ), title , textProvider );
6067 }
@@ -67,6 +74,7 @@ public class Sidebar<R> {
6774 */
6875 Sidebar (@ NonNull TextIterator titleIterator , @ NonNull Plugin plugin , @ NonNull TextProvider <R > textProvider ) {
6976 this .plugin = plugin ;
77+ this .foliaLib = new FoliaLib (plugin );
7078 this .textProvider = textProvider ;
7179
7280 this .objective = new ScoreboardObjective <>(
@@ -121,7 +129,7 @@ private void setTitleIter(@NonNull TextIterator iterator) {
121129 cancelTitleUpdater ();
122130
123131 this .titleText = iterator ;
124- this .titleUpdater = plugin . getServer (). getScheduler ().runTaskTimerAsynchronously ( plugin , () -> {
132+ this .titleUpdater = foliaLib . getScheduler ().runTimerAsync ( () -> {
125133 String next = titleText .next ();
126134
127135 objective .setDisplayName (textProvider .fromLegacyMessage (next ));
@@ -151,8 +159,14 @@ public void shiftLine(SidebarLine<R> line, int offset) {
151159 * @param task - task to bind
152160 * @return the task
153161 */
162+ @ Deprecated (since = "6.2.9" )
154163 public BukkitTask bindBukkitTask (@ NonNull BukkitTask task ) {
155- this .taskIds .add (task .getTaskId ());
164+ this .tasks .add (new WrappedBukkitTask (task ));
165+ return task ;
166+ }
167+
168+ public WrappedTask bindWrappedTask (@ NonNull WrappedTask task ) {
169+ this .tasks .add (task );
156170 return task ;
157171 }
158172
@@ -163,7 +177,7 @@ public BukkitTask bindBukkitTask(@NonNull BukkitTask task) {
163177 * @param period period in ticks
164178 * @return the scheduled task
165179 */
166- public BukkitTask updateLinesPeriodically (long delay , long period ) {
180+ public WrappedTask updateLinesPeriodically (long delay , long period ) {
167181 return updateLinesPeriodically (delay , period , true );
168182 }
169183
@@ -175,12 +189,12 @@ public BukkitTask updateLinesPeriodically(long delay, long period) {
175189 * @param async whether the task should be executed asynchronously
176190 * @return the scheduled task
177191 */
178- public BukkitTask updateLinesPeriodically (long delay , long period , boolean async ) {
192+ public WrappedTask updateLinesPeriodically (long delay , long period , boolean async ) {
179193 return async ?
180- bindBukkitTask ( Bukkit .getScheduler ()
181- .runTaskTimerAsynchronously ( plugin , this ::updateAllLines , delay , period )) :
182- bindBukkitTask ( Bukkit .getScheduler ()
183- .runTaskTimer ( plugin , this ::updateAllLines , delay , period ));
194+ bindWrappedTask ( foliaLib .getScheduler ()
195+ .runTimerAsync ( this ::updateAllLines , delay , period )) :
196+ bindWrappedTask ( foliaLib .getScheduler ()
197+ .runTimer ( this ::updateAllLines , delay , period ));
184198 }
185199
186200 /**
@@ -371,8 +385,8 @@ public void removeViewers() {
371385 public void destroy () {
372386 cancelTitleUpdater ();
373387
374- for (int taskId : taskIds ) {
375- Bukkit .getScheduler ().cancelTask (taskId );
388+ for (WrappedTask task : tasks ) {
389+ foliaLib .getScheduler ().cancelTask (task );
376390 }
377391
378392 removeViewers ();
@@ -381,7 +395,7 @@ public void destroy() {
381395 lines .clear ();
382396 }
383397
384- taskIds .clear ();
398+ tasks .clear ();
385399 }
386400
387401 /**
0 commit comments