diff options
| author | Sho Sakuma <me@m1sk9.dev> | 2026-02-23 16:43:04 +0900 |
|---|---|---|
| committer | Sho Sakuma <me@m1sk9.dev> | 2026-02-23 16:43:04 +0900 |
| commit | ea90554c8b3c92699e903d492de1a67cfb4d5ec5 (patch) | |
| tree | a08a901b67c567c0246ccae58295ca65fab91932 /platform-paper/src | |
| parent | 1265f5d6051fbf7942be2a913870bdd1bd5f24fe (diff) | |
| download | LunaticChat-ea90554c8b3c92699e903d492de1a67cfb4d5ec5.tar.gz LunaticChat-ea90554c8b3c92699e903d492de1a67cfb4d5ec5.tar.bz2 LunaticChat-ea90554c8b3c92699e903d492de1a67cfb4d5ec5.zip | |
feat: Support Folia platform
Diffstat (limited to 'platform-paper/src')
6 files changed, 61 insertions, 52 deletions
diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/ServiceInitializer.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/ServiceInitializer.kt index 0af6ed8..73657a1 100644 --- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/ServiceInitializer.kt +++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/ServiceInitializer.kt @@ -23,6 +23,7 @@ import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.plugin.java.JavaPlugin +import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.logging.Logger import kotlin.time.Duration.Companion.milliseconds @@ -337,12 +338,13 @@ class ServiceInitializer( // Only perform handshake once if (!handshakeCompleted.getAndSet(true)) { // Schedule handshake 1 second after first player joins - plugin.server.scheduler.runTaskLater( + plugin.server.asyncScheduler.runDelayed( plugin, - Runnable { + { performVelocityHandshake(event.player, manager) }, - 20L, + 1, + TimeUnit.SECONDS, ) } } @@ -408,17 +410,20 @@ class ServiceInitializer( /** * Schedules periodic tasks such as cache saving. + * Uses Folia-compatible AsyncScheduler API. */ fun schedulePeriodicTasks() { if (configuration.features.japaneseConversion.enabled && conversionCache != null) { - val saveInterval = configuration.features.japaneseConversion.cacheSaveIntervalSeconds * 20L - plugin.server.scheduler.runTaskTimerAsynchronously( + val intervalSeconds = + configuration.features.japaneseConversion + .cacheSaveIntervalSeconds + .toLong() + plugin.server.asyncScheduler.runAtFixedRate( plugin, - Runnable { - conversionCache?.saveToDisk() - }, - saveInterval, - saveInterval, + { conversionCache?.saveToDisk() }, + intervalSeconds, + intervalSeconds, + TimeUnit.SECONDS, ) } } diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelMessageLogger.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelMessageLogger.kt index 206968c..a100d93 100644 --- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelMessageLogger.kt +++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelMessageLogger.kt @@ -2,6 +2,7 @@ package dev.m1sk9.lunaticChat.paper.chat.channel import dev.m1sk9.lunaticChat.engine.chat.channel.ChannelMessageLogEntry import io.ktor.util.logging.Logger +import io.papermc.paper.threadedregions.scheduler.ScheduledTask import kotlinx.serialization.encodeToString import kotlinx.serialization.json.Json import org.bukkit.plugin.Plugin @@ -13,6 +14,7 @@ import java.time.LocalDate import java.time.ZoneOffset import java.time.format.DateTimeFormatter import java.util.concurrent.ConcurrentLinkedQueue +import java.util.concurrent.TimeUnit import kotlin.io.path.deleteIfExists import kotlin.io.path.fileSize import kotlin.io.path.listDirectoryEntries @@ -39,16 +41,16 @@ class ChannelMessageLogger( ) { private val pendingEntries = ConcurrentLinkedQueue<ChannelMessageLogEntry>() private val json = Json { encodeDefaults = true } - private var flushTaskId: Int? = null - private var cleanupTaskId: Int? = null + private var flushTask: ScheduledTask? = null + private var cleanupTask: ScheduledTask? = null companion object { private const val LOG_FILE_PREFIX = "channel-messages-" private const val LOG_FILE_EXTENSION = ".json" private val DATE_FORMATTER: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") - private const val FLUSH_INTERVAL_TICKS = 20L // 1 second - private const val CLEANUP_INTERVAL_TICKS = 24 * 60 * 60 * 20L // 24 hours - private const val CLEANUP_INITIAL_DELAY_TICKS = 5 * 60 * 20L // 5 minutes + private const val FLUSH_INTERVAL_SECONDS = 1L + private const val CLEANUP_INTERVAL_SECONDS = 24 * 60 * 60L // 24 hours + private const val CLEANUP_INITIAL_DELAY_SECONDS = 5 * 60L // 5 minutes } init { @@ -74,20 +76,22 @@ class ChannelMessageLogger( /** * Schedules periodic flushing of pending log entries. + * Uses Folia-compatible AsyncScheduler API. */ private fun schedulePeriodicFlush() { - flushTaskId = - plugin.server.scheduler - .runTaskTimerAsynchronously( - plugin, - Runnable { flushPendingEntries() }, - FLUSH_INTERVAL_TICKS, - FLUSH_INTERVAL_TICKS, - ).taskId + flushTask = + plugin.server.asyncScheduler.runAtFixedRate( + plugin, + { flushPendingEntries() }, + FLUSH_INTERVAL_SECONDS, + FLUSH_INTERVAL_SECONDS, + TimeUnit.SECONDS, + ) } /** * Schedules periodic cleanup of old log files. + * Uses Folia-compatible AsyncScheduler API. */ private fun schedulePeriodicCleanup() { if (retentionDays <= 0) { @@ -95,14 +99,14 @@ class ChannelMessageLogger( return } - cleanupTaskId = - plugin.server.scheduler - .runTaskTimerAsynchronously( - plugin, - Runnable { cleanupOldLogs(retentionDays) }, - CLEANUP_INITIAL_DELAY_TICKS, - CLEANUP_INTERVAL_TICKS, - ).taskId + cleanupTask = + plugin.server.asyncScheduler.runAtFixedRate( + plugin, + { cleanupOldLogs(retentionDays) }, + CLEANUP_INITIAL_DELAY_SECONDS, + CLEANUP_INTERVAL_SECONDS, + TimeUnit.SECONDS, + ) logger.info("Scheduled log cleanup task (retention: $retentionDays days)") } @@ -159,8 +163,8 @@ class ChannelMessageLogger( */ fun shutdown() { // Cancel scheduled tasks - flushTaskId?.let { plugin.server.scheduler.cancelTask(it) } - cleanupTaskId?.let { plugin.server.scheduler.cancelTask(it) } + flushTask?.cancel() + cleanupTask?.cancel() // Flush remaining entries flushPendingEntries() diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelStorage.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelStorage.kt index 7176014..7a0c3c8 100644 --- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelStorage.kt +++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/chat/channel/ChannelStorage.kt @@ -84,16 +84,13 @@ class ChannelStorage( * @throws ChannelStorageSaveException if there is an error saving the data. */ fun queueAsyncSave(data: ChannelData) { - plugin.server.scheduler.runTaskAsynchronously( - plugin, - Runnable { - try { - saveToDisk(data) - } catch (e: ChannelStorageSaveException) { - logger.severe("Error saving channel data asynchronously: ${e.message}") - e.printStackTrace() - } - }, - ) + plugin.server.asyncScheduler.runNow(plugin) { + try { + saveToDisk(data) + } catch (e: ChannelStorageSaveException) { + logger.severe("Error saving channel data asynchronously: ${e.message}") + e.printStackTrace() + } + } } } diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/ConversionCache.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/ConversionCache.kt index da79cfa..bab4e8d 100644 --- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/ConversionCache.kt +++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/ConversionCache.kt @@ -2,10 +2,10 @@ package dev.m1sk9.lunaticChat.paper.converter import dev.m1sk9.lunaticChat.engine.converter.CacheData import kotlinx.serialization.json.Json -import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin import java.nio.file.Path import java.util.concurrent.ConcurrentHashMap +import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.logging.Logger import kotlin.io.path.bufferedReader @@ -110,13 +110,14 @@ class ConversionCache( private fun queueSaveToDisk() { if (conversionSaveQueue.compareAndSet(false, true)) { - Bukkit.getScheduler().runTaskLaterAsynchronously( + plugin.server.asyncScheduler.runDelayed( plugin, - Runnable { + { conversionSaveQueue.set(false) saveToDisk() }, - 100L, // 5 seconds = 100 ticks + 5, + TimeUnit.SECONDS, ) } } diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/settings/YamlPlayerSettingsStorage.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/settings/YamlPlayerSettingsStorage.kt index 0aa8eef..5a2c8e2 100644 --- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/settings/YamlPlayerSettingsStorage.kt +++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/settings/YamlPlayerSettingsStorage.kt @@ -2,9 +2,9 @@ package dev.m1sk9.lunaticChat.paper.settings import com.charleskorn.kaml.Yaml import dev.m1sk9.lunaticChat.engine.settings.PlayerSettingsData -import org.bukkit.Bukkit import org.bukkit.plugin.java.JavaPlugin import java.nio.file.Path +import java.util.concurrent.TimeUnit import java.util.concurrent.atomic.AtomicBoolean import java.util.logging.Logger import kotlin.io.path.bufferedReader @@ -87,13 +87,14 @@ class YamlPlayerSettingsStorage( */ fun queueAsyncSave(data: PlayerSettingsData) { if (saveFlag.compareAndSet(false, true)) { - Bukkit.getScheduler().runTaskLaterAsynchronously( + plugin.server.asyncScheduler.runDelayed( plugin, - Runnable { + { saveFlag.set(false) saveToDisk(data) }, - 100L, // 5 seconds = 100 ticks + 5, + TimeUnit.SECONDS, ) } } diff --git a/platform-paper/src/main/resources/paper-plugin.yml b/platform-paper/src/main/resources/paper-plugin.yml index f8a804b..cd37c13 100644 --- a/platform-paper/src/main/resources/paper-plugin.yml +++ b/platform-paper/src/main/resources/paper-plugin.yml @@ -2,6 +2,7 @@ name: LunaticChat version: ${version} main: dev.m1sk9.lunaticChat.paper.LunaticChat api-version: '1.21' +folia-supported: true prefix: LunaticChat load: STARTUP authors: [ m1sk9 ] |
