diff --git a/src/main/kotlin/net/hareworks/hcu/items/api/component/CustomComponent.kt b/src/main/kotlin/net/hareworks/hcu/items/api/component/CustomComponent.kt index 2849f07..5ee02cc 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/api/component/CustomComponent.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/api/component/CustomComponent.kt @@ -1,5 +1,6 @@ package net.hareworks.hcu.items.api.component +import io.papermc.paper.datacomponent.DataComponentType import net.hareworks.hcu.items.api.Tier import org.bukkit.NamespacedKey import org.bukkit.inventory.ItemStack @@ -10,6 +11,14 @@ interface CustomComponent { val maxTier: Int val minTier: Int + /** 依存する他のCustomComponent(NamespacedKeyで指定) */ + val componentDependencies: List + get() = emptyList() + + /** 依存するバニラDataComponent(値なしフラグタイプ用) */ + val dataComponentDependencies: List + get() = emptyList() + fun apply(item: ItemStack, tier: Tier? = null) fun has(item: ItemStack): Boolean fun remove(item: ItemStack) diff --git a/src/main/kotlin/net/hareworks/hcu/items/command/CommandRegistrar.kt b/src/main/kotlin/net/hareworks/hcu/items/command/CommandRegistrar.kt index ca725dc..8e27ebd 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/command/CommandRegistrar.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/command/CommandRegistrar.kt @@ -161,7 +161,7 @@ object CommandRegistrar { return@executes } - component.apply(item) + ComponentRegistry.applyWithDependencies(component, item) sender.sendMessage("Applied component '${component.displayName}' to held item.") } integer("tier", min = 1, max = 100) { @@ -190,7 +190,7 @@ object CommandRegistrar { } val tier = Tier.fromLevel(tierLevel) - component.apply(item, tier) + ComponentRegistry.applyWithDependencies(component, item, tier) sender.sendMessage("Applied component '${component.displayName}' (Tier ${tier.level}) to held item.") } } diff --git a/src/main/kotlin/net/hareworks/hcu/items/content/components/DoubleJumpComponent.kt b/src/main/kotlin/net/hareworks/hcu/items/content/components/DoubleJumpComponent.kt index 75e2589..267731d 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/content/components/DoubleJumpComponent.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/content/components/DoubleJumpComponent.kt @@ -1,5 +1,7 @@ package net.hareworks.hcu.items.content.components +import io.papermc.paper.datacomponent.DataComponentType +import io.papermc.paper.datacomponent.DataComponentTypes import net.hareworks.hcu.items.App import net.hareworks.hcu.items.api.Tier import net.hareworks.hcu.items.api.component.AbstractComponent @@ -20,6 +22,9 @@ class DoubleJumpComponent(private val plugin: App) : AbstractComponent(plugin, " override val displayName: String = "Double Jump" override val maxTier: Int = 3 + override val dataComponentDependencies: List + get() = listOf(DataComponentTypes.GLIDER) + override fun apply(item: ItemStack, tier: Tier?) { if (isBoots(item.type)) { super.apply(item, tier) 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 be370b0..fa3638e 100644 --- a/src/main/kotlin/net/hareworks/hcu/items/registry/ComponentRegistry.kt +++ b/src/main/kotlin/net/hareworks/hcu/items/registry/ComponentRegistry.kt @@ -1,7 +1,9 @@ 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 org.bukkit.inventory.ItemStack object ComponentRegistry { private val components = mutableMapOf() @@ -17,4 +19,27 @@ object ComponentRegistry { fun getEquippableComponents(): List { return components.values.filterIsInstance() } + + /** + * コンポーネントを依存関係を解決しながら適用する + */ + fun applyWithDependencies(component: CustomComponent, item: ItemStack, tier: Tier? = null) { + // 1. バニラDataComponent依存を適用 + for (dataType in component.dataComponentDependencies) { + if (!item.hasData(dataType)) { + item.setData(dataType) + } + } + + // 2. CustomComponent依存を再帰的に適用 + for (depKey in component.componentDependencies) { + val dep = get(depKey) + if (dep != null && !dep.has(item)) { + applyWithDependencies(dep, item, null) + } + } + + // 3. 本体を適用 + component.apply(item, tier) + } }