Chaos Plugin

This commit is contained in:
Alex Zenla 2023-01-28 00:21:14 -08:00
parent c39c5a99d5
commit 2119b89ae2
Signed by: alex
GPG Key ID: C0780728420EBFE5
13 changed files with 200 additions and 0 deletions

View File

@ -0,0 +1,3 @@
dependencies {
compileOnly(project(":foundation-core"))
}

View File

@ -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<ChaosModule> = 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)
}
}

View File

@ -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<out String>
): 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
}
}

View File

@ -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) }

View File

@ -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<FoundationChaosPlugin>(
slF4JLogger,
foundation.pluginDataPath,
"chaos.yaml"
)
config = Yaml.default.decodeFromStream(ChaosConfig.serializer(), configPath.inputStream())
val chaosCommand = getCommand("chaos")!!
chaosCommand.setExecutor(ChaosToggleCommand())
}
}

View File

@ -0,0 +1,9 @@
package gay.pizza.foundation.chaos.model
import kotlinx.serialization.Serializable
@Serializable
class ChaosConfig(
val allowed: Boolean = true,
val enable: Map<String, Boolean> = mapOf()
)

View File

@ -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() {}
}

View File

@ -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)
)
}

View File

@ -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)
}
}
}
}

View File

@ -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)
}
}
}
}
}

View File

@ -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

View File

@ -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

View File

@ -11,4 +11,5 @@ pluginManagement {
include(
":foundation-core",
":foundation-bifrost",
":foundation-chaos",
)