From 9933aa78205a5a7431f5e7b7717fc2e0c4f8d7a0 Mon Sep 17 00:00:00 2001 From: Hare Date: Sun, 7 Dec 2025 06:55:22 +0900 Subject: [PATCH] =?UTF-8?q?feat:=20kommand-lib=E5=AF=BE=E5=BF=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 4 +- kommand-lib | 2 +- .../net/hareworks/npc-mannequin/Plugin.kt | 16 +- .../commands/MannequinCommands.kt | 204 +++++++++++++----- .../mannequin/MannequinSettings.kt | 15 +- .../service/MannequinListener.kt | 59 +++++ .../service/MannequinRegistry.kt | 4 +- .../npc-mannequin/storage/MannequinStorage.kt | 6 +- 8 files changed, 249 insertions(+), 61 deletions(-) create mode 100644 src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinListener.kt diff --git a/.gitignore b/.gitignore index 1b6985c..8e774d4 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ -# Ignore Gradle project-specific cache directory .gradle - -# Ignore Gradle build output directory +bin build diff --git a/kommand-lib b/kommand-lib index 6c62d33..3835c9b 160000 --- a/kommand-lib +++ b/kommand-lib @@ -1 +1 @@ -Subproject commit 6c62d3306e2cc0e0fefe8ec7fb9b64a47caae3cb +Subproject commit 3835c9b9e2d488681b39f4cfd827e3e3371a1e1b diff --git a/src/main/kotlin/net/hareworks/npc-mannequin/Plugin.kt b/src/main/kotlin/net/hareworks/npc-mannequin/Plugin.kt index 8c22825..aa3173a 100644 --- a/src/main/kotlin/net/hareworks/npc-mannequin/Plugin.kt +++ b/src/main/kotlin/net/hareworks/npc-mannequin/Plugin.kt @@ -1,22 +1,33 @@ package net.hareworks.npc_mannequin import net.hareworks.kommand_lib.KommandLib +import java.util.logging.Level import net.hareworks.npc_mannequin.commands.MannequinCommands import net.hareworks.npc_mannequin.service.MannequinController +import net.hareworks.npc_mannequin.service.MannequinListener import net.hareworks.npc_mannequin.service.MannequinRegistry import net.hareworks.npc_mannequin.storage.MannequinStorage +import net.hareworks.permits_lib.PermitsLib +import net.hareworks.permits_lib.bukkit.MutationSession import org.bukkit.plugin.ServicePriority import org.bukkit.plugin.java.JavaPlugin class Plugin : JavaPlugin() { private var kommand: KommandLib? = null private lateinit var registry: MannequinRegistry + private var permissionSession: MutationSession? = null override fun onEnable() { val storage = MannequinStorage(this) val controller = MannequinController(this) registry = MannequinRegistry(this, storage, controller) - kommand = MannequinCommands(this, registry).register() + permissionSession = runCatching { PermitsLib.session(this) } + .onFailure { logger.log(Level.WARNING, "Failed to acquire permits session", it) } + .getOrNull() + + server.pluginManager.registerEvents(MannequinListener(this, registry), this) + + kommand = MannequinCommands.register(this, registry, permissionSession) server.servicesManager.register(MannequinRegistry::class.java, registry, this, ServicePriority.Normal) logger.info("Loaded ${registry.all().size} mannequin definitions.") } @@ -24,5 +35,8 @@ class Plugin : JavaPlugin() { override fun onDisable() { server.servicesManager.unregisterAll(this) kommand?.unregister() + permissionSession?.clearAll() + permissionSession = null + kommand = null } } diff --git a/src/main/kotlin/net/hareworks/npc-mannequin/commands/MannequinCommands.kt b/src/main/kotlin/net/hareworks/npc-mannequin/commands/MannequinCommands.kt index 9a9e571..5b35cfc 100644 --- a/src/main/kotlin/net/hareworks/npc-mannequin/commands/MannequinCommands.kt +++ b/src/main/kotlin/net/hareworks/npc-mannequin/commands/MannequinCommands.kt @@ -1,5 +1,6 @@ package net.hareworks.npc_mannequin.commands +import java.util.Locale import net.hareworks.kommand_lib.KommandLib import net.hareworks.kommand_lib.context.KommandContext import net.hareworks.kommand_lib.kommand @@ -9,8 +10,10 @@ import net.hareworks.npc_mannequin.mannequin.StoredLocation import net.hareworks.npc_mannequin.mannequin.StoredProfile import net.hareworks.npc_mannequin.service.MannequinRegistry import net.hareworks.npc_mannequin.text.TextSerializers -import net.hareworks.permits_lib.PermitsLib +import net.hareworks.permits_lib.bukkit.MutationSession import net.kyori.adventure.text.Component +import net.kyori.adventure.text.event.ClickEvent +import net.kyori.adventure.text.event.HoverEvent import net.kyori.adventure.text.format.NamedTextColor import org.bukkit.entity.Entity import org.bukkit.entity.Mannequin @@ -18,25 +21,25 @@ import org.bukkit.entity.Player import org.bukkit.entity.Pose import org.bukkit.inventory.MainHand import org.bukkit.plugin.java.JavaPlugin -import java.util.Locale -class MannequinCommands( - private val plugin: JavaPlugin, - private val registry: MannequinRegistry -) { - fun register(): KommandLib = kommand(plugin) { +object MannequinCommands { + fun register( + plugin: JavaPlugin, + registry: MannequinRegistry, + permissionSession: MutationSession? + ): KommandLib = kommand(plugin) { permissions { - namespace = "hareworks" + namespace = "npc-mannequin" rootSegment = "command" defaultDescription { ctx -> "Allows /${ctx.commandName} ${ctx.path.joinToString(" ")}" } - session { PermitsLib.session(it) } + permissionSession?.let { session(it) } } command("mannequin", listOf("mnpc", "mnq", "npcmannequin")) { description = "Register and manage mannequin NPCs" - permission = "hareworks.command.mannequin" + permission = "npc-mannequin.command.mannequin" executes { listMannequins(registry) } @@ -48,24 +51,25 @@ class MannequinCommands( string("id") { selector("target") { executes { - val id = argument("id") - val entity = argument>("target").firstOrNull { it is Mannequin } as? Mannequin - if (entity == null) { - error("Selector must target at least one mannequin entity.") - return@executes - } - runCatching { - registry.register(id, entity, overwrite = false) - }.onSuccess { - success("Registered mannequin '$id' from entity ${entity.uniqueId}.") - }.onFailure { - error(it.message ?: "Failed to register mannequin.") - } + val id = argument("id") + val entity = argument>("target").firstOrNull { it is Mannequin } as? Mannequin + if (entity == null) { + error("Selector must target at least one mannequin entity.") + return@executes } + runCatching { + registry.register(id, entity, overwrite = false) + }.onSuccess { + success("Registered mannequin '$id' from entity ${entity.uniqueId}.") + }.onFailure { + error(it.message ?: "Failed to register mannequin.") + } + } literal("--overwrite") { executes { val id = argument("id") - val entity = argument>("target").firstOrNull { it is Mannequin } as? Mannequin + val entity = + argument>("target").firstOrNull { it is Mannequin } as? Mannequin if (entity == null) { error("Selector must target at least one mannequin entity.") return@executes @@ -96,6 +100,7 @@ class MannequinCommands( literal("move") { string("id") { + suggests { prefix -> registry.all().map { it.id }.filter { it.startsWith(prefix) } } executes { val player = requirePlayer() ?: return@executes val id = argument("id") @@ -112,6 +117,7 @@ class MannequinCommands( literal("apply") { string("id") { + suggests { prefix -> registry.all().map { it.id }.filter { it.startsWith(prefix) } } executes { val id = argument("id") runCatching { @@ -128,6 +134,7 @@ class MannequinCommands( literal("remove") { string("id") { + suggests { prefix -> registry.all().map { it.id }.filter { it.startsWith(prefix) } } executes { val id = argument("id") runCatching { registry.remove(id, deleteEntity = false) } @@ -147,15 +154,19 @@ class MannequinCommands( literal("set") { string("id") { + suggests { prefix -> registry.all().map { it.id }.filter { it.startsWith(prefix) } } literal("pose") { string("pose") { - suggests { prefix -> Pose.entries.map { it.name.lowercase() }.filter { it.startsWith(prefix.lowercase()) } } + suggests { prefix -> + MannequinSettings.POSES.map { it.name.lowercase() } + .filter { it.startsWith(prefix.lowercase()) } + } executes { val id = argument("id") val poseToken = argument("pose") val pose = runCatching { Pose.valueOf(poseToken.uppercase()) }.getOrNull() - if (pose == null) { - error("Unknown pose '$poseToken'.") + if (pose == null || pose !in MannequinSettings.POSES) { + error("Unknown or invalid pose '$poseToken'.") return@executes } registry.updateSettings(id) { it.copy(pose = pose) } @@ -166,7 +177,10 @@ class MannequinCommands( literal("mainhand") { string("hand") { - suggests { prefix -> MainHand.entries.map { it.name.lowercase() }.filter { it.startsWith(prefix.lowercase()) } } + suggests { prefix -> + MainHand.entries.map { it.name.lowercase() } + .filter { it.startsWith(prefix.lowercase()) } + } executes { val id = argument("id") val handToken = argument("hand") @@ -183,7 +197,14 @@ class MannequinCommands( literal("immovable") { string("state") { - suggests { prefix -> listOf("true", "false", "on", "off").filter { it.startsWith(prefix.lowercase()) } } + suggests { prefix -> + listOf( + "true", + "false", + "on", + "off" + ).filter { it.startsWith(prefix.lowercase()) } + } executes { val id = argument("id") val input = argument("state") @@ -200,21 +221,24 @@ class MannequinCommands( literal("description") { literal("text") { - executes { - val id = argument("id") - val payload = remainingInput(DESCRIPTION_TEXT_OFFSET) - if (payload.isNullOrBlank()) { - error("Provide MiniMessage text after the command, e.g. /mannequin set $id description text ") - return@executes + greedyString("content") { + executes { + val id = argument("id") + val payload = argument("content") + val component = runCatching { TextSerializers.miniMessage(payload) } + .onFailure { error("MiniMessage parse failed: ${it.message}") } + .getOrNull() + if (component == null) { + return@executes + } + registry.updateSettings(id) { + it.copy( + description = component, + hideDescription = false + ) + } + success("Updated description for '$id'.") } - val component = runCatching { TextSerializers.miniMessage(payload) } - .onFailure { error("MiniMessage parse failed: ${it.message}") } - .getOrNull() - if (component == null) { - return@executes - } - registry.updateSettings(id) { it.copy(description = component, hideDescription = false) } - success("Updated description for '$id'.") } } literal("clear") { @@ -226,7 +250,14 @@ class MannequinCommands( } literal("hide") { string("state") { - suggests { prefix -> listOf("true", "false", "on", "off").filter { it.startsWith(prefix.lowercase()) } } + suggests { prefix -> + listOf( + "true", + "false", + "on", + "off" + ).filter { it.startsWith(prefix.lowercase()) } + } executes { val id = argument("id") val input = argument("state") @@ -304,6 +335,62 @@ class MannequinCommands( } } } + + literal("respawn-delay") { + integer("seconds") { + executes { + val id = argument("id") + val seconds = argument("seconds") + registry.updateSettings(id) { it.copy(respawnDelay = seconds) } + if (seconds < 0) { + success("Disabled auto-respawn for '$id'.") + } else { + success("Set auto-respawn delay for '$id' to $seconds seconds.") + } + } + } + } + + literal("command") { + literal("console") { + literal("set") { + greedyString("command") { + executes { + val id = argument("id") + val cmd = argument("command") + registry.updateSettings(id) { it.copy(serverCommand = cmd) } + success("Set console command for '$id'.") + } + } + } + literal("clear") { + executes { + val id = argument("id") + registry.updateSettings(id) { it.copy(serverCommand = null) } + success("Cleared console command for '$id'.") + } + } + } + literal("player") { + literal("set") { + greedyString("command") { + executes { + val id = argument("id") + val cmd = argument("command") + registry.updateSettings(id) { it.copy(playerCommand = cmd) } + success("Set player command for '$id'.") + } + } + } + literal("clear") { + executes { + val id = argument("id") + registry.updateSettings(id) { it.copy(playerCommand = null) } + success("Cleared player command for '$id'.") + } + } + } + } } } } @@ -311,6 +398,7 @@ class MannequinCommands( } private const val DESCRIPTION_TEXT_OFFSET = 4 +private const val COMMAND_TEXT_OFFSET = 5 private fun KommandContext.listMannequins(registry: MannequinRegistry) { val entries = registry.all() @@ -320,11 +408,28 @@ private fun KommandContext.listMannequins(registry: MannequinRegistry) { } sender.sendMessage(Component.text("Registered mannequins (${entries.size}):", NamedTextColor.GRAY)) entries.forEach { record -> - val status = if (registry.locate(record.id) != null) "active" else "offline" + val isAlive = registry.locate(record.id) != null + val statusText = if (isAlive) "active" else "offline" + val statusColor = if (isAlive) NamedTextColor.GREEN else NamedTextColor.RED + + val idComponent = Component.text("- ${record.id}: ", NamedTextColor.WHITE) + .clickEvent(ClickEvent.suggestCommand("/mannequin set ${record.id} ")) + .hoverEvent(HoverEvent.showText(Component.text("Click to configure settings", NamedTextColor.GRAY))) + + val statusBase = Component.text(statusText, statusColor) + val statusComponent = if (!isAlive) { + statusBase + .clickEvent(ClickEvent.runCommand("/mannequin apply ${record.id}")) + .hoverEvent(HoverEvent.showText(Component.text("Click to respawn", NamedTextColor.YELLOW))) + } else { + statusBase + .hoverEvent(HoverEvent.showText(Component.text("Mannequin is active", NamedTextColor.GREEN))) + } + val location = formatLocation(record.location) sender.sendMessage( - Component.text("- ${record.id}: ", NamedTextColor.WHITE) - .append(Component.text(status, if (status == "active") NamedTextColor.GREEN else NamedTextColor.DARK_GRAY)) + idComponent + .append(statusComponent) .append(Component.text(" @ $location", NamedTextColor.GRAY)) ) } @@ -345,11 +450,6 @@ private fun KommandContext.error(message: String) { sender.sendMessage(Component.text(message, NamedTextColor.RED)) } -private fun KommandContext.remainingInput(offset: Int): String? { - if (args.size <= offset) return null - return args.drop(offset).joinToString(" ").trim().ifEmpty { null } -} - private fun layerSuggestions(prefix: String): List = MannequinHiddenLayer.entries.map { it.key }.filter { it.startsWith(prefix.lowercase()) } diff --git a/src/main/kotlin/net/hareworks/npc-mannequin/mannequin/MannequinSettings.kt b/src/main/kotlin/net/hareworks/npc-mannequin/mannequin/MannequinSettings.kt index ad4d4b5..7b47d4a 100644 --- a/src/main/kotlin/net/hareworks/npc-mannequin/mannequin/MannequinSettings.kt +++ b/src/main/kotlin/net/hareworks/npc-mannequin/mannequin/MannequinSettings.kt @@ -21,9 +21,22 @@ data class MannequinSettings( val description: Component? = null, val hideDescription: Boolean = false, val hiddenLayers: Set = emptySet(), - val profile: StoredProfile? = null + val profile: StoredProfile? = null, + val respawnDelay: Int = 0, + val serverCommand: String? = null, + val playerCommand: String? = null ) { companion object { + val POSES = setOf( + Pose.STANDING, // Typical upright standing pose + Pose.SLEEPING, // Laying horizontal, as if in a bed + Pose.SNEAKING, // Crouched/sneaking posture + Pose.SWIMMING, // Horizontal swimming animation + Pose.SPIN_ATTACK, // Riptide trident spin animation + Pose.LONG_JUMPING, // Goat long jump animation (arms raised) + Pose.FALL_FLYING // Elytra gliding pose + ) + fun from(entity: Mannequin): MannequinSettings { val skinParts = entity.skinParts return MannequinSettings( diff --git a/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinListener.kt b/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinListener.kt new file mode 100644 index 0000000..41a3465 --- /dev/null +++ b/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinListener.kt @@ -0,0 +1,59 @@ +package net.hareworks.npc_mannequin.service + +import com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent +import org.bukkit.entity.Mannequin +import org.bukkit.event.EventHandler +import org.bukkit.event.Listener +import org.bukkit.plugin.java.JavaPlugin + +class MannequinListener( + private val plugin: JavaPlugin, + private val registry: MannequinRegistry +) : Listener { + + @EventHandler + fun onEntityRemove(event: EntityRemoveFromWorldEvent) { + val entity = event.entity + if (entity !is Mannequin) return + + // If the entity is being removed because the chunk is unloading, we don't want to respawn it. + // Usually, isDead is false in that case. + // We only care if the entity is actually destroyed/killed. + if (!entity.isDead) return + + val record = registry.all().firstOrNull { it.entityId == entity.uniqueId } ?: return + + val delaySeconds = record.settings.respawnDelay + if (delaySeconds < 0) return + + val delayTicks = (delaySeconds * 20).toLong() + + plugin.server.scheduler.runTaskLater(plugin, Runnable { + // Verify record still exists (wasn't removed from registry while waiting) + if (registry.find(record.id) == null) return@Runnable + registry.apply(record.id, spawnIfMissing = true) + }, delayTicks) + } + + @EventHandler + fun onInteract(event: org.bukkit.event.player.PlayerInteractEntityEvent) { + val entity = event.rightClicked + if (entity !is Mannequin) return + + val record = registry.all().firstOrNull { it.entityId == entity.uniqueId } ?: return + val settings = record.settings + val player = event.player + + settings.serverCommand?.let { cmd -> + val cleanCmd = if (cmd.startsWith("/")) cmd.substring(1) else cmd + val finalCmd = cleanCmd.replace("", player.name) + plugin.server.dispatchCommand(plugin.server.consoleSender, finalCmd) + } + + settings.playerCommand?.let { cmd -> + val cleanCmd = if (cmd.startsWith("/")) cmd.substring(1) else cmd + val finalCmd = cleanCmd.replace("", player.name) + player.performCommand(finalCmd) + } + } +} diff --git a/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinRegistry.kt b/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinRegistry.kt index 96b17f7..135aabb 100644 --- a/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinRegistry.kt +++ b/src/main/kotlin/net/hareworks/npc-mannequin/service/MannequinRegistry.kt @@ -106,11 +106,11 @@ class MannequinRegistry( fun remove(id: String, deleteEntity: Boolean) { val record = require(id) + records.remove(id) + persist() if (deleteEntity) { controller.locate(record)?.remove() } - records.remove(id) - persist() } fun locate(id: String): Mannequin? = controller.locate(require(id)) diff --git a/src/main/kotlin/net/hareworks/npc-mannequin/storage/MannequinStorage.kt b/src/main/kotlin/net/hareworks/npc-mannequin/storage/MannequinStorage.kt index e597f44..3641568 100644 --- a/src/main/kotlin/net/hareworks/npc-mannequin/storage/MannequinStorage.kt +++ b/src/main/kotlin/net/hareworks/npc-mannequin/storage/MannequinStorage.kt @@ -84,7 +84,9 @@ class MannequinStorage(private val plugin: JavaPlugin) { description = description, hideDescription = hideDescription, hiddenLayers = hiddenLayers, - profile = profile + profile = profile, + serverCommand = section.getString("serverCommand"), + playerCommand = section.getString("playerCommand") ) return MannequinRecord(id, settings, location, entityId) } @@ -96,6 +98,8 @@ class MannequinStorage(private val plugin: JavaPlugin) { section.set("hideDescription", record.settings.hideDescription) section.set("description", TextSerializers.miniMessage(record.settings.description)) section.set("hiddenLayers", record.settings.hiddenLayers.map { it.key }) + section.set("serverCommand", record.settings.serverCommand) + section.set("playerCommand", record.settings.playerCommand) record.settings.profile?.let { profile -> val profileSection = section.createSection("profile") profileSection.set("name", profile.name)