mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 05:30:55 +00:00
chaos: selection controller
This commit is contained in:
@ -3,37 +3,64 @@ package gay.pizza.foundation.chaos
|
|||||||
import gay.pizza.foundation.chaos.model.ChaosConfig
|
import gay.pizza.foundation.chaos.model.ChaosConfig
|
||||||
import gay.pizza.foundation.chaos.modules.ChaosModule
|
import gay.pizza.foundation.chaos.modules.ChaosModule
|
||||||
import gay.pizza.foundation.chaos.modules.ChaosModules
|
import gay.pizza.foundation.chaos.modules.ChaosModules
|
||||||
|
import net.kyori.adventure.text.Component
|
||||||
import org.bukkit.event.HandlerList
|
import org.bukkit.event.HandlerList
|
||||||
import org.bukkit.event.Listener
|
import org.bukkit.event.Listener
|
||||||
import org.bukkit.plugin.Plugin
|
import org.bukkit.plugin.Plugin
|
||||||
import java.util.concurrent.atomic.AtomicBoolean
|
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 state: AtomicBoolean = AtomicBoolean(false)
|
||||||
|
val selectorController = ChaosSelectorController(this, plugin)
|
||||||
|
|
||||||
private val allModules = ChaosModules.all(plugin)
|
val allModules = ChaosModules.all(plugin)
|
||||||
private var modules: List<ChaosModule> = emptyList()
|
private var allowedModules: List<ChaosModule> = emptyList()
|
||||||
|
private var activeModules = mutableSetOf<ChaosModule>()
|
||||||
|
|
||||||
fun load() {
|
fun load() {
|
||||||
if (state.get()) {
|
if (state.get()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
modules = allModules.filter { config.enable[it.id()] ?: true }
|
allowedModules = allModules.filter { config.enable[it.id()] ?: true }
|
||||||
modules.forEach { module ->
|
|
||||||
plugin.server.pluginManager.registerEvents(module, plugin)
|
|
||||||
module.load()
|
|
||||||
}
|
|
||||||
state.set(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() {
|
fun unload() {
|
||||||
if (!state.get()) {
|
if (!state.get()) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
modules.forEach { module ->
|
deactivateAll()
|
||||||
HandlerList.unregisterAll(module)
|
|
||||||
module.unload()
|
|
||||||
}
|
|
||||||
state.set(false)
|
state.set(false)
|
||||||
|
selectorController.cancel()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
@ -5,5 +5,6 @@ import kotlinx.serialization.Serializable
|
|||||||
@Serializable
|
@Serializable
|
||||||
class ChaosConfig(
|
class ChaosConfig(
|
||||||
val allowed: Boolean = true,
|
val allowed: Boolean = true,
|
||||||
val enable: Map<String, Boolean> = mapOf()
|
val enable: Map<String, Boolean> = mapOf(),
|
||||||
|
val selectionTimerTicks: Long
|
||||||
)
|
)
|
||||||
|
@ -5,6 +5,6 @@ import org.bukkit.event.Listener
|
|||||||
interface ChaosModule : Listener {
|
interface ChaosModule : Listener {
|
||||||
fun id(): String
|
fun id(): String
|
||||||
fun what(): String
|
fun what(): String
|
||||||
fun load() {}
|
fun activate() {}
|
||||||
fun unload() {}
|
fun deactivate() {}
|
||||||
}
|
}
|
||||||
|
@ -7,7 +7,7 @@ class TeleportAllEntitiesNearestPlayer(val plugin: Plugin) : ChaosModule {
|
|||||||
override fun id(): String = "teleport-all-entities-nearest-player"
|
override fun id(): String = "teleport-all-entities-nearest-player"
|
||||||
override fun what(): String = "Teleports all entities to the 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 (world in plugin.server.worlds) {
|
||||||
for (entity in world.entities) {
|
for (entity in world.entities) {
|
||||||
val player = entity.location.nearestPlayer()
|
val player = entity.location.nearestPlayer()
|
||||||
|
@ -4,3 +4,4 @@ allowed: false
|
|||||||
enable:
|
enable:
|
||||||
nearest-player-entity-spawn: true
|
nearest-player-entity-spawn: true
|
||||||
teleport-all-entities-nearest-player: true
|
teleport-all-entities-nearest-player: true
|
||||||
|
selectionTimerTicks: 100
|
||||||
|
Reference in New Issue
Block a user