From 0bc38384b40ff29895a50ee4e996b107eb1ef118 Mon Sep 17 00:00:00 2001 From: trivernis Date: Sat, 27 Jun 2020 14:39:45 +0200 Subject: [PATCH] Change pause on join to have a configurable player count Closes #31 --- README.md | 9 ++-- .../net/trivernis/chunkmaster/Chunkmaster.kt | 2 +- .../chunkmaster/ChunkmasterEvents.kt | 41 ++++++++----------- .../lib/generation/GenerationManager.kt | 4 +- .../resources/i18n/DEFAULT.i18n.properties | 6 ++- src/main/resources/i18n/de.i18n.properties | 8 +++- src/main/resources/i18n/en.i18n.properties | 6 ++- 7 files changed, 40 insertions(+), 36 deletions(-) diff --git a/README.md b/README.md index 1e45572..a8b782c 100644 --- a/README.md +++ b/README.md @@ -114,13 +114,12 @@ generation: # The value should be a positive integer greater than 50. mspt-pause-threshold: 500 - # If the chunk generation process should pause on player join. + # Pauses the generation if the number of players on the server is larger or equal + # to the configured value # Notice that playing on a server that constantly generates chunks can be # very laggy and can cause it to crash. - # You could configure the values above so that the performance impact of the generation - # process is minimal. - # The value should be a boolean - pause-on-join: true + # The value should be a posivitve integer > 1. + pause-on-player-count: 1 ``` ### Spigot and Paper diff --git a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt index 285b8d9..9c709a4 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/Chunkmaster.kt @@ -75,7 +75,7 @@ class Chunkmaster: JavaPlugin() { config.addDefault("generation.chunks-per-step", 2) config.addDefault("generation.chunk-skips-per-step", 100) config.addDefault("generation.mspt-pause-threshold", 500L) - config.addDefault("generation.pause-on-join", true) + config.addDefault("generation.pause-on-player-count", 1) config.addDefault("generation.max-pending-chunks", 10) config.addDefault("generation.max-loaded-chunks", 10) config.addDefault("generation.ignore-worldborder", false) diff --git a/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt b/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt index 7c57428..650f65c 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/ChunkmasterEvents.kt @@ -1,16 +1,17 @@ package net.trivernis.chunkmaster -import net.trivernis.chunkmaster.lib.generation.GenerationTaskPaper import org.bukkit.Server import org.bukkit.event.EventHandler import org.bukkit.event.Listener import org.bukkit.event.player.PlayerJoinEvent import org.bukkit.event.player.PlayerQuitEvent -import org.bukkit.event.world.WorldSaveEvent class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server: Server) : Listener { - private val pauseOnJoin = chunkmaster.config.getBoolean("generation.pause-on-join") + private val pauseOnPlayerCount: Int + get () { + return chunkmaster.config.getInt("generation.pause-on-player-count") + } private var playerPaused = false /** @@ -18,21 +19,15 @@ class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server */ @EventHandler fun onPlayerQuit(event: PlayerQuitEvent) { - if (pauseOnJoin) { - if (server.onlinePlayers.size == 1 && server.onlinePlayers.contains(event.player) || - server.onlinePlayers.isEmpty() - ) { - if (!playerPaused) { - if (chunkmaster.generationManager.pausedTasks.isNotEmpty()) { - chunkmaster.logger.info("Server is empty. Resuming chunk generation tasks.") - } - chunkmaster.generationManager.resumeAll() - } else if (chunkmaster.generationManager.paused){ - chunkmaster.logger.info("Generation was manually paused. Not resuming automatically.") - playerPaused = chunkmaster.generationManager.paused - } else { - chunkmaster.logger.info("Generation tasks are already running.") + if (server.onlinePlayers.size == pauseOnPlayerCount) { + if (!playerPaused) { + if (chunkmaster.generationManager.pausedTasks.isNotEmpty()) { + chunkmaster.logger.info(chunkmaster.langManager.getLocalized("RESUME_PLAYER_LEAVE")) } + chunkmaster.generationManager.resumeAll() + } else if (chunkmaster.generationManager.paused){ + chunkmaster.logger.info(chunkmaster.langManager.getLocalized("PAUSE_MANUALLY")) + playerPaused = chunkmaster.generationManager.paused } } } @@ -42,14 +37,12 @@ class ChunkmasterEvents(private val chunkmaster: Chunkmaster, private val server */ @EventHandler fun onPlayerJoin(event: PlayerJoinEvent) { - if (pauseOnJoin) { - if (server.onlinePlayers.size == 1 || server.onlinePlayers.isEmpty()) { - if (chunkmaster.generationManager.tasks.isNotEmpty()) { - chunkmaster.logger.info("Pausing generation tasks because of player join.") - } - playerPaused = chunkmaster.generationManager.paused - chunkmaster.generationManager.pauseAll() + if (server.onlinePlayers.size >= pauseOnPlayerCount) { + if (chunkmaster.generationManager.tasks.isNotEmpty()) { + chunkmaster.logger.info(chunkmaster.langManager.getLocalized("PAUSE_PLAYER_JOIN")) } + playerPaused = chunkmaster.generationManager.paused + chunkmaster.generationManager.pauseAll() } } } \ No newline at end of file diff --git a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt index 509d910..989fe04 100644 --- a/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt +++ b/src/main/kotlin/net/trivernis/chunkmaster/lib/generation/GenerationManager.kt @@ -19,7 +19,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server this.loadWorldCenters() } this.startAll() - if (!server.onlinePlayers.isEmpty()) { + if (server.onlinePlayers.size >= chunkmaster.config.getInt("generation.pause-on-player-count")) { this.pauseAll() } } @@ -239,7 +239,7 @@ class GenerationManager(private val chunkmaster: Chunkmaster, private val server /** * Overload that doesn't need an argument */ - fun loadWorldCenters() { + private fun loadWorldCenters() { loadWorldCenters(null) } diff --git a/src/main/resources/i18n/DEFAULT.i18n.properties b/src/main/resources/i18n/DEFAULT.i18n.properties index 3b9bb3c..6e9ec32 100644 --- a/src/main/resources/i18n/DEFAULT.i18n.properties +++ b/src/main/resources/i18n/DEFAULT.i18n.properties @@ -56,4 +56,8 @@ COORD_INVALID = §cThe provided coordinate ('%s', '%s') is invalid! CENTER_UPDATED = §9The center for world §2%s §9has been set to §2(%s, %s)§9. CENTER_INFO = §9The center for world §2%s §9is §2(%s, %s)§9. -PLUGIN_DETECTED = Detected %s version %s \ No newline at end of file +PLUGIN_DETECTED = Detected %s version %s + +RESUME_PLAYER_LEAVE = The player count is smaller than the configured pause value. Resuming generation... +PAUSE_PLAYER_JOIN = The player count has reached the pause value. Pausing generation... +PAUSE_MANUALLY = Generation was manually paused. Not resuming automatically. \ No newline at end of file diff --git a/src/main/resources/i18n/de.i18n.properties b/src/main/resources/i18n/de.i18n.properties index e30787d..33f7a10 100644 --- a/src/main/resources/i18n/de.i18n.properties +++ b/src/main/resources/i18n/de.i18n.properties @@ -51,9 +51,13 @@ TABLE_CREATE_ERROR = §cFehler beim erstellen der Tabelle %s. UPDATE_TABLE_DEFINITION = Tabelle %s wurde mit sql %s geupdated. UPDATE_TABLE_FAILED = Fehler beim Updaten der Tabelle %s mit sql %s. -TOO_FEW_ARGUMENTS = §cDu hast nicht genug arguments angegeben. +TOO_FEW_ARGUMENTS = §cDu hast nicht genug Parameter angegeben. COORD_INVALID = §cDie Koordinate ('%s', '%s') ist ungültig! CENTER_UPDATED = §9Die Mitte der Welt §2%s §9wurde auf §2(%s, %s)§9 gesetzt. CENTER_INFO = §9Die Mitte der Welt §2%s §9ist §2(%s, %s)§9. -PLUGIN_DETECTED = Plugin %s in der Version %s gefunden! \ No newline at end of file +PLUGIN_DETECTED = Plugin %s in der Version %s gefunden! + +RESUME_PLAYER_LEAVE = Die Anzahl der Spieler hat den festgelegen Wert zum Pausieren unterschritten. Setze Generieren fort... +PAUSE_PLAYER_JOIN = Die Anzahl der Spieler hat den festgelegten Wert zum Pausieren erreicht. Pausiere... +PAUSE_MANUALLY = Das Generieren wurde manuell pausiert und wird deswegen nicht automatisch fortgesetzt. \ No newline at end of file diff --git a/src/main/resources/i18n/en.i18n.properties b/src/main/resources/i18n/en.i18n.properties index 3b9bb3c..6e9ec32 100644 --- a/src/main/resources/i18n/en.i18n.properties +++ b/src/main/resources/i18n/en.i18n.properties @@ -56,4 +56,8 @@ COORD_INVALID = §cThe provided coordinate ('%s', '%s') is invalid! CENTER_UPDATED = §9The center for world §2%s §9has been set to §2(%s, %s)§9. CENTER_INFO = §9The center for world §2%s §9is §2(%s, %s)§9. -PLUGIN_DETECTED = Detected %s version %s \ No newline at end of file +PLUGIN_DETECTED = Detected %s version %s + +RESUME_PLAYER_LEAVE = The player count is smaller than the configured pause value. Resuming generation... +PAUSE_PLAYER_JOIN = The player count has reached the pause value. Pausing generation... +PAUSE_MANUALLY = Generation was manually paused. Not resuming automatically. \ No newline at end of file