Skip to content

Commit

Permalink
✨ live config updates & reload command
Browse files Browse the repository at this point in the history
  • Loading branch information
b8daniel committed Apr 19, 2022
1 parent 29f598d commit 858718e
Show file tree
Hide file tree
Showing 14 changed files with 113 additions and 49 deletions.
26 changes: 13 additions & 13 deletions src/main/java/de/crightgames/blxckoxymoron/paintball/Paintball.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<UUID, Long>()
val lastKill = mutableMapOf<UUID, Long>()
Expand All @@ -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)
Expand All @@ -66,11 +72,5 @@ class Paintball : JavaPlugin() {
}

override fun onDisable() {
// Plugin shutdown logic

// config
config.set("game", gameConfig)
config.set("theme", themeConfig)
saveConfig()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class ArenaCommand : ArgumentBuilder<CommandSender, ArenaCommand>() {

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'"
))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CommandSender, AutostartCommand>() {
Expand All @@ -24,10 +23,9 @@ class AutostartCommand : ArgumentBuilder<CommandSender, AutostartCommand>() {

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"}*."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ForceStartCommand : ArgumentBuilder<CommandSender, ForceStartCommand>() {
}

override fun build(): CommandNode<CommandSender> {
return literal<CommandSender>("forcestart").executes { ctx ->
return literal<CommandSender>("forcestart").executes {

Game.start()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class MinPlayersCommand : ArgumentBuilder<CommandSender, MinPlayersCommand>() {

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*."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class PaintballCommand : BrigadierTabExecutor("paintball"){
AutostartCommand()
).then(
ArenaCommand()
).then(
ReloadConfigCommand()
).then(
literal<CommandSender?>("dev")
.then(
Expand Down
Original file line number Diff line number Diff line change
@@ -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<CommandSender, ReloadConfigCommand>() {
override fun getThis(): ReloadConfigCommand {
return this
}

override fun build(): CommandNode<CommandSender> {
return literal<CommandSender>("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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class TeamspawnCommand : ArgumentBuilder<CommandSender, TeamspawnCommand>() {
teamSpawnPos.direction = dir

team.spawnPos = teamSpawnPos
Paintball.gameConfig.save()

player.sendMessage(ThemeBuilder.themed(
"*Successfully* updated the spawnposition for team ${team.displayName}."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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()
}
}
Expand Down Expand Up @@ -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)
Expand All @@ -120,6 +117,7 @@ object Game {
Bukkit.unloadWorld(Paintball.gameConfig.lastArenaName, false)

Paintball.gameConfig.lastArenaName = randomWorldName
Paintball.gameConfig.save()
})
}
}
Expand All @@ -140,7 +138,6 @@ object Game {

Scores.createAndResetScores()
Countdown.checkAndStart()
Command.SINGLE_SUCCESS
}

fun start() {
Expand All @@ -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"
Expand All @@ -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!",
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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<GameConfig>("game") {

override val additionalConfigClasses: List<Class<out ConfigurationSerializable>> = 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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String, Any> = mutableMapOf()) : ConfigurationSerializable {

companion object {
fun registerConfigClasses() {
ConfigurationSerialization.registerClass(ThemeConfig::class.java)
}
}
class ThemeConfig(cfg: Map<String, Any> = mutableMapOf()) : ConfigObject<ThemeConfig>("theme") {

val default: ChatColor
val highlight: ChatColor
Expand Down
Original file line number Diff line number Diff line change
@@ -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<T : ConfigObject<T>> (private val key: String): ConfigurationSerializable {
companion object {
private var wasRegistered = mutableSetOf<String>()
}

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<Class<out ConfigurationSerializable>> = 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()
}
}

0 comments on commit 858718e

Please sign in to comment.