From 6752d075f4c8b417cf4a573977041e728c9d1ea4 Mon Sep 17 00:00:00 2001 From: Kariya Date: Tue, 16 Dec 2025 13:02:20 +0000 Subject: [PATCH] feat: Introduce `ToolComponent` interface for held item events and update `EventListener` to dispatch them. --- .../hcu/items/api/component/ToolComponent.kt | 9 +++ .../content/components/VeinMinerComponent.kt | 9 ++- .../hcu/items/listeners/EventListener.kt | 67 ++++++++++++++++++- .../hcu/items/registry/ComponentRegistry.kt | 5 ++ 4 files changed, 83 insertions(+), 7 deletions(-) create mode 100644 src/main/kotlin/net/hareworks/hcu/items/api/component/ToolComponent.kt diff --git a/src/main/kotlin/net/hareworks/hcu/items/api/component/ToolComponent.kt b/src/main/kotlin/net/hareworks/hcu/items/api/component/ToolComponent.kt new file mode 100644 index 0000000..b4b2cab --- /dev/null +++ b/src/main/kotlin/net/hareworks/hcu/items/api/component/ToolComponent.kt @@ -0,0 +1,9 @@ +package net.hareworks.hcu.items.api.component + +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack + +interface ToolComponent : CustomComponent { + fun onHoldTick(player: Player, item: ItemStack) + fun onStopHolding(player: Player, item: ItemStack) {} +} diff --git a/src/main/kotlin/net/hareworks/hcu/items/content/components/VeinMinerComponent.kt b/src/main/kotlin/net/hareworks/hcu/items/content/components/VeinMinerComponent.kt index d692ae1..5b98f7c 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/content/components/VeinMinerComponent.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/content/components/VeinMinerComponent.kt @@ -1,6 +1,6 @@ package net.hareworks.hcu.items.content.components -import net.hareworks.hcu.items.api.component.EquippableComponent +import net.hareworks.hcu.items.api.component.ToolComponent import net.hareworks.hcu.items.api.Tier import net.hareworks.hcu.items.config.Config import org.bukkit.Material @@ -25,7 +25,7 @@ import java.util.Queue import java.util.Random import java.util.UUID -class VeinMinerComponent(private val plugin: JavaPlugin) : EquippableComponent { +class VeinMinerComponent(private val plugin: JavaPlugin) : ToolComponent { override val key: NamespacedKey = NamespacedKey(plugin, "vein_miner") override val displayName: String = "Vein Miner" @@ -57,7 +57,7 @@ class VeinMinerComponent(private val plugin: JavaPlugin) : EquippableComponent { item.itemMeta = meta } - override fun onTick(player: Player, item: ItemStack) { + override fun onHoldTick(player: Player, item: ItemStack) { // ハイライト条件チェック (Sneakしながら) if (!player.isSneaking) { clearHighlight(player) @@ -117,8 +117,7 @@ class VeinMinerComponent(private val plugin: JavaPlugin) : EquippableComponent { activeHighlights[player.uniqueId] = HighlightSession(targetBlock, entities, System.currentTimeMillis()) } - // アイテム持ち替えやスニーク解除時にも消えるように - override fun onUnequip(player: Player, item: ItemStack) { + override fun onStopHolding(player: Player, item: ItemStack) { clearHighlight(player) } diff --git a/src/main/kotlin/net/hareworks/hcu/items/listeners/EventListener.kt b/src/main/kotlin/net/hareworks/hcu/items/listeners/EventListener.kt index 3edccf8..347ffa7 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/listeners/EventListener.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/listeners/EventListener.kt @@ -11,10 +11,15 @@ import org.bukkit.inventory.EquipmentSlot import org.bukkit.inventory.ItemStack import net.hareworks.hcu.items.api.component.EquippableComponent +import net.hareworks.hcu.items.api.component.ToolComponent import org.bukkit.plugin.Plugin class EventListener(private val plugin: Plugin) : Listener { + // Track previously held items for ToolComponents to detect when they stop being held + // Key: Player UUID, Value: Pair(MainHandItem, OffHandItem) + private val lastHeldItems = java.util.concurrent.ConcurrentHashMap>() + init { plugin.server.scheduler.runTaskTimer(plugin, Runnable { tickComponents() @@ -132,14 +137,72 @@ class EventListener(private val plugin: Plugin) : Listener { private fun tickComponents() { for (player in plugin.server.onlinePlayers) { - val items = getEquipmentItems(player) - for (item in items) { + val uuid = player.uniqueId + val currentMainHand = player.inventory.itemInMainHand + val currentOffHand = player.inventory.itemInOffHand + + // Get previous items + val lastItems = lastHeldItems[uuid] + val lastMainHand = lastItems?.first + val lastOffHand = lastItems?.second + + // Check Main Hand Change + if (lastMainHand != null && lastMainHand != currentMainHand) { + dispatchToComponents(lastMainHand) { component -> + if (component is ToolComponent) { + component.onStopHolding(player, lastMainHand) + } + } + } + + // Check Off Hand Change + if (lastOffHand != null && lastOffHand != currentOffHand) { + dispatchToComponents(lastOffHand) { component -> + if (component is ToolComponent) { + component.onStopHolding(player, lastOffHand) + } + } + } + + // Update Cache + lastHeldItems[uuid] = Pair(currentMainHand, currentOffHand) + + // --- Regular Tick Processing --- + + // Armor Tick + val armorItems = player.inventory.armorContents.filterNotNull() + for (item in armorItems) { + if (item.type.isAir) continue dispatchToComponents(item) { component -> if (component is EquippableComponent) { component.onTick(player, item) } } } + + // ToolComponents in Main Hand + if (!currentMainHand.type.isAir) { + dispatchToComponents(currentMainHand) { component -> + if (component is ToolComponent) { + component.onHoldTick(player, currentMainHand) + } + if (component is EquippableComponent) { + component.onTick(player, currentMainHand) + } + } + } + + // ToolComponents in Off Hand + if (!currentOffHand.type.isAir) { + dispatchToComponents(currentOffHand) { component -> + if (component is ToolComponent) { + component.onHoldTick(player, currentOffHand) + } + if (component is EquippableComponent) { + component.onTick(player, currentOffHand) + } + } + } } } diff --git a/src/main/kotlin/net/hareworks/hcu/items/registry/ComponentRegistry.kt b/src/main/kotlin/net/hareworks/hcu/items/registry/ComponentRegistry.kt index fa3638e..1552bc2 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/registry/ComponentRegistry.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/registry/ComponentRegistry.kt @@ -3,6 +3,7 @@ package net.hareworks.hcu.items.registry import net.hareworks.hcu.items.api.Tier import net.hareworks.hcu.items.api.component.CustomComponent import net.hareworks.hcu.items.api.component.EquippableComponent +import net.hareworks.hcu.items.api.component.ToolComponent import org.bukkit.inventory.ItemStack object ComponentRegistry { @@ -20,6 +21,10 @@ object ComponentRegistry { return components.values.filterIsInstance() } + fun getToolComponents(): List { + return components.values.filterIsInstance() + } + /** * コンポーネントを依存関係を解決しながら適用する */