From 872af86f00f9636a2bc005dd1f412738ee9ba355 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Mon, 16 Nov 2026 11:58:50 +0100 Subject: [PATCH] feat: Add ItemsAdder items and blocks providers Items and blocks are available with the 'itemsadder' namespace. The default 'itemsadder' namespace is dropped from IDs, other namespaces become the first path segment. The 'ia' items registry provided by ItemsAdder itself is no longer bridged to Items API, so the same items are not claimed twice. Closes #49 Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 4 + MODRINTH.md | 4 +- gradle/libs.versions.toml | 3 + mimic-bukkit-api/README.md | 2 +- mimic-bukkit/README.md | 9 +- mimic-bukkit/build.gradle.kts | 2 + mimic-bukkit/src/main/kotlin/MimicImpl.kt | 11 +- mimic-bukkit/src/main/kotlin/MimicPlugin.kt | 7 + .../itemsadder/ItemsAdderBlockProvider.kt | 71 ++++++++ .../impl/itemsadder/ItemsAdderItemProvider.kt | 41 +++++ .../impl/itemsadder/ItemsAdderWrapper.kt | 50 ++++++ mimic-bukkit/src/test/kotlin/MimicImplTest.kt | 8 + .../itemsadder/ItemsAdderBlockProviderTest.kt | 166 ++++++++++++++++++ .../itemsadder/ItemsAdderItemProviderTest.kt | 87 +++++++++ 14 files changed, 459 insertions(+), 6 deletions(-) create mode 100644 mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderBlockProvider.kt create mode 100644 mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderItemProvider.kt create mode 100644 mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderWrapper.kt create mode 100644 mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderBlockProviderTest.kt create mode 100644 mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderItemProviderTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a4e5adc..9b81c923 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -93,6 +93,10 @@ See [Resources](https://github.com/EndlessCodeGroup/Mimic/blob/develop/mimic-buk - Added [Jobs Reborn](https://www.spigotmc.org/resources/4216/) levels provider ([#57](https://github.com/EndlessCodeGroup/Mimic/issues/57)). - Added [Fabled](https://www.spigotmc.org/resources/91913/) levels and classes providers ([#48](https://github.com/EndlessCodeGroup/Mimic/issues/48)): class groups are slots and level tracks. - Added [ValhallaMMO](https://modrinth.com/plugin/valhallammo) levels provider ([#99](https://github.com/EndlessCodeGroup/Mimic/issues/99)). +- Added [ItemsAdder](https://www.spigotmc.org/resources/73355/) items and blocks providers ([#49](https://github.com/EndlessCodeGroup/Mimic/issues/49)). + IDs are `itemsadder:ruby_sword` for the default `itemsadder` namespace and `itemsadder:my_items/ruby_sword` for others. + **Changed:** The `ia` items registry provided by ItemsAdder itself is not available via Items API, Mimic warns about it. + Use `itemsadder:ruby_sword` instead of `ia:itemsadder:ruby_sword`. - MMOCore and Heroes support Levels and Classes API natively: class and profession levels, class slots and subclasses. This fixes class IDs in MMOCore ([#78](https://github.com/EndlessCodeGroup/Mimic/issues/78)) and reporting class history as classes in Heroes ([#79](https://github.com/EndlessCodeGroup/Mimic/issues/79)). - Fixed MMOCore experience to reach a level in the deprecated `LevelSystem` API ([#76](https://github.com/EndlessCodeGroup/Mimic/issues/76)). diff --git a/MODRINTH.md b/MODRINTH.md index 8c91f29b..03a56523 100644 --- a/MODRINTH.md +++ b/MODRINTH.md @@ -20,8 +20,8 @@ That's it. Mimic detects installed RPG plugins automatically. | **Levels** | Vanilla experience, AuraSkills, mcMMO, ValhallaMMO, Jobs Reborn, MMOCore, Heroes, Fabled | | **Classes** | MMOCore, Heroes, Fabled | | **Resources** | Vanilla health, food and air, MMOCore, Heroes, AuraSkills, Fabled | -| **Items** | Vanilla items, CraftEngine, MMOItems, CustomItems, ItemsAdder¹, RPGInventory¹ | -| **Blocks** | Vanilla blocks, CraftEngine | +| **Items** | Vanilla items, CraftEngine, MMOItems, CustomItems, ItemsAdder, RPGInventory¹ | +| **Blocks** | Vanilla blocks, CraftEngine, ItemsAdder | | **Inventory** | Vanilla inventory, RPGInventory¹ | ¹ Support is provided by the plugin itself. diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f864590e..d448b560 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -21,6 +21,7 @@ fabled = "1.1.0" codex = "1.2.0" valhallammo = "premium_1.14.1" valhallammo-free = "1.10.4" +itemsadder = "4.0.17" junit = "6.1.3" kotest = "6.2.6" @@ -56,6 +57,7 @@ rpgplugins-cmilib = { module = "com.github.Zrips:CMILib", version.ref = "cmilib" rpgplugins-fabled = { module = "studio.magemonkey:fabled", version.ref = "fabled" } rpgplugins-codex = { module = "studio.magemonkey:codex", version.ref = "codex" } rpgplugins-valhallammo = { module = "me.athlaeos:valhallammo-core", version.ref = "valhallammo" } +rpgplugins-itemsadder = { module = "beer.devs:itemsadder-api", version.ref = "itemsadder" } # Test dependencies junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" } @@ -92,4 +94,5 @@ rpgplugins = [ "rpgplugins-fabled", "rpgplugins-codex", "rpgplugins-valhallammo", + "rpgplugins-itemsadder", ] diff --git a/mimic-bukkit-api/README.md b/mimic-bukkit-api/README.md index 5416a17c..b1f6d9fd 100644 --- a/mimic-bukkit-api/README.md +++ b/mimic-bukkit-api/README.md @@ -100,7 +100,7 @@ The path may contain lowercase letters, digits, `.`, `_`, `-` and `/`. - **Resources:** the namespace is required, see [Resource IDs](#resource-ids). > The deprecated `ItemsRegistry` API still searches an item ID without namespace, or with the `mimic` namespace, over all plugins. -> It also accepts item IDs with uppercase letters (`minecraft:DIAMOND_SWORD`), with more than one `:` (`ia:itemsadder:ruby_sword`) +> It also accepts item IDs with uppercase letters (`minecraft:DIAMOND_SWORD`), with more than one `:` (`craftengine:default:palm_log`) > and MMOItems IDs without item type (`mmoitems:excalibur`), but logs a warning.
diff --git a/mimic-bukkit/README.md b/mimic-bukkit/README.md index 3e281bc7..402109af 100644 --- a/mimic-bukkit/README.md +++ b/mimic-bukkit/README.md @@ -117,9 +117,9 @@ An item given to a player is built by MMOItems for this player. **ID Format:** `rpginventory:[id]`\ **Payload:** *Not supported* -#### [ItemsAdder] _(deprecated API)_ +#### [ItemsAdder] -**ID Format:** `ia:[id]`\ +**ID Format:** `itemsadder:[id]` for the default `itemsadder` namespace, `itemsadder:[namespace]/[id]` for others\ **Payload:** *Not supported* ### [Block Providers][BlockProvider] @@ -138,6 +138,11 @@ A block belongs to the provider with the highest priority that recognizes it, va **ID Format:** `craftengine:[namespace]/[id]`\ **State:** Block state properties, for example `[facing=north]` +#### [ItemsAdder] + +**ID Format:** `itemsadder:[id]` for the default `itemsadder` namespace, `itemsadder:[namespace]/[id]` for others\ +**State:** *Not supported* + [minecraft-exp]: https://minecraft.fandom.com/wiki/Experience [minecraft-inv]: https://minecraft.fandom.com/wiki/Inventory [item-components]: https://minecraft.wiki/w/Data_component_format diff --git a/mimic-bukkit/build.gradle.kts b/mimic-bukkit/build.gradle.kts index 840c3097..02691d99 100644 --- a/mimic-bukkit/build.gradle.kts +++ b/mimic-bukkit/build.gradle.kts @@ -33,6 +33,7 @@ bukkit { "Jobs", "Fabled", "ValhallaMMO", + "ItemsAdder", ) } @@ -109,6 +110,7 @@ dependencies { testImplementation(libs.rpgplugins.mythiclib) { isTransitive = false } testImplementation(libs.rpgplugins.fabled) { isTransitive = false } testImplementation(libs.rpgplugins.codex) { isTransitive = false } + testImplementation(libs.rpgplugins.itemsadder) } kotlin { diff --git a/mimic-bukkit/src/main/kotlin/MimicImpl.kt b/mimic-bukkit/src/main/kotlin/MimicImpl.kt index 4a003be2..2e894a13 100644 --- a/mimic-bukkit/src/main/kotlin/MimicImpl.kt +++ b/mimic-bukkit/src/main/kotlin/MimicImpl.kt @@ -15,6 +15,7 @@ import ru.endlesscode.mimic.blocks.Blocks import ru.endlesscode.mimic.blocks.WrappedBlockProvider import ru.endlesscode.mimic.classes.* import ru.endlesscode.mimic.config.MimicConfig +import ru.endlesscode.mimic.impl.itemsadder.ItemsAdderItemProvider import ru.endlesscode.mimic.impl.mimic.MimicBlocks import ru.endlesscode.mimic.impl.mimic.MimicClasses import ru.endlesscode.mimic.impl.mimic.MimicItems @@ -164,7 +165,15 @@ internal class MimicImpl( val itemsRegistry = itemsRegistries.tryRegister(apiLevel, plugin, priority) { WrappedItemsRegistry(registry, plugin) } - if (itemsRegistry != null) { + if (itemsRegistry == null) return null + + // Items registry of ItemsAdder isn't bridged, otherwise its items would have two IDs + if (itemsRegistry.id == ItemsAdderItemProvider.REGISTRY_ID) { + Log.w( + "ItemsRegistry 'ia' by $plugin is deprecated and replaced with the 'itemsadder' item provider " + + "built into Mimic. Use 'itemsadder:' instead of 'ia:itemsadder:'." + ) + } else { itemProviders.registerBridge(ItemsRegistryItemProvider(itemsRegistry), plugin, priority) { "ItemsRegistry '${itemsRegistry.id}' by $plugin uses deprecated ItemsRegistry API, " + "so its items are available via Items API as '${itemsRegistry.id}:'. " + diff --git a/mimic-bukkit/src/main/kotlin/MimicPlugin.kt b/mimic-bukkit/src/main/kotlin/MimicPlugin.kt index 7f50b192..17c456ad 100644 --- a/mimic-bukkit/src/main/kotlin/MimicPlugin.kt +++ b/mimic-bukkit/src/main/kotlin/MimicPlugin.kt @@ -31,6 +31,8 @@ import ru.endlesscode.mimic.impl.heroes.HeroesClassSystem import ru.endlesscode.mimic.impl.heroes.HeroesLevelProvider import ru.endlesscode.mimic.impl.heroes.HeroesLevelSystem import ru.endlesscode.mimic.impl.heroes.HeroesResourceProvider +import ru.endlesscode.mimic.impl.itemsadder.ItemsAdderBlockProvider +import ru.endlesscode.mimic.impl.itemsadder.ItemsAdderItemProvider import ru.endlesscode.mimic.impl.jobs.JobsLevelProvider import ru.endlesscode.mimic.impl.mcmmo.McMmoLevelProvider import ru.endlesscode.mimic.impl.mimic.PermissionsClassSystem @@ -148,6 +150,11 @@ public class MimicPlugin : JavaPlugin() { pluginHooks("ValhallaMMO", "me.athlaeos.valhallammo.ValhallaMMO") { hookLevels(::ValhallaMmoLevelProvider) } + + pluginHooks("ItemsAdder", "dev.lone.itemsadder.api.CustomBlock") { + hookItems(::ItemsAdderItemProvider) + hookBlocks(::ItemsAdderBlockProvider) + } } private fun pluginHooks(name: String, vararg requiredClasses: String, hooks: () -> Unit) { diff --git a/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderBlockProvider.kt b/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderBlockProvider.kt new file mode 100644 index 00000000..866ad793 --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderBlockProvider.kt @@ -0,0 +1,71 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.itemsadder + +import org.bukkit.block.Block +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.blocks.BlockProvider +import ru.endlesscode.mimic.blocks.DropsConsumer +import ru.endlesscode.mimic.internal.Log + +/** + * Block provider for ItemsAdder custom blocks. + * The default `itemsadder` namespace is dropped from block ID, other namespaces become the first path segment: + * `itemsadder:ruby_ore` -> `ruby_ore`, `my_blocks:ruby_ore` -> `my_blocks/ruby_ore`. + * A block has the same ID as the item placing it. + * + * ItemsAdder blocks have no states, so [setBlock] with a state sets nothing. + * Furniture is not a block. + */ +internal class ItemsAdderBlockProvider( + private val itemsAdder: ItemsAdderWrapper = ItemsAdderWrapper(), +) : BlockProvider { + + override val id: String = ID + override val isEnabled: Boolean get() = itemsAdder.isEnabled + + override val allBlocks: Collection + get() = itemsAdder.blockIds.map(String::toMimicId) + + override fun hasBlock(blockId: String): Boolean = itemsAdder.hasBlock(itemsAdderId(blockId)) + + override fun getBlockId(block: Block): String? = itemsAdder.getCustomBlock(block)?.namespacedID?.toMimicId() + + override fun getBlockIdFromItem(item: ItemStack): String? = + itemsAdder.getCustomBlock(item)?.namespacedID?.toMimicId() + + override fun createBlockItem(blockId: String, amount: Int): ItemStack? { + val customBlock = itemsAdder.getCustomBlock(itemsAdderId(blockId)) ?: return null + return customBlock.itemStack.also { it.amount = amount } + } + + override fun setBlock(block: Block, blockId: String, state: String?): Boolean { + val itemsAdderId = itemsAdderId(blockId) + if (state != null) { + if (itemsAdder.hasBlock(itemsAdderId)) { + Log.w("[$TAG] Invalid state for block $blockId: $state. ItemsAdder blocks have no states.") + } + return false + } + return itemsAdder.placeBlock(itemsAdderId, block.location) + } + + override fun breakBlock(block: Block, tool: ItemStack?, dropsConsumer: DropsConsumer): Boolean { + val customBlock = itemsAdder.getCustomBlock(block) ?: return false + + // The block itself is a part of the loot if ItemsAdder drops it on break + val drops = customBlock.getLoot(tool, /* includeSelfBlock = */ true) + if (!customBlock.remove()) return false + + for (item in dropsConsumer.consume(drops)) { + block.world.dropItemNaturally(block.location, item) + } + return true + } + + companion object { + const val ID: String = "itemsadder" + private const val TAG = "ItemsAdderBlockProvider" + } +} diff --git a/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderItemProvider.kt b/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderItemProvider.kt new file mode 100644 index 00000000..424b91ba --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderItemProvider.kt @@ -0,0 +1,41 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.itemsadder + +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.items.ItemProvider + +/** + * Item provider for ItemsAdder custom items. + * The default `itemsadder` namespace is dropped from item ID, other namespaces become the first path segment: + * `itemsadder:ruby_sword` -> `ruby_sword`, `my_items:ruby_sword` -> `my_items/ruby_sword`. + * Takes no payload. ItemsAdder can't create an item for a player, so the player is ignored. + */ +internal class ItemsAdderItemProvider( + private val itemsAdder: ItemsAdderWrapper = ItemsAdderWrapper(), +) : ItemProvider { + + override val id: String = ID + override val isEnabled: Boolean get() = itemsAdder.isEnabled + + override val allItems: Collection + get() = itemsAdder.itemIds.map(String::toMimicId) + + override fun hasItem(itemId: String): Boolean = itemsAdder.hasItem(itemsAdderId(itemId)) + + override fun getItemId(item: ItemStack): String? = itemsAdder.getCustomStack(item)?.namespacedID?.toMimicId() + + override fun createItem(itemId: String, amount: Int, player: Player?, payload: String?): ItemStack? { + val customStack = itemsAdder.getCustomStack(itemsAdderId(itemId)) ?: return null + return customStack.itemStack.also { it.amount = amount } + } + + companion object { + const val ID: String = "itemsadder" + + /** ID of the items registry provided by ItemsAdder itself. */ + const val REGISTRY_ID: String = "ia" + } +} diff --git a/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderWrapper.kt b/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderWrapper.kt new file mode 100644 index 00000000..d69c764a --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/itemsadder/ItemsAdderWrapper.kt @@ -0,0 +1,50 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.itemsadder + +import dev.lone.itemsadder.api.CustomBlock +import dev.lone.itemsadder.api.CustomStack +import org.bukkit.Bukkit +import org.bukkit.Location +import org.bukkit.block.Block +import org.bukkit.inventory.ItemStack + +/** + * Wraps ItemsAdder static API. + * ItemsAdder loads its data after the server start, its registries are empty until then. + */ +internal class ItemsAdderWrapper { + + val isEnabled: Boolean get() = Bukkit.getPluginManager().isPluginEnabled("ItemsAdder") + + val itemIds: Set get() = CustomStack.getNamespacedIdsInRegistry() + val blockIds: Set get() = CustomBlock.getNamespacedIdsInRegistry() + + fun hasItem(id: String): Boolean = CustomStack.isInRegistry(id) + fun hasBlock(id: String): Boolean = CustomBlock.isInRegistry(id) + + /** Returns a new instance of the item with the given [id]. */ + fun getCustomStack(id: String): CustomStack? = CustomStack.getInstance(id) + fun getCustomStack(item: ItemStack): CustomStack? = CustomStack.byItemStack(item) + + /** Returns a new instance of the item placing the block with the given [id]. */ + fun getCustomBlock(id: String): CustomBlock? = CustomBlock.getInstance(id) + fun getCustomBlock(item: ItemStack): CustomBlock? = CustomBlock.byItemStack(item) + fun getCustomBlock(block: Block): CustomBlock? = CustomBlock.byAlreadyPlaced(block) + + fun placeBlock(id: String, location: Location): Boolean = CustomBlock.place(id, location) != null +} + +private const val DEFAULT_NAMESPACE = "itemsadder" + +/** + * Converts ItemsAdder ID to Mimic ID: `itemsadder:ruby_ore` -> `ruby_ore`, `my_blocks:ruby_ore` -> `my_blocks/ruby_ore`. + * The `itemsadder` namespace is kept if the ID has `/`, so the first path segment is always a namespace. + */ +internal fun String.toMimicId(): String = + (if ('/' in this) this else removePrefix("$DEFAULT_NAMESPACE:")).replaceFirst(':', '/') + +/** Converts Mimic ID to ItemsAdder ID: `ruby_ore` -> `itemsadder:ruby_ore`, `my_blocks/ruby_ore` -> `my_blocks:ruby_ore`. */ +internal fun itemsAdderId(id: String): String = + if ('/' in id) id.replaceFirst('/', ':') else "$DEFAULT_NAMESPACE:$id" diff --git a/mimic-bukkit/src/test/kotlin/MimicImplTest.kt b/mimic-bukkit/src/test/kotlin/MimicImplTest.kt index c7cf0603..cfcbaead 100644 --- a/mimic-bukkit/src/test/kotlin/MimicImplTest.kt +++ b/mimic-bukkit/src/test/kotlin/MimicImplTest.kt @@ -252,6 +252,14 @@ internal class MimicImplTest : BukkitTestBase() { mimic.items.getAllItems().map { it.asString() } shouldContainExactly listOf("custom:provider_item") } + @Test + fun `when items registry of ItemsAdder is registered - should not bridge it`() { + mimic.registerItemsRegistry(TestItemsRegistry("ia"), MimicApiLevel.CURRENT, thirdParty, Normal) + + mimic.items.getAllItems().shouldBeEmpty() + mimic.getAllItemsRegistries().keys shouldContainExactly listOf("ia") + } + @Test fun `when plugin is disabled - should unregister its items registries and item providers`() { mimic.registerItemsRegistry(TestItemsRegistry("legacy"), MimicApiLevel.CURRENT, thirdParty, Normal) diff --git a/mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderBlockProviderTest.kt b/mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderBlockProviderTest.kt new file mode 100644 index 00000000..1113562c --- /dev/null +++ b/mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderBlockProviderTest.kt @@ -0,0 +1,166 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.itemsadder + +import dev.lone.itemsadder.api.CustomBlock +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.nulls.shouldBeNull +import io.kotest.matchers.shouldBe +import io.mockk.every +import io.mockk.mockk +import io.mockk.verify +import org.bukkit.Location +import org.bukkit.Material +import org.bukkit.World +import org.bukkit.block.Block +import org.bukkit.inventory.ItemStack +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import ru.endlesscode.mimic.BukkitTestBase +import ru.endlesscode.mimic.blocks.DropsConsumer +import kotlin.test.Test + +internal class ItemsAdderBlockProviderTest : BukkitTestBase() { + + private val world: World = mockk(relaxed = true) + private val location = Location(world, 1.0, 2.0, 3.0) + private val block: Block = mockk { + every { this@mockk.world } returns this@ItemsAdderBlockProviderTest.world + every { this@mockk.location } returns this@ItemsAdderBlockProviderTest.location + } + + private val itemsAdder: ItemsAdderWrapper = mockk { + every { blockIds } returns setOf("itemsadder:ruby_ore", "my_blocks:ruby_ore") + every { hasBlock(any()) } answers { firstArg() in blockIds } + every { getCustomBlock(any()) } returns null + every { getCustomBlock("my_blocks:ruby_ore") } answers { mockCustomBlock() } + every { getCustomBlock(any()) } returns null + every { getCustomBlock(any()) } returns null + every { placeBlock(any(), any()) } answers { firstArg() in blockIds } + } + + // SUT + private val provider = ItemsAdderBlockProvider(itemsAdder) + + @Test + fun `when get all blocks - should drop default namespace and keep others as the first segment`() { + provider.allBlocks shouldContainExactly listOf("ruby_ore", "my_blocks/ruby_ore") + } + + @ParameterizedTest + @CsvSource( + "ruby_ore, true", + "my_blocks/ruby_ore, true", + "my_blocks/unknown, false", + "unknown, false", + ) + fun `when check block exists - should look for ItemsAdder id`(blockId: String, expected: Boolean) { + provider.hasBlock(blockId) shouldBe expected + } + + @Test + fun `when get block id - should return id with namespace as the first segment`() { + every { itemsAdder.getCustomBlock(block) } returns mockCustomBlock() + + provider.getBlockId(block) shouldBe "my_blocks/ruby_ore" + } + + @Test + fun `when get id of not ItemsAdder block - should return null`() { + provider.getBlockId(block).shouldBeNull() + } + + @Test + fun `when get block id from item - should return id of the block`() { + val item = mockk() + every { itemsAdder.getCustomBlock(item) } returns mockCustomBlock() + + provider.getBlockIdFromItem(item) shouldBe "my_blocks/ruby_ore" + } + + @Test + fun `when get block id from item not placing a block - should return null`() { + provider.getBlockIdFromItem(ItemStack(Material.PAPER)).shouldBeNull() + } + + @Test + fun `when create block item - should return the item with the given amount`() { + val item = provider.createBlockItem("my_blocks/ruby_ore", amount = 3) + + item?.type shouldBe Material.PAPER + item?.amount shouldBe 3 + } + + @ParameterizedTest + @ValueSource(strings = ["unknown", "my_blocks/unknown"]) + fun `when create item of unknown block - should return null`(blockId: String) { + provider.createBlockItem(blockId, amount = 1).shouldBeNull() + } + + @Test + fun `when set block - should place the block by ItemsAdder id`() { + provider.setBlock(block, "my_blocks/ruby_ore", state = null) shouldBe true + + verify { itemsAdder.placeBlock("my_blocks:ruby_ore", location) } + } + + @ParameterizedTest + @ValueSource(strings = ["my_blocks/ruby_ore", "my_blocks/unknown"]) + fun `when set block with state - should not place the block`(blockId: String) { + provider.setBlock(block, blockId, state = "[facing=north]") shouldBe false + + verify(exactly = 0) { itemsAdder.placeBlock(any(), any()) } + } + + @ParameterizedTest + @ValueSource(strings = ["unknown", "my_blocks/unknown"]) + fun `when set unknown block - should return false`(blockId: String) { + provider.setBlock(block, blockId, state = null) shouldBe false + } + + @Test + fun `when break block - should remove the block and drop items returned by the consumer`() { + val tool = mockk() + val loot = listOf(mockk(), mockk()) + val customBlock = mockCustomBlock() + every { customBlock.getLoot(tool, true) } returns loot + every { itemsAdder.getCustomBlock(block) } returns customBlock + var consumed: Collection? = null + val dropsConsumer = DropsConsumer { drops -> + consumed = drops + drops.take(1) + } + + provider.breakBlock(block, tool, dropsConsumer) shouldBe true + + consumed shouldBe loot + verify { customBlock.remove() } + verify(exactly = 1) { world.dropItemNaturally(location, any()) } + verify { world.dropItemNaturally(location, loot.first()) } + } + + @Test + fun `when break not ItemsAdder block - should return false`() { + provider.breakBlock(block, tool = null, DropsConsumer.drop()) shouldBe false + } + + @Test + fun `when break block that can't be removed - should drop nothing`() { + val customBlock = mockCustomBlock() + every { customBlock.remove() } returns false + every { itemsAdder.getCustomBlock(block) } returns customBlock + + provider.breakBlock(block, tool = null, DropsConsumer.drop()) shouldBe false + + verify(exactly = 0) { world.dropItemNaturally(any(), any()) } + } + + private fun mockCustomBlock(): CustomBlock = mockk { + every { namespacedID } returns "my_blocks:ruby_ore" + every { itemStack } returns ItemStack(Material.PAPER) + every { getLoot(any(), any()) } returns listOf(ItemStack(Material.DIAMOND)) + every { remove() } returns true + } +} diff --git a/mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderItemProviderTest.kt b/mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderItemProviderTest.kt new file mode 100644 index 00000000..c7dbde22 --- /dev/null +++ b/mimic-bukkit/src/test/kotlin/impl/itemsadder/ItemsAdderItemProviderTest.kt @@ -0,0 +1,87 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.itemsadder + +import dev.lone.itemsadder.api.CustomStack +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.nulls.shouldBeNull +import io.kotest.matchers.shouldBe +import io.mockk.every +import io.mockk.mockk +import org.bukkit.Material +import org.bukkit.inventory.ItemStack +import org.junit.jupiter.params.ParameterizedTest +import org.junit.jupiter.params.provider.CsvSource +import org.junit.jupiter.params.provider.ValueSource +import ru.endlesscode.mimic.BukkitTestBase +import kotlin.test.Test + +internal class ItemsAdderItemProviderTest : BukkitTestBase() { + + private val itemsAdder: ItemsAdderWrapper = mockk { + every { itemIds } returns setOf("itemsadder:ruby_sword", "my_items:ruby_sword") + every { hasItem(any()) } answers { firstArg() in itemIds } + every { getCustomStack(any()) } returns null + every { getCustomStack("itemsadder:ruby_sword") } answers { mockCustomStack(Material.DIAMOND_SWORD) } + every { getCustomStack("my_items:ruby_sword") } answers { mockCustomStack(Material.IRON_SWORD) } + every { getCustomStack(any()) } returns null + } + + // SUT + private val provider = ItemsAdderItemProvider(itemsAdder) + + @Test + fun `when get all items - should drop default namespace and keep others as the first segment`() { + provider.allItems shouldContainExactly listOf("ruby_sword", "my_items/ruby_sword") + } + + @ParameterizedTest + @CsvSource( + "ruby_sword, true", + "my_items/ruby_sword, true", + "my_items/unknown, false", + "unknown, false", + ) + fun `when check item exists - should look for ItemsAdder id`(itemId: String, expected: Boolean) { + provider.hasItem(itemId) shouldBe expected + } + + @ParameterizedTest + @CsvSource( + "ruby_sword, DIAMOND_SWORD", + "my_items/ruby_sword, IRON_SWORD", + ) + fun `when create item - should return the item of the given namespace`(itemId: String, expected: Material) { + val item = provider.createItem(itemId, amount = 2, player, payload = "ignored") + + item?.type shouldBe expected + item?.amount shouldBe 2 + } + + @ParameterizedTest + @ValueSource(strings = ["unknown", "my_items/unknown", "unknown/ruby_sword"]) + fun `when create unknown item - should return null`(itemId: String) { + provider.createItem(itemId, amount = 1, player = null, payload = null).shouldBeNull() + } + + @ParameterizedTest + @CsvSource( + "itemsadder:ruby_sword, ruby_sword", + "my_items:ruby_sword, my_items/ruby_sword", + "itemsadder:swords/ruby, itemsadder/swords/ruby", + ) + fun `when get item id - should convert ItemsAdder id`(itemsAdderId: String, expected: String) { + val item = mockk() + every { itemsAdder.getCustomStack(item) } returns mockk { every { namespacedID } returns itemsAdderId } + + provider.getItemId(item) shouldBe expected + } + + @Test + fun `when get id of not ItemsAdder item - should return null`() { + provider.getItemId(ItemStack(Material.IRON_SWORD)).shouldBeNull() + } + + private fun mockCustomStack(type: Material): CustomStack = mockk { every { itemStack } returns ItemStack(type) } +}