Skip to content

Commit c5c791b

Browse files
committed
Tentative fix for Folia Player-Update ask scheduling error
1 parent 892d3e2 commit c5c791b

File tree

1 file changed

+8
-12
lines changed

1 file changed

+8
-12
lines changed

implementations/folia/src/main/java/de/bluecolored/bluemap/bukkit/BukkitPlugin.java

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@
4949
import org.bukkit.plugin.java.JavaPlugin;
5050

5151
import java.io.IOException;
52-
import java.lang.ref.WeakReference;
5352
import java.lang.reflect.Field;
5453
import java.nio.file.Path;
5554
import java.util.*;
@@ -69,7 +68,7 @@ public class BukkitPlugin extends JavaPlugin implements ServerInterface, Listene
6968
private final Map<UUID, Player> onlinePlayerMap;
7069
private final List<BukkitPlayer> onlinePlayerList;
7170

72-
private final Collection<WeakReference<ScheduledTask>> scheduledTasks;
71+
private final Collection<ScheduledTask> scheduledTasks;
7372

7473
private final LoadingCache<World, ServerWorld> worlds;
7574

@@ -91,7 +90,7 @@ public BukkitPlugin() {
9190
this.onlinePlayerMap = new ConcurrentHashMap<>();
9291
this.onlinePlayerList = Collections.synchronizedList(new ArrayList<>());
9392

94-
this.scheduledTasks = new ArrayList<>();
93+
this.scheduledTasks = Collections.synchronizedCollection(Collections.newSetFromMap(new WeakHashMap<>()));
9594

9695
this.eventForwarder = new EventForwarder();
9796
this.pluginInstance = new Plugin("bukkit", this);
@@ -156,8 +155,7 @@ public void onEnable() {
156155
@Override
157156
public void onDisable() {
158157
Logger.global.logInfo("Stopping...");
159-
scheduledTasks.forEach(taskRef -> {
160-
ScheduledTask task = taskRef.get();
158+
scheduledTasks.forEach(task -> {
161159
if (task != null) task.cancel();
162160
});
163161
scheduledTasks.clear();
@@ -249,9 +247,6 @@ public void onPlayerLeave(PlayerQuitEvent evt) {
249247
synchronized (onlinePlayerList) {
250248
onlinePlayerList.removeIf(p -> p.getUuid().equals(playerUUID));
251249
}
252-
253-
// tidy up task-references
254-
scheduledTasks.removeIf(ref -> ref.get() == null);
255250
}
256251

257252
@Override
@@ -270,10 +265,11 @@ private void initPlayer(org.bukkit.entity.Player bukkitPlayer) {
270265
onlinePlayerList.add(player);
271266

272267
// update player every 20 seconds
273-
bukkitPlayer.getScheduler().runAtFixedRate(this, task -> {
274-
player.update();
275-
scheduledTasks.add(new WeakReference<>(task));
276-
}, () -> {}, 20, 20);
268+
scheduledTasks.add(
269+
bukkitPlayer.getScheduler().runAtFixedRate(this, task -> {
270+
player.update();
271+
}, () -> {}, 20, 20)
272+
);
277273
}
278274

279275
}

0 commit comments

Comments
 (0)