From 2119b89ae2fe61c1b4380c3e29cb1a7ccbfa58df Mon Sep 17 00:00:00 2001 From: Alex Zenla Date: Sat, 28 Jan 2023 00:21:14 -0800 Subject: [PATCH] Chaos Plugin --- foundation-chaos/build.gradle.kts | 3 ++ .../pizza/foundation/chaos/ChaosController.kt | 39 +++++++++++++++++++ .../foundation/chaos/ChaosToggleCommand.kt | 30 ++++++++++++++ .../gay/pizza/foundation/chaos/ChaosTools.kt | 7 ++++ .../foundation/chaos/FoundationChaosPlugin.kt | 29 ++++++++++++++ .../foundation/chaos/model/ChaosConfig.kt | 9 +++++ .../foundation/chaos/modules/ChaosModule.kt | 10 +++++ .../foundation/chaos/modules/ChaosModules.kt | 10 +++++ .../chaos/modules/NearestPlayerEntitySpawn.kt | 21 ++++++++++ .../TeleportAllEntitiesNearestPlayer.kt | 20 ++++++++++ .../src/main/resources/chaos.yaml | 6 +++ .../src/main/resources/plugin.yml | 15 +++++++ settings.gradle.kts | 1 + 13 files changed, 200 insertions(+) create mode 100644 foundation-chaos/build.gradle.kts create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosController.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosToggleCommand.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/model/ChaosConfig.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModule.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/NearestPlayerEntitySpawn.kt create mode 100644 foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/TeleportAllEntitiesNearestPlayer.kt create mode 100644 foundation-chaos/src/main/resources/chaos.yaml create mode 100644 foundation-chaos/src/main/resources/plugin.yml diff --git a/foundation-chaos/build.gradle.kts b/foundation-chaos/build.gradle.kts new file mode 100644 index 0000000..4610ec9 --- /dev/null +++ b/foundation-chaos/build.gradle.kts @@ -0,0 +1,3 @@ +dependencies { + compileOnly(project(":foundation-core")) +} diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosController.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosController.kt new file mode 100644 index 0000000..ece553e --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosController.kt @@ -0,0 +1,39 @@ +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 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 { + val state: AtomicBoolean = AtomicBoolean(false) + + private val allModules = ChaosModules.all(plugin) + private var modules: List = emptyList() + + 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() + } + state.set(true) + } + + fun unload() { + if (!state.get()) { + return + } + modules.forEach { module -> + HandlerList.unregisterAll(module) + module.unload() + } + state.set(false) + } +} diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosToggleCommand.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosToggleCommand.kt new file mode 100644 index 0000000..cf3ef83 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosToggleCommand.kt @@ -0,0 +1,30 @@ +package gay.pizza.foundation.chaos + +import net.kyori.adventure.text.Component +import org.bukkit.command.Command +import org.bukkit.command.CommandExecutor +import org.bukkit.command.CommandSender + +class ChaosToggleCommand : CommandExecutor { + override fun onCommand( + sender: CommandSender, + command: Command, + label: String, + args: Array + ): Boolean { + val plugin = sender.server.pluginManager.getPlugin("Foundation-Chaos") as FoundationChaosPlugin + if (!plugin.config.allowed) { + sender.sendMessage("Chaos is not allowed.") + return true + } + val controller = plugin.controller + if (controller.state.get()) { + controller.unload() + sender.server.broadcast(Component.text("Chaos Mode Disabled")) + } else { + controller.load() + sender.server.broadcast(Component.text("Chaos Mode Enabled")) + } + return true + } +} \ No newline at end of file diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt new file mode 100644 index 0000000..1e6a538 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/ChaosTools.kt @@ -0,0 +1,7 @@ +package gay.pizza.foundation.chaos + +import org.bukkit.Location +import org.bukkit.entity.Player + +fun Location.nearestPlayer(): Player? = + world?.players?.minByOrNull { it.location.distance(this) } diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt new file mode 100644 index 0000000..72e7c16 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/FoundationChaosPlugin.kt @@ -0,0 +1,29 @@ +package gay.pizza.foundation.chaos + +import com.charleskorn.kaml.Yaml +import gay.pizza.foundation.chaos.model.ChaosConfig +import gay.pizza.foundation.core.FoundationCorePlugin +import gay.pizza.foundation.core.Util +import org.bukkit.plugin.java.JavaPlugin +import kotlin.io.path.inputStream + +class FoundationChaosPlugin : JavaPlugin() { + lateinit var config: ChaosConfig + + val controller by lazy { + ChaosController(this, config) + } + + override fun onEnable() { + val foundation = server.pluginManager.getPlugin("Foundation") as FoundationCorePlugin + slF4JLogger.info("Plugin data path: ${foundation.pluginDataPath}") + val configPath = Util.copyDefaultConfig( + slF4JLogger, + foundation.pluginDataPath, + "chaos.yaml" + ) + config = Yaml.default.decodeFromStream(ChaosConfig.serializer(), configPath.inputStream()) + val chaosCommand = getCommand("chaos")!! + chaosCommand.setExecutor(ChaosToggleCommand()) + } +} diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/model/ChaosConfig.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/model/ChaosConfig.kt new file mode 100644 index 0000000..5a3bf10 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/model/ChaosConfig.kt @@ -0,0 +1,9 @@ +package gay.pizza.foundation.chaos.model + +import kotlinx.serialization.Serializable + +@Serializable +class ChaosConfig( + val allowed: Boolean = true, + val enable: Map = mapOf() +) diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModule.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModule.kt new file mode 100644 index 0000000..b141f2d --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModule.kt @@ -0,0 +1,10 @@ +package gay.pizza.foundation.chaos.modules + +import org.bukkit.event.Listener + +interface ChaosModule : Listener { + fun id(): String + fun what(): String + fun load() {} + fun unload() {} +} diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt new file mode 100644 index 0000000..d040e89 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/ChaosModules.kt @@ -0,0 +1,10 @@ +package gay.pizza.foundation.chaos.modules + +import org.bukkit.plugin.Plugin + +object ChaosModules { + fun all(plugin: Plugin) = listOf( + NearestPlayerEntitySpawn(plugin), + TeleportAllEntitiesNearestPlayer(plugin) + ) +} diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/NearestPlayerEntitySpawn.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/NearestPlayerEntitySpawn.kt new file mode 100644 index 0000000..df2b4c0 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/NearestPlayerEntitySpawn.kt @@ -0,0 +1,21 @@ +package gay.pizza.foundation.chaos.modules + +import gay.pizza.foundation.chaos.nearestPlayer +import org.bukkit.event.EventHandler +import org.bukkit.event.entity.EntitySpawnEvent +import org.bukkit.plugin.Plugin + +class NearestPlayerEntitySpawn(val plugin: Plugin) : ChaosModule { + override fun id(): String = "nearest-player-entity-spawn" + override fun what(): String = "Teleports all entities on spawn to the nearest player." + + @EventHandler + fun onMobSpawn(e: EntitySpawnEvent) { + val player = e.location.nearestPlayer() + if (player != null) { + e.entity.server.scheduler.runTask(plugin) { -> + e.entity.teleport(player) + } + } + } +} \ No newline at end of file diff --git a/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/TeleportAllEntitiesNearestPlayer.kt b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/TeleportAllEntitiesNearestPlayer.kt new file mode 100644 index 0000000..ec0a096 --- /dev/null +++ b/foundation-chaos/src/main/kotlin/gay/pizza/foundation/chaos/modules/TeleportAllEntitiesNearestPlayer.kt @@ -0,0 +1,20 @@ +package gay.pizza.foundation.chaos.modules + +import gay.pizza.foundation.chaos.nearestPlayer +import org.bukkit.plugin.Plugin + +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() { + for (world in plugin.server.worlds) { + for (entity in world.entities) { + val player = entity.location.nearestPlayer() + if (player != null) { + entity.teleport(player) + } + } + } + } +} \ No newline at end of file diff --git a/foundation-chaos/src/main/resources/chaos.yaml b/foundation-chaos/src/main/resources/chaos.yaml new file mode 100644 index 0000000..e3773cf --- /dev/null +++ b/foundation-chaos/src/main/resources/chaos.yaml @@ -0,0 +1,6 @@ +# Whether enabling the chaos mode is allowed. +allowed: false +# Chaos modules enablement. +enable: + nearest-player-entity-spawn: true + teleport-all-entities-nearest-player: true diff --git a/foundation-chaos/src/main/resources/plugin.yml b/foundation-chaos/src/main/resources/plugin.yml new file mode 100644 index 0000000..bafb4d8 --- /dev/null +++ b/foundation-chaos/src/main/resources/plugin.yml @@ -0,0 +1,15 @@ +name: Foundation-Chaos +version: '${version}' +main: gay.pizza.foundation.chaos.FoundationChaosPlugin +api-version: 1.18 +prefix: Foundation-Chaos +load: STARTUP +depend: + - Foundation +authors: + - kubelet +commands: + chaos: + description: Chaos Toggle + usage: /chaos + permission: foundation.command.chaos diff --git a/settings.gradle.kts b/settings.gradle.kts index e7f3c9d..62799c7 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -11,4 +11,5 @@ pluginManagement { include( ":foundation-core", ":foundation-bifrost", + ":foundation-chaos", )