FormatChat (untested) and module config command

This commit is contained in:
bizink
2025-06-15 18:31:14 +10:00
parent a5fd8286d5
commit 79d1f682a7
14 changed files with 381 additions and 157 deletions

View File

@ -3,6 +3,7 @@ package com.pobnellion.pobutils
import com.pobnellion.pobutils.modules.CmdModule
import com.pobnellion.pobutils.modules.ModuleBase
import com.pobnellion.pobutils.modules.disableTrample.DisableTrample
import com.pobnellion.pobutils.modules.formatChat.FormatChat
import com.pobnellion.pobutils.modules.hub.Hub
import com.pobnellion.pobutils.modules.noJoinMessage.NoJoinMessage
import com.pobnellion.pobutils.modules.portals.Portals
@ -22,14 +23,15 @@ class Pobutils : JavaPlugin() {
override fun onEnable() {
loadDefaultConfig()
CmdModule.register(this)
registerModule(Portals(this))
registerModule(NoJoinMessage(this))
registerModule(Sit(this)) // TODO: maybe sit when right click stairs?
registerModule(Spawn(this))
registerModule(Hub(this))
registerModule(DisableTrample(this))
registerModule(FormatChat(this))
CmdModule.register(this)
server.messenger.registerOutgoingPluginChannel(this, "BungeeCord")
logger.info("Registered ${availableModules.size} modules: [${availableModules.keys.joinToString()}]")
@ -66,12 +68,12 @@ class Pobutils : JavaPlugin() {
config.addDefault("modules.hub", false)
config.addDefault("modules.disableTrample", false)
config.addDefault("modules.tabList", false)
config.addDefault("modules.disableTNT", false)
config.addDefault("modules.formatChat", false)
config.addDefault("modules.disableTNT", false)
config.addDefault("modules.warp", false)
config.addDefault("modules.snowballDamage", false)
config.addDefault("modules.tabList", false)
config.addDefault("data.spawn.location", "")
config.addDefault("data.spawn.spawnOnJoin", false)

View File

@ -8,7 +8,6 @@ import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.plugin.java.JavaPlugin
@ -17,9 +16,10 @@ object CmdModule {
fun register(plugin: JavaPlugin) {
val command = Commands.literal("module")
.requires { source -> source.sender.hasPermission("pobutils.admin") }
.then(handleAction("enable", plugin))
.then(handleAction("disable", plugin))
.then(handleAction("reload", plugin))
.then(enable(plugin))
.then(disable(plugin))
.then(reload(plugin))
.then(config(plugin))
.build()
plugin.lifecycleManager.registerEventHandler(LifecycleEvents.COMMANDS) {commands ->
@ -27,70 +27,120 @@ object CmdModule {
}
}
private fun handleAction(action: String, plugin: JavaPlugin) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal(action)
private fun enable(plugin: JavaPlugin) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal("enable")
.then(Commands.argument("module", StringArgumentType.word())
.suggests { ctx, builder ->
when (action) {
"enable" -> Pobutils.getDisabledModuleNames().forEach { module -> builder.suggest(module) }
"disable" -> Pobutils.enabledModules.keys.forEach { module -> builder.suggest(module) }
"reload" -> Pobutils.enabledModules.keys.forEach { module -> builder.suggest(module) }
}
Pobutils.getDisabledModuleNames().forEach { module -> builder.suggest(module) }
return@suggests builder.buildFuture()
}
.executes { ctx ->
val moduleName = StringArgumentType.getString(ctx, "module")
val moduleName = StringArgumentType.getString(ctx, "module")
val module = Pobutils.availableModules[moduleName]
val module = Pobutils.availableModules[moduleName]
if (module == null) {
ctx.source.sender.sendMessage(Component.text("No module named '$moduleName'", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
if (Pobutils.isEnabled(moduleName)) {
ctx.source.sender.sendMessage(Component.text("Module '$moduleName' is already enabled", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
Pobutils.enabledModules[moduleName] = module
module.onEnable()
plugin.config.set("modules.$moduleName", true)
plugin.saveConfig()
plugin.server.onlinePlayers.forEach { player -> player.updateCommands() }
ctx.source.sender.sendMessage(Component.text("Module '$moduleName' enabled", NamedTextColor.YELLOW))
if (module == null) {
ctx.source.sender.sendMessage(Component.text("No module named '$moduleName'", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
})
}
private fun disable(plugin: JavaPlugin) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal("disable")
.then(Commands.argument("module", StringArgumentType.word())
.suggests { ctx, builder ->
Pobutils.enabledModules.keys.forEach { module -> builder.suggest(module) }
return@suggests builder.buildFuture()
}
.executes { ctx ->
val moduleName = StringArgumentType.getString(ctx, "module")
val module = Pobutils.availableModules[moduleName]
val message : TextComponent
when (action) {
"enable" -> {
if (Pobutils.isEnabled(moduleName)) {
message = Component.text("Module '$moduleName' is already enabled", NamedTextColor.RED)
}
else {
Pobutils.enabledModules[moduleName] = module
module.onEnable()
plugin.config.set("modules.$moduleName", true)
plugin.saveConfig()
message = Component.text("Module '$moduleName' enabled", NamedTextColor.YELLOW)
}
if (module == null) {
ctx.source.sender.sendMessage(Component.text("No module named '$moduleName'", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
"disable" -> {
if (!Pobutils.isEnabled(moduleName)) {
message = Component.text("Module '$moduleName' is already disabled", NamedTextColor.RED)
}
else {
Pobutils.enabledModules.remove(moduleName)
module.onDisable()
plugin.config.set("modules.$moduleName", false)
plugin.saveConfig()
message = Component.text("Module '$moduleName' disabled", NamedTextColor.YELLOW)
}
if (!Pobutils.isEnabled(moduleName)) {
ctx.source.sender.sendMessage(Component.text("Module '$moduleName' is already disabled", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
"reload" -> {
if (!Pobutils.isEnabled(moduleName)) {
message = Component.text("Module '$moduleName' is not currently enabled", NamedTextColor.RED)
}
else {
module.reload()
message = Component.text("Module '$moduleName' reloaded", NamedTextColor.YELLOW)
}
}
else -> message = Component.text("Unknown argument '$action'", NamedTextColor.RED)
Pobutils.enabledModules.remove(moduleName)
module.onDisable()
plugin.config.set("modules.$moduleName", false)
plugin.saveConfig()
plugin.server.onlinePlayers.forEach { player -> player.updateCommands() }
ctx.source.sender.sendMessage(Component.text("Module '$moduleName' disabled", NamedTextColor.YELLOW))
return@executes Command.SINGLE_SUCCESS
})
}
private fun reload(plugin: JavaPlugin) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal("reload")
.then(Commands.argument("module", StringArgumentType.word())
.suggests { ctx, builder ->
Pobutils.enabledModules.keys.forEach { module -> builder.suggest(module) }
return@suggests builder.buildFuture()
}
.executes { ctx ->
val moduleName = StringArgumentType.getString(ctx, "module")
val module = Pobutils.availableModules[moduleName]
plugin.server.onlinePlayers.forEach { player -> player.updateCommands() }
ctx.source.sender.sendMessage(message)
if (module == null) {
ctx.source.sender.sendMessage(Component.text("No module named '$moduleName'", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
if (!Pobutils.isEnabled(moduleName)) {
ctx.source.sender.sendMessage(Component.text("Module '$moduleName' is not currently enabled", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
module.reload()
plugin.server.onlinePlayers.forEach { player -> player.updateCommands() }
ctx.source.sender.sendMessage(Component.text("Module '$moduleName' reloaded", NamedTextColor.YELLOW))
return@executes Command.SINGLE_SUCCESS
})
}
private fun config(plugin: JavaPlugin) : LiteralArgumentBuilder<CommandSourceStack> {
val configCommand = Commands.literal("config")
.executes { ctx ->
for ((name, _) in Pobutils.availableModules) {
val enabledText = if (Pobutils.isEnabled(name))
Component.text("enabled", NamedTextColor.GREEN)
else
Component.text("disabled", NamedTextColor.RED)
ctx.source.sender.sendMessage(Component.text("$name: ", NamedTextColor.YELLOW).append(enabledText))
}
return@executes Command.SINGLE_SUCCESS
})
}
for ((_, module) in Pobutils.availableModules) {
val moduleConfigCommand = module.configCmd()
if (moduleConfigCommand != null)
configCommand.then(moduleConfigCommand)
}
return configCommand
}
}

View File

@ -1,11 +1,16 @@
package com.pobnellion.pobutils.modules
import com.mojang.brigadier.builder.ArgumentBuilder
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
abstract class ModuleBase(val plugin: JavaPlugin) {
abstract val name : String
abstract fun register()
abstract fun reload()
abstract fun onDisable()
abstract fun onEnable()
abstract fun configCmd() : LiteralArgumentBuilder<CommandSourceStack>?
}

View File

@ -1,7 +1,9 @@
package com.pobnellion.pobutils.modules.disableTrample
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.Material
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
@ -9,6 +11,7 @@ import org.bukkit.event.block.Action
import org.bukkit.event.player.PlayerInteractEvent
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
class DisableTrample(plugin: JavaPlugin) : ModuleBase(plugin) , Listener {
override val name: String = "disableTrample"
@ -17,10 +20,9 @@ class DisableTrample(plugin: JavaPlugin) : ModuleBase(plugin) , Listener {
}
override fun reload() { }
override fun onDisable() { }
override fun onEnable() { }
override fun configCmd(): LiteralArgumentBuilder<CommandSourceStack>? { return null }
@EventHandler
fun onTrample(event: PlayerInteractEvent) {

View File

@ -0,0 +1,59 @@
package com.pobnellion.pobutils.modules.formatChat
import com.mojang.brigadier.Command
import com.mojang.brigadier.arguments.BoolArgumentType
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
object CmdFormatChatConfig {
fun configCmd(plugin: JavaPlugin, formatChat: FormatChat) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal(formatChat.name)
.then(Commands.literal("serverAlias")
.then(Commands.argument("alias", StringArgumentType.word())
.executes { ctx ->
val alias = StringArgumentType.getString(ctx, "alias")
plugin.config.set("data.formatChat.serverAlias", alias)
formatChat.serverAlias = alias
plugin.saveConfig()
return@executes Command.SINGLE_SUCCESS
}))
.then(Commands.literal("messageFormat"))
.then(Commands.argument("format", StringArgumentType.greedyString())
.executes { ctx ->
val format = StringArgumentType.getString(ctx, "format")
plugin.config.set("data.formatChat.messageFormat", format)
formatChat.messageFormat = format
plugin.saveConfig()
return@executes Command.SINGLE_SUCCESS
})
.then(Commands.literal("formatMessageText")
.then(Commands.argument("shouldFormat", BoolArgumentType.bool())
.executes { ctx ->
val shouldFormat = BoolArgumentType.getBool(ctx, "shouldFormat")
plugin.config.set("data.formatChat.formatMessageText", shouldFormat)
formatChat.formatMessageText = shouldFormat
plugin.saveConfig()
return@executes Command.SINGLE_SUCCESS
}))
.executes { ctx ->
ctx.source.sender.sendMessage("FormatChat config:")
ctx.source.sender.sendMessage(Component.text("serverAlias: ", NamedTextColor.YELLOW)
.append(Component.text(formatChat.serverAlias, NamedTextColor.WHITE)))
ctx.source.sender.sendMessage(Component.text("messageFormat: ", NamedTextColor.YELLOW)
.append(Component.text(formatChat.messageFormat, NamedTextColor.WHITE)))
ctx.source.sender.sendMessage(Component.text("formatMessageText: ", NamedTextColor.YELLOW)
.append(Component.text(formatChat.formatMessageText.toString(), NamedTextColor.WHITE)))
return@executes Command.SINGLE_SUCCESS
}
}
}

View File

@ -0,0 +1,53 @@
package com.pobnellion.pobutils.modules.formatChat
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.event.player.AsyncChatDecorateEvent
import io.papermc.paper.event.player.AsyncChatEvent
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
class FormatChat(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
override val name: String = "formatChat"
lateinit var serverAlias: String
lateinit var messageFormat: String
var formatMessageText: Boolean = false
val renderer: Renderer = Renderer(this)
override fun register() {
plugin.server.pluginManager.registerEvents(this, plugin)
}
override fun reload() {
plugin.reloadConfig()
onEnable()
}
override fun onDisable() { }
override fun onEnable() {
serverAlias = plugin.config.getString("data.formatChat.serverAlias")!!
messageFormat = plugin.config.getString("data.formatChat.messageFormat")!!
formatMessageText = plugin.config.getBoolean("data.formatChat.formatMessageText")
}
override fun configCmd() : LiteralArgumentBuilder<CommandSourceStack>? {
return CmdFormatChatConfig.configCmd(plugin, this)
}
@EventHandler
fun onPlayerChat(event: AsyncChatEvent) {
if (Pobutils.Companion.isEnabled(this.name))
event.renderer(renderer)
}
@EventHandler
fun onChatDecorate(event: AsyncChatDecorateEvent) {
if (Pobutils.Companion.isEnabled(this.name))
event.result(renderer.previewRender(event.player()!!.displayName(), event.originalMessage()))
}
}

View File

@ -0,0 +1,32 @@
package com.pobnellion.pobutils.modules.formatChat
import io.papermc.paper.chat.ChatRenderer
import net.kyori.adventure.audience.Audience
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.TextComponent
import net.kyori.adventure.text.minimessage.MiniMessage
import net.kyori.adventure.text.minimessage.tag.resolver.Placeholder
import net.kyori.adventure.text.minimessage.tag.resolver.TagResolver
import net.kyori.adventure.text.minimessage.tag.standard.StandardTags
import org.bukkit.entity.Player
class Renderer(val formatChat: FormatChat) : ChatRenderer {
override fun render(source: Player, sourceDisplayName: Component, message: Component, viewer: Audience): Component {
return message
}
fun previewRender(sourceDisplayName: Component, message: Component): Component {
var builder = TagResolver.builder()
.resolver(StandardTags.defaults())
.resolver(Placeholder.component("username", sourceDisplayName))
.resolver(Placeholder.unparsed("server_alias", formatChat.serverAlias))
builder = if (formatChat.formatMessageText)
builder.resolver(Placeholder.parsed("message", (message as TextComponent).content()))
else
builder.resolver(Placeholder.component("message", message))
val miniMessage = MiniMessage.builder().tags(builder.build()).build()
return miniMessage.deserialize(formatChat.messageFormat) as TextComponent
}
}

View File

@ -2,8 +2,10 @@ package com.pobnellion.pobutils.modules.hub
import com.google.common.io.ByteStreams
import com.mojang.brigadier.Command
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents
import org.bukkit.entity.Player
@ -32,8 +34,7 @@ class Hub(plugin: JavaPlugin) : ModuleBase(plugin) {
}
override fun reload() { }
override fun onDisable() { }
override fun onEnable() { }
override fun configCmd(): LiteralArgumentBuilder<CommandSourceStack>? { return null }
}

View File

@ -1,13 +1,16 @@
package com.pobnellion.pobutils.modules.noJoinMessage
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
class NoJoinMessage(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
override val name: String = "noJoinMessage"
@ -18,6 +21,7 @@ class NoJoinMessage(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
override fun reload() {}
override fun onDisable() {}
override fun onEnable() {}
override fun configCmd(): LiteralArgumentBuilder<CommandSourceStack>? { return null }
@EventHandler
fun onPlayerJoin(event: PlayerJoinEvent) {

View File

@ -1,8 +1,10 @@
package com.pobnellion.pobutils.modules.portals
import com.google.common.io.ByteStreams
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.math.BlockPosition
import org.bukkit.Location
import org.bukkit.NamespacedKey
@ -44,6 +46,10 @@ class Portals(plugin: JavaPlugin) : ModuleBase(plugin), Listener, PluginMessageL
refreshServerList(plugin.server.onlinePlayers.first())
}
override fun configCmd() : LiteralArgumentBuilder<CommandSourceStack>? {
return null
}
override fun onDisable() {
portals.clear()
CmdPortal.serverList.clear()

View File

@ -1,7 +1,9 @@
package com.pobnellion.pobutils.modules.sit
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.entity.Entity
import org.bukkit.entity.EntityType
import org.bukkit.entity.Player
@ -11,6 +13,7 @@ import org.bukkit.event.entity.EntityDismountEvent
import org.bukkit.event.player.PlayerQuitEvent
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
class Sit(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
override val name: String = "sit"
private val sittingPlayers: MutableMap<Player, Entity> = mutableMapOf()
@ -25,6 +28,7 @@ class Sit(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
}
override fun onEnable() { }
override fun configCmd(): LiteralArgumentBuilder<CommandSourceStack>? { return null }
override fun onDisable() {
for (player in sittingPlayers.keys)

View File

@ -1,14 +1,8 @@
package com.pobnellion.pobutils.modules.spawn
import com.mojang.brigadier.Command
import com.mojang.brigadier.arguments.BoolArgumentType
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import io.papermc.paper.command.brigadier.argument.ArgumentTypes
import io.papermc.paper.command.brigadier.argument.resolvers.BlockPositionResolver
import io.papermc.paper.plugin.lifecycle.event.types.LifecycleEvents
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
@ -20,8 +14,6 @@ object CmdSpawn {
fun register(plugin: JavaPlugin, spawn: Spawn) {
val command = Commands.literal("spawn")
.requires { source -> Pobutils.isEnabled(spawn.name) && source.sender is Player }
.then(setSpawn(plugin, spawn))
.then(config(plugin))
.executes { ctx ->
if (spawn.spawnLocation == null)
ctx.source.sender.sendMessage(Component.text("A spawn location has not been set", NamedTextColor.RED))
@ -36,89 +28,4 @@ object CmdSpawn {
commands.registrar().register(command)
}
}
private fun setSpawn(plugin: JavaPlugin, spawn: Spawn) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal("set")
.requires { source -> source.sender.hasPermission("pobutils.admin") }
.then(Commands.argument("position", ArgumentTypes.blockPosition())
.then(Commands.argument("direction", StringArgumentType.word())
.suggests { ctx, builder ->
builder.suggest("north")
builder.suggest("south")
builder.suggest("east")
builder.suggest("west")
return@suggests builder.buildFuture()
}.executes { ctx ->
val position = ctx.getArgument("position", BlockPositionResolver::class.java).resolve(ctx.source)
val direction = StringArgumentType.getString(ctx, "direction")
val location = position.toLocation((ctx.source.sender as Player).world)
location.pitch = 0.0f
when (direction.lowercase()) {
"north" -> location.yaw = 180f;
"east" -> location.yaw = 270f;
"south" -> location.yaw = 0f;
"west" -> location.yaw = 90f;
else -> {
ctx.source.sender.sendMessage(Component.text("Unknown direction: $direction", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
}
spawn.spawnLocation = location
plugin.config.set("data.spawn.location", location)
plugin.saveConfig()
ctx.source.sender.sendMessage(Component.text("Set spawn location to ${location.x} ${location.y} ${location.z}", NamedTextColor.YELLOW))
return@executes Command.SINGLE_SUCCESS
}))
}
private fun config(plugin: JavaPlugin) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal("config")
.requires { source -> source.sender.hasPermission("pobutils.admin") }
.then(Commands.argument("variable", StringArgumentType.word())
.suggests { ctx, builder ->
builder.suggest("spawnOnJoin")
builder.suggest("spawnOnDeath")
return@suggests builder.buildFuture()
}
.then(Commands.argument("value", BoolArgumentType.bool())
.executes { ctx ->
val variable = StringArgumentType.getString(ctx, "variable")
val value = BoolArgumentType.getBool(ctx, "value")
when (variable) {
"spawnOnJoin" -> plugin.config.set("data.spawn.spawnOnJoin", value)
"spawnOnDeath" -> plugin.config.set("data.spawn.spawnOnDeath", value)
else -> {
ctx.source.sender.sendMessage(Component.text("Unknown config variable: $variable", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
}
plugin.saveConfig()
plugin.reloadConfig()
ctx.source.sender.sendMessage(Component.text("$variable set to: $value", NamedTextColor.YELLOW))
return@executes Command.SINGLE_SUCCESS
}))
.executes { ctx ->
val location = plugin.config.getLocation("data.spawn.location")
ctx.source.sender.sendMessage("Spawn config:")
ctx.source.sender.sendMessage(Component.text("location: ", NamedTextColor.YELLOW)
.append(Component.text("${location?.x} ${location?.y} ${location?.z}", NamedTextColor.WHITE)))
ctx.source.sender.sendMessage(Component.text("spawnOnJoin: ", NamedTextColor.YELLOW)
.append(Component.text(plugin.config.getString("data.spawn.spawnOnJoin").orEmpty(), NamedTextColor.WHITE)))
ctx.source.sender.sendMessage(Component.text("spawnOnDeath: ", NamedTextColor.YELLOW)
.append(Component.text(plugin.config.getString("data.spawn.spawnOnDeath").orEmpty(), NamedTextColor.WHITE)))
return@executes Command.SINGLE_SUCCESS
}
}
}

View File

@ -0,0 +1,94 @@
package com.pobnellion.pobutils.modules.spawn
import com.mojang.brigadier.Command
import com.mojang.brigadier.arguments.BoolArgumentType
import com.mojang.brigadier.arguments.StringArgumentType
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import io.papermc.paper.command.brigadier.CommandSourceStack
import io.papermc.paper.command.brigadier.Commands
import io.papermc.paper.command.brigadier.argument.ArgumentTypes
import io.papermc.paper.command.brigadier.argument.resolvers.BlockPositionResolver
import net.kyori.adventure.text.Component
import net.kyori.adventure.text.format.NamedTextColor
import org.bukkit.entity.Player
import org.bukkit.plugin.java.JavaPlugin
@Suppress("UnstableApiUsage")
object CmdSpawnConfig {
fun configCmd(plugin: JavaPlugin, spawn: Spawn) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal(spawn.name)
.then(setLocation(plugin, spawn))
.then(setVariable(plugin,"spawnOnJoin"))
.then(setVariable(plugin,"spawnOnDeath"))
.executes { ctx ->
val location = plugin.config.getLocation("data.spawn.location")
ctx.source.sender.sendMessage("Spawn config:")
ctx.source.sender.sendMessage(Component.text("location: ", NamedTextColor.YELLOW)
.append(Component.text("${location?.x} ${location?.y} ${location?.z}", NamedTextColor.WHITE)))
ctx.source.sender.sendMessage(Component.text("spawnOnJoin: ", NamedTextColor.YELLOW)
.append(Component.text(plugin.config.getString("data.spawn.spawnOnJoin").orEmpty(), NamedTextColor.WHITE)))
ctx.source.sender.sendMessage(Component.text("spawnOnDeath: ", NamedTextColor.YELLOW)
.append(Component.text(plugin.config.getString("data.spawn.spawnOnDeath").orEmpty(), NamedTextColor.WHITE)))
return@executes Command.SINGLE_SUCCESS
}
}
private fun setLocation(plugin: JavaPlugin, spawn: Spawn) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal("location")
.then(Commands.argument("position", ArgumentTypes.blockPosition())
.then(Commands.argument("direction", StringArgumentType.word())
.suggests { ctx, builder ->
builder.suggest("north")
builder.suggest("south")
builder.suggest("east")
builder.suggest("west")
return@suggests builder.buildFuture()
}.executes { ctx ->
val position = ctx.getArgument("position", BlockPositionResolver::class.java).resolve(ctx.source)
val direction = StringArgumentType.getString(ctx, "direction")
val location = position.toLocation((ctx.source.sender as Player).world)
location.pitch = 0.0f
when (direction.lowercase()) {
"north" -> location.yaw = 180f
"east" -> location.yaw = 270f
"south" -> location.yaw = 0f
"west" -> location.yaw = 90f
else -> {
ctx.source.sender.sendMessage(Component.text("Unknown direction: $direction", NamedTextColor.RED))
return@executes Command.SINGLE_SUCCESS
}
}
spawn.spawnLocation = location
plugin.config.set("data.spawn.location", location)
plugin.saveConfig()
ctx.source.sender.sendMessage(Component.text("Set spawn location to ${location.x} ${location.y} ${location.z}", NamedTextColor.YELLOW))
return@executes Command.SINGLE_SUCCESS
}))
}
private fun setVariable(plugin: JavaPlugin, variableName: String) : LiteralArgumentBuilder<CommandSourceStack> {
return Commands.literal(variableName)
.then(Commands.argument("value", BoolArgumentType.bool())
.executes { ctx ->
val value = BoolArgumentType.getBool(ctx, "value")
when (variableName) {
"spawnOnJoin" -> plugin.config.set("data.spawn.spawnOnJoin", value)
"spawnOnDeath" -> plugin.config.set("data.spawn.spawnOnDeath", value)
}
plugin.saveConfig()
plugin.reloadConfig()
ctx.source.sender.sendMessage(Component.text("$variableName set to: $value", NamedTextColor.YELLOW))
return@executes Command.SINGLE_SUCCESS
})
}
}

View File

@ -1,16 +1,17 @@
package com.pobnellion.pobutils.modules.spawn
import com.mojang.brigadier.builder.LiteralArgumentBuilder
import com.pobnellion.pobutils.Pobutils
import com.pobnellion.pobutils.modules.ModuleBase
import io.papermc.paper.command.brigadier.CommandSourceStack
import org.bukkit.Location
import org.bukkit.event.EventHandler
import org.bukkit.event.Listener
import org.bukkit.event.entity.PlayerDeathEvent
import org.bukkit.event.player.PlayerJoinEvent
import org.bukkit.event.player.PlayerRespawnEvent
import org.bukkit.plugin.java.JavaPlugin
import org.spigotmc.event.player.PlayerSpawnLocationEvent
@Suppress("UnstableApiUsage")
class Spawn(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
override val name: String = "spawn"
var spawnLocation: Location? = null
@ -34,6 +35,10 @@ class Spawn(plugin: JavaPlugin) : ModuleBase(plugin), Listener {
spawnLocation = plugin.config.getLocation("data.spawn.location")
}
override fun configCmd(): LiteralArgumentBuilder<CommandSourceStack>? {
return CmdSpawnConfig.configCmd(plugin, this)
}
@EventHandler
fun onPlayerRespawn(event: PlayerRespawnEvent) {
if (Pobutils.isEnabled(this.name)