diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/Paintball.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/Paintball.kt index c5735cc..62d2127 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/Paintball.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/Paintball.kt @@ -21,8 +21,8 @@ class Paintball : JavaPlugin() { companion object { lateinit var INSTANCE: Paintball - lateinit var gameConfig: GameConfig - private lateinit var themeConfig: ThemeConfig + var gameConfig: GameConfig = GameConfig() + private var themeConfig: ThemeConfig = ThemeConfig() val lastShot = mutableMapOf() val lastKill = mutableMapOf() @@ -33,17 +33,23 @@ class Paintball : JavaPlugin() { } + override fun reloadConfig() { + super.reloadConfig() + gameConfig = gameConfig.load() + themeConfig = themeConfig.load() + ThemeBuilder.loadConfig(themeConfig) + } + override fun onEnable() { // Plugin startup logic INSTANCE = this // config - saveDefaultConfig() - GameConfig.registerConfigClasses() - ThemeConfig.registerConfigClasses() - gameConfig = config.get("game") as? GameConfig ?: GameConfig() - themeConfig = config.get("theme") as? ThemeConfig ?: ThemeConfig() + gameConfig = gameConfig.load() + themeConfig = themeConfig.load() ThemeBuilder.loadConfig(themeConfig) + gameConfig.save() + themeConfig.save() // commands PaintballCommand().register(this) @@ -66,11 +72,5 @@ class Paintball : JavaPlugin() { } override fun onDisable() { - // Plugin shutdown logic - - // config - config.set("game", gameConfig) - config.set("theme", themeConfig) - saveConfig() } } \ No newline at end of file diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ArenaCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ArenaCommand.kt index 929b19e..1f3fed3 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ArenaCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ArenaCommand.kt @@ -74,6 +74,7 @@ class ArenaCommand : ArgumentBuilder() { val newWordName = ctx.getArgument("worldName", String::class.java) Paintball.gameConfig.arenaWorldName = newWordName + Paintball.gameConfig.save() ctx.source.sendMessage(ThemeBuilder.themed( "*Successfully* updated arena world name to '$newWordName'" )) diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/AutostartCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/AutostartCommand.kt index a87084a..775a9ac 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/AutostartCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/AutostartCommand.kt @@ -9,7 +9,6 @@ import com.mojang.brigadier.tree.CommandNode import de.crightgames.blxckoxymoron.paintball.Paintball import de.crightgames.blxckoxymoron.paintball.game.Countdown import de.crightgames.blxckoxymoron.paintball.util.ThemeBuilder -import org.bukkit.Bukkit import org.bukkit.command.CommandSender class AutostartCommand : ArgumentBuilder() { @@ -24,10 +23,9 @@ class AutostartCommand : ArgumentBuilder() { val newEnabled = ctx.getArgument("enabled", Boolean::class.java) ?: return@executes -1 Paintball.gameConfig.autostart = newEnabled + Paintball.gameConfig.save() - if (newEnabled && Bukkit.getOnlinePlayers().size >= Paintball.gameConfig.minimumPlayers) { - Countdown.start() - } + Countdown.checkAndStart() ctx.source.sendMessage(ThemeBuilder.themed( "*Autostart* is now *${if (newEnabled) "enabled" else "disabled"}*." diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ForceStartCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ForceStartCommand.kt index 894aa73..f17903f 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ForceStartCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ForceStartCommand.kt @@ -13,7 +13,7 @@ class ForceStartCommand : ArgumentBuilder() { } override fun build(): CommandNode { - return literal("forcestart").executes { ctx -> + return literal("forcestart").executes { Game.start() diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/MinPlayersCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/MinPlayersCommand.kt index 783cbe6..862495a 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/MinPlayersCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/MinPlayersCommand.kt @@ -23,6 +23,7 @@ class MinPlayersCommand : ArgumentBuilder() { val newCount = ctx.getArgument("playercount", Int::class.java) ?: return@executes -1 Paintball.gameConfig.minimumPlayers = newCount + Paintball.gameConfig.save() ctx.source.sendMessage(ThemeBuilder.themed( "*Updated* minimum player count to *$newCount*." diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/PaintballCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/PaintballCommand.kt index 9d3bdfa..5a080ee 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/PaintballCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/PaintballCommand.kt @@ -16,6 +16,8 @@ class PaintballCommand : BrigadierTabExecutor("paintball"){ AutostartCommand() ).then( ArenaCommand() + ).then( + ReloadConfigCommand() ).then( literal("dev") .then( diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ReloadConfigCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ReloadConfigCommand.kt new file mode 100644 index 0000000..e6bee87 --- /dev/null +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/ReloadConfigCommand.kt @@ -0,0 +1,34 @@ +package de.crightgames.blxckoxymoron.paintball.commands + +import com.mojang.brigadier.Command +import com.mojang.brigadier.builder.ArgumentBuilder +import com.mojang.brigadier.builder.LiteralArgumentBuilder.literal +import com.mojang.brigadier.tree.CommandNode +import de.crightgames.blxckoxymoron.paintball.Paintball +import de.crightgames.blxckoxymoron.paintball.game.Countdown +import de.crightgames.blxckoxymoron.paintball.game.Game +import de.crightgames.blxckoxymoron.paintball.util.ThemeBuilder +import org.bukkit.command.CommandSender + +class ReloadConfigCommand : ArgumentBuilder() { + override fun getThis(): ReloadConfigCommand { + return this + } + + override fun build(): CommandNode { + return literal("reloadConfig").executes { ctx -> + if (Game.state == Game.GameState.RUNNING) { + ctx.source.sendMessage(ThemeBuilder.themed( + ":RED:Can't reload the config while the game is running::" + )) + return@executes Command.SINGLE_SUCCESS + } + Paintball.INSTANCE.reloadConfig() + Countdown.checkAndStart() + ctx.source.sendMessage(ThemeBuilder.themed( + "Config reloaded" + )) + Command.SINGLE_SUCCESS + }.build() + } +} \ No newline at end of file diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/TeamspawnCommand.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/TeamspawnCommand.kt index 1bd4c4a..eb78356 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/TeamspawnCommand.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/commands/TeamspawnCommand.kt @@ -39,6 +39,7 @@ class TeamspawnCommand : ArgumentBuilder() { teamSpawnPos.direction = dir team.spawnPos = teamSpawnPos + Paintball.gameConfig.save() player.sendMessage(ThemeBuilder.themed( "*Successfully* updated the spawnposition for team ${team.displayName}." diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Countdown.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Countdown.kt index 08d8300..f5c3e91 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Countdown.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Countdown.kt @@ -17,15 +17,19 @@ object Countdown { private var currentTime = Duration.ZERO private var decreaseTask: BukkitTask? = null + private val countingDown + get() = decreaseTask?.isCancelled == false + fun checkAndStart() { val onlinePlayerCount = Bukkit.getOnlinePlayers().size - if (Game.state == Game.GameState.WAITING && onlinePlayerCount >= Paintball.gameConfig.minimumPlayers && Paintball.gameConfig.autostart) + if (onlinePlayerCount < Paintball.gameConfig.minimumPlayers || !Paintball.gameConfig.autostart) return cancelStart() + + if (Game.state == Game.GameState.WAITING && !countingDown) start() } - fun start() { - if (decreaseTask?.isCancelled == false) return // already counting down + private fun start() { currentTime = Paintball.gameConfig.durations["timer"]!! decreaseTask = Bukkit.getScheduler().runTaskTimer(Paintball.INSTANCE, decrease, 0, TIMER_SPEED.inWholeTicks) } @@ -51,9 +55,8 @@ object Countdown { } private fun cancelStart() { + if (countingDown) Bukkit.broadcastMessage(ThemeBuilder.themed(":RED:Start abgebrochen::")) decreaseTask?.cancel() - - Bukkit.broadcastMessage(ThemeBuilder.themed(":RED:Zu wenig Spieler - Start abgebrochen::")) } private fun notifyPlayer(player: Player) { diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt index c9b46ec..8cde9f8 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/Game.kt @@ -1,6 +1,5 @@ package de.crightgames.blxckoxymoron.paintball.game -import com.mojang.brigadier.Command import de.crightgames.blxckoxymoron.paintball.Paintball import de.crightgames.blxckoxymoron.paintball.Paintball.Companion.inWholeTicks import de.crightgames.blxckoxymoron.paintball.game.config.ConfigTeam @@ -77,7 +76,7 @@ object Game { if (Paintball.gameConfig.lastArenaName.isNotBlank()) { val worldFolder = File(Bukkit.getWorldContainer(), Paintball.gameConfig.lastArenaName) if (worldFolder.exists() && worldFolder.isDirectory) { - Bukkit.getLogger().info("deleting an old arena world") + Bukkit.getLogger().info("deleting old arena world") worldFolder.deleteRecursively() } } @@ -107,9 +106,7 @@ object Game { Bukkit.getScheduler().runTask(Paintball.INSTANCE, Runnable { arenaWorld = Bukkit.createWorld(WorldCreator(randomWorldName).generator(EmptyWorldGen())) arenaWorld?.isAutoSave = false - Paintball.gameConfig.teams.forEach { - it.spawnPos?.world = arenaWorld - } + arenaWorld?.spawnLocation?.let { spawn -> Bukkit.getOnlinePlayers().forEach { it.teleport(spawn) @@ -120,6 +117,7 @@ object Game { Bukkit.unloadWorld(Paintball.gameConfig.lastArenaName, false) Paintball.gameConfig.lastArenaName = randomWorldName + Paintball.gameConfig.save() }) } } @@ -140,7 +138,6 @@ object Game { Scores.createAndResetScores() Countdown.checkAndStart() - Command.SINGLE_SUCCESS } fun start() { @@ -159,7 +156,7 @@ object Game { } Paintball.gameConfig.teams.forEach { team -> - val spawnLocation = team.spawnPos ?: return@forEach run { + val spawnLocation = team.spawnPosInGame ?: return@forEach run { Bukkit.broadcastMessage(ThemeBuilder.themed( ":RED:Can't teleport players of team ::${team.displayName}\n" + "Please set a spawnpoint and start again" @@ -170,7 +167,7 @@ object Game { pl.sendMessage(ThemeBuilder.themed( "*Paintball*: Benutze ${if (Paintball.gameConfig.easterMode) "das Osterei" else "den Schneeball"}, um Blöcke einzufärben! " + "Außerdem kannst du deine Gegner abschießen, " + - "wodurch du aber für ${Paintball.gameConfig.durations["kill"]?.inWholeSeconds ?: "ein paar"} " + + "wodurch du aber für ${Paintball.gameConfig.durations["kill"]?.inWholeSeconds ?: "ein paar"}s " + "niemanden treffen kannst. " + "\nDas Team, das *am Ende die größte Fläche* eingefärbt hat, gewinnt!" + "\nViel Erfolg!", diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt index 90d7ffa..9952de0 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ConfigTeam.kt @@ -1,6 +1,7 @@ package de.crightgames.blxckoxymoron.paintball.game.config import de.crightgames.blxckoxymoron.paintball.Paintball +import de.crightgames.blxckoxymoron.paintball.game.Game import de.crightgames.blxckoxymoron.paintball.game.IncMaterial import org.bukkit.Bukkit import org.bukkit.Location @@ -21,6 +22,9 @@ class ConfigTeam (val material: IncMaterial, val displayName: String, var spawnP val name get() = material.name + val spawnPosInGame + get() = spawnPos?.let { it.world = Game.arenaWorld; it } + fun addPlayer(p: Player) { players.add(p) bossBar.addPlayer(p) diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/GameConfig.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/GameConfig.kt index baf7431..6b64a8f 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/GameConfig.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/GameConfig.kt @@ -1,24 +1,22 @@ package de.crightgames.blxckoxymoron.paintball.game.config import de.crightgames.blxckoxymoron.paintball.game.IncMaterial +import de.crightgames.blxckoxymoron.paintball.util.ConfigObject import de.crightgames.blxckoxymoron.paintball.util.ThemeBuilder import org.bukkit.ChatColor import org.bukkit.Material import org.bukkit.configuration.serialization.ConfigurationSerializable -import org.bukkit.configuration.serialization.ConfigurationSerialization import kotlin.time.Duration.Companion.milliseconds import kotlin.time.Duration.Companion.minutes import kotlin.time.Duration.Companion.seconds -class GameConfig() : ConfigurationSerializable { +class GameConfig() : ConfigObject("game") { + + override val additionalConfigClasses: List> = listOf( + ConfigDuration::class.java, + ConfigTeam::class.java, + ) - companion object { - fun registerConfigClasses() { - ConfigurationSerialization.registerClass(ConfigDuration::class.java) - ConfigurationSerialization.registerClass(ConfigTeam::class.java) - ConfigurationSerialization.registerClass(GameConfig::class.java) - } - } /** * game diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ThemeConfig.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ThemeConfig.kt index 038872c..6d6e4ae 100644 --- a/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ThemeConfig.kt +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/game/config/ThemeConfig.kt @@ -1,16 +1,9 @@ package de.crightgames.blxckoxymoron.paintball.game.config +import de.crightgames.blxckoxymoron.paintball.util.ConfigObject import org.bukkit.ChatColor -import org.bukkit.configuration.serialization.ConfigurationSerializable -import org.bukkit.configuration.serialization.ConfigurationSerialization -class ThemeConfig(cfg: Map = mutableMapOf()) : ConfigurationSerializable { - - companion object { - fun registerConfigClasses() { - ConfigurationSerialization.registerClass(ThemeConfig::class.java) - } - } +class ThemeConfig(cfg: Map = mutableMapOf()) : ConfigObject("theme") { val default: ChatColor val highlight: ChatColor diff --git a/src/main/java/de/crightgames/blxckoxymoron/paintball/util/ConfigObject.kt b/src/main/java/de/crightgames/blxckoxymoron/paintball/util/ConfigObject.kt new file mode 100644 index 0000000..c29eba0 --- /dev/null +++ b/src/main/java/de/crightgames/blxckoxymoron/paintball/util/ConfigObject.kt @@ -0,0 +1,32 @@ +package de.crightgames.blxckoxymoron.paintball.util + +import de.crightgames.blxckoxymoron.paintball.Paintball +import org.bukkit.configuration.serialization.ConfigurationSerializable +import org.bukkit.configuration.serialization.ConfigurationSerialization + +abstract class ConfigObject> (private val key: String): ConfigurationSerializable { + companion object { + private var wasRegistered = mutableSetOf() + } + + private fun registerConfigClasses() { + if (wasRegistered.contains(this::class.qualifiedName)) return + + ConfigurationSerialization.registerClass(this::class.java) + additionalConfigClasses.forEach(ConfigurationSerialization::registerClass) + + this::class.qualifiedName?.let { wasRegistered.add(it) } + } + + open val additionalConfigClasses: List> = emptyList() + + fun load(): T { + registerConfigClasses() + return (Paintball.INSTANCE.config.get(key) as? T ?: this as T) + } + + fun save() { + Paintball.INSTANCE.config.set(key, this) + Paintball.INSTANCE.saveConfig() + } +}