From 96c12ef483f636fe1f79cbddb3f3cf7f670559c5 Mon Sep 17 00:00:00 2001 From: nwayo <153366651+404Setup@users.noreply.github.com> Date: Sat, 30 Nov 2024 20:42:59 +0800 Subject: [PATCH] Use global region scheduler in runSync methods on Folia (#4292) --- .../viaversion/ViaVersionPlugin.java | 8 ++++++ .../bukkit/platform/FoliaViaTask.java | 28 +++++++++++++++++++ 2 files changed, 36 insertions(+) create mode 100644 bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/FoliaViaTask.java diff --git a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java index a11c8cef4..ea69290e0 100644 --- a/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java +++ b/bukkit/src/main/java/com/viaversion/viaversion/ViaVersionPlugin.java @@ -33,6 +33,7 @@ import com.viaversion.viaversion.bukkit.platform.BukkitViaInjector; import com.viaversion.viaversion.bukkit.platform.BukkitViaLoader; import com.viaversion.viaversion.bukkit.platform.BukkitViaTask; import com.viaversion.viaversion.bukkit.platform.BukkitViaTaskTask; +import com.viaversion.viaversion.bukkit.platform.FoliaViaTask; import com.viaversion.viaversion.bukkit.platform.PaperViaInjector; import com.viaversion.viaversion.dump.PluginInfo; import com.viaversion.viaversion.unsupported.UnsupportedPlugin; @@ -171,11 +172,18 @@ public class ViaVersionPlugin extends JavaPlugin implements ViaPlatform @Override public PlatformTask runSync(Runnable runnable, long delay) { + if (FOLIA) { + // Set the delayed tick to at least 1, as Folia requires this. + return new FoliaViaTask(getServer().getGlobalRegionScheduler().runDelayed(this, (e) -> runnable.run(), delay <= 0L ? 1L : delay)); + } return new BukkitViaTask(getServer().getScheduler().runTaskLater(this, runnable, delay)); } @Override public PlatformTask runRepeatingSync(Runnable runnable, long period) { + if (FOLIA) { + return new FoliaViaTask(getServer().getGlobalRegionScheduler().runAtFixedRate(this, (e) -> runnable.run(), 1, period)); + } return new BukkitViaTask(getServer().getScheduler().runTaskTimer(this, runnable, 0, period)); } diff --git a/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/FoliaViaTask.java b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/FoliaViaTask.java new file mode 100644 index 000000000..a1dfb668e --- /dev/null +++ b/bukkit/src/main/java/com/viaversion/viaversion/bukkit/platform/FoliaViaTask.java @@ -0,0 +1,28 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viaversion.bukkit.platform; + +import com.viaversion.viaversion.api.platform.PlatformTask; +import io.papermc.paper.threadedregions.scheduler.ScheduledTask; + +public record FoliaViaTask(ScheduledTask task) implements PlatformTask { + @Override + public void cancel() { + task.cancel(); + } +}