mirror of
https://github.com/GayPizzaSpecifications/foundation.git
synced 2025-08-03 05:30:55 +00:00
Chaos Plugin
This commit is contained in:
3
foundation-chaos/build.gradle.kts
Normal file
3
foundation-chaos/build.gradle.kts
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
dependencies {
|
||||||
|
compileOnly(project(":foundation-core"))
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
@ -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) }
|
@ -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())
|
||||||
|
}
|
||||||
|
}
|
@ -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()
|
||||||
|
)
|
@ -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() {}
|
||||||
|
}
|
@ -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)
|
||||||
|
)
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
6
foundation-chaos/src/main/resources/chaos.yaml
Normal file
6
foundation-chaos/src/main/resources/chaos.yaml
Normal 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
|
15
foundation-chaos/src/main/resources/plugin.yml
Normal file
15
foundation-chaos/src/main/resources/plugin.yml
Normal 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
|
@ -11,4 +11,5 @@ pluginManagement {
|
|||||||
include(
|
include(
|
||||||
":foundation-core",
|
":foundation-core",
|
||||||
":foundation-bifrost",
|
":foundation-bifrost",
|
||||||
|
":foundation-chaos",
|
||||||
)
|
)
|
||||||
|
Reference in New Issue
Block a user