chaos: selection controller

This commit is contained in:
2023-01-28 14:05:29 -08:00
parent d762bbc056
commit 6c16884ae2
6 changed files with 70 additions and 16 deletions

View File

@ -3,37 +3,64 @@ package gay.pizza.foundation.chaos
import gay.pizza.foundation.chaos.model.ChaosConfig
import gay.pizza.foundation.chaos.modules.ChaosModule
import gay.pizza.foundation.chaos.modules.ChaosModules
import net.kyori.adventure.text.Component
import org.bukkit.event.HandlerList
import org.bukkit.event.Listener
import org.bukkit.plugin.Plugin
import java.util.concurrent.atomic.AtomicBoolean
class ChaosController(private val plugin: Plugin, val config: ChaosConfig) : Listener {
class ChaosController(val plugin: Plugin, val config: ChaosConfig) : Listener {
val state: AtomicBoolean = AtomicBoolean(false)
val selectorController = ChaosSelectorController(this, plugin)
private val allModules = ChaosModules.all(plugin)
private var modules: List<ChaosModule> = emptyList()
val allModules = ChaosModules.all(plugin)
private var allowedModules: List<ChaosModule> = emptyList()
private var activeModules = mutableSetOf<ChaosModule>()
fun load() {
if (state.get()) {
return
}
modules = allModules.filter { config.enable[it.id()] ?: true }
modules.forEach { module ->
plugin.server.pluginManager.registerEvents(module, plugin)
module.load()
}
allowedModules = allModules.filter { config.enable[it.id()] ?: true }
state.set(true)
selectorController.schedule()
}
fun activateAll() {
for (module in allowedModules) {
if (activeModules.contains(module)) {
continue
}
activate(module)
}
}
fun activate(module: ChaosModule) {
plugin.server.pluginManager.registerEvents(module, plugin)
module.activate()
activeModules.add(module)
plugin.server.broadcast(Component.text("Chaos Module Activated: ${module.id()}"))
}
fun deactivate(module: ChaosModule) {
HandlerList.unregisterAll(module)
module.deactivate()
activeModules.remove(module)
plugin.server.broadcast(Component.text("Chaos Module Deactivated: ${module.id()}"))
}
fun deactivateAll() {
for (module in activeModules.toList()) {
deactivate(module)
}
}
fun unload() {
if (!state.get()) {
return
}
modules.forEach { module ->
HandlerList.unregisterAll(module)
module.unload()
}
deactivateAll()
state.set(false)
selectorController.cancel()
}
}

View File

@ -0,0 +1,25 @@
package gay.pizza.foundation.chaos
import org.bukkit.plugin.Plugin
import org.bukkit.scheduler.BukkitTask
class ChaosSelectorController(val controller: ChaosController, val plugin: Plugin) {
var task: BukkitTask? = null
fun schedule() {
cancel()
task = plugin.server.scheduler.runTaskTimer(controller.plugin, { ->
select()
}, controller.config.selectionTimerTicks, controller.config.selectionTimerTicks)
}
fun select() {
controller.deactivateAll()
val module = controller.allModules.random()
controller.activate(module)
}
fun cancel() {
task?.cancel()
}
}

View File

@ -5,5 +5,6 @@ import kotlinx.serialization.Serializable
@Serializable
class ChaosConfig(
val allowed: Boolean = true,
val enable: Map<String, Boolean> = mapOf()
val enable: Map<String, Boolean> = mapOf(),
val selectionTimerTicks: Long
)

View File

@ -5,6 +5,6 @@ import org.bukkit.event.Listener
interface ChaosModule : Listener {
fun id(): String
fun what(): String
fun load() {}
fun unload() {}
fun activate() {}
fun deactivate() {}
}

View File

@ -7,7 +7,7 @@ class TeleportAllEntitiesNearestPlayer(val plugin: Plugin) : ChaosModule {
override fun id(): String = "teleport-all-entities-nearest-player"
override fun what(): String = "Teleports all entities to the nearest player."
override fun load() {
override fun activate() {
for (world in plugin.server.worlds) {
for (entity in world.entities) {
val player = entity.location.nearestPlayer()

View File

@ -4,3 +4,4 @@ allowed: false
enable:
nearest-player-entity-spawn: true
teleport-all-entities-nearest-player: true
selectionTimerTicks: 100