Skip to content

Commit 061895c

Browse files
committed
Add folia support.
1 parent b400627 commit 061895c

4 files changed

Lines changed: 39 additions & 22 deletions

File tree

build.gradle.kts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,14 @@ val paperVersion = "1.20.1-R0.1-SNAPSHOT"
1313
val viaVersionVersion = "4.8.1"
1414
val miniPlaceholdersVersion = "2.2.3"
1515
val lombokVersion = "1.18.30"
16+
val foliaLibVersion = "main-SNAPSHOT"
1617

1718
allprojects {
1819
apply(plugin = "java-library")
1920

2021
repositories {
2122
mavenLocal()
23+
maven { url = uri("https://jitpack.io") }
2224
maven { url = uri("https://repo.papermc.io/repository/maven-public/") }
2325
maven { url = uri("https://hub.spigotmc.org/nexus/content/groups/public/") }
2426
maven { url = uri("https://repo.dmulloy2.net/content/groups/public/") }
@@ -52,6 +54,8 @@ allprojects {
5254
compileOnly("net.kyori:adventure-text-minimessage:${adventureVersion}")
5355
compileOnly("net.kyori:adventure-text-serializer-gson:${adventureVersion}")
5456
compileOnly("net.kyori:adventure-text-serializer-legacy:${adventureVersion}")
57+
58+
compileOnly("com.github.technicallycoded:FoliaLib:${foliaLibVersion}")
5559
}
5660
}
5761

src/main/java/me/catcoder/sidebar/Sidebar.java

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package me.catcoder.sidebar;
22

33
import 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;
47
import lombok.AccessLevel;
58
import lombok.Getter;
69
import 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
/**

src/main/java/me/catcoder/sidebar/SidebarLine.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.google.common.base.Function;
44
import com.google.common.base.Preconditions;
5+
import com.tcoded.folialib.wrapper.task.WrappedTask;
56
import io.netty.buffer.ByteBuf;
67
import lombok.*;
78
import me.catcoder.sidebar.protocol.ChannelInjector;
@@ -11,9 +12,7 @@
1112
import me.catcoder.sidebar.util.lang.ThrowingFunction;
1213
import me.catcoder.sidebar.util.lang.ThrowingPredicate;
1314
import me.catcoder.sidebar.util.lang.ThrowingSupplier;
14-
import org.bukkit.Bukkit;
1515
import org.bukkit.entity.Player;
16-
import org.bukkit.scheduler.BukkitTask;
1716

1817
@Getter
1918
@ToString
@@ -28,7 +27,7 @@ public class SidebarLine<R> {
2827
private final boolean staticText;
2928

3029
// for internal use
31-
BukkitTask updateTask;
30+
WrappedTask updateTask;
3231

3332
private ThrowingFunction<Player, R, Throwable> updater;
3433
private ThrowingPredicate<Player, Throwable> displayCondition;
@@ -68,21 +67,21 @@ public SidebarLine<R> scoreNumberFormatStyled(@NonNull Function<Player, R> score
6867
return this;
6968
}
7069

71-
public BukkitTask updatePeriodically(long delay, long period, @NonNull Sidebar<R> sidebar) {
70+
public WrappedTask updatePeriodically(long delay, long period, @NonNull Sidebar<R> sidebar) {
7271
Preconditions.checkState(!isStaticText(), "Cannot set updater for static text line");
7372

7473
if (updateTask != null) {
7574
Preconditions.checkState(updateTask.isCancelled(),
7675
"Update task for line %s is already running. Cancel it first.", this);
77-
sidebar.taskIds.remove(updateTask.getTaskId());
76+
sidebar.getFoliaLib().getScheduler().cancelTask(updateTask);
77+
sidebar.tasks.remove(updateTask);
7878
}
7979

80-
BukkitTask task = Bukkit.getScheduler().runTaskTimerAsynchronously(sidebar.getPlugin(),
81-
() -> sidebar.updateLine(this), delay, period);
80+
WrappedTask task = sidebar.getFoliaLib().getScheduler().runTimerAsync(() -> sidebar.updateLine(this), delay, period);
8281

8382
this.updateTask = task;
8483

85-
sidebar.bindBukkitTask(task);
84+
sidebar.bindWrappedTask(task);
8685

8786
return task;
8887
}

standalone-plugin/build.gradle.kts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@ dependencies {
99
implementation(project(":"))
1010
}
1111

12-
1312
tasks.withType<ShadowJar> {
1413
archiveFileName.set("ProtocolSidebar-${rootProject.version}.jar")
14+
relocate("com.tcoded.folialib", "me.catcoder.protocolsidebar.lib.folialib")
1515

1616
// create final jar in project root dir
1717
destinationDirectory.set(rootProject.rootDir.resolve("bin"))

0 commit comments

Comments
 (0)