From 7796472134e092558e2171b930fb46654e97fb2a Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Mon, 16 Nov 2026 12:33:15 +0100 Subject: [PATCH] feat: Add ExecutableItems item provider Closes #111 Co-Authored-By: Claude Fable 5.2 --- CHANGELOG.md | 5 ++ MODRINTH.md | 2 +- gradle/libs.versions.toml | 3 + mimic-bukkit/README.md | 7 ++ mimic-bukkit/build.gradle.kts | 3 + mimic-bukkit/src/main/kotlin/MimicPlugin.kt | 5 ++ .../ExecutableItemsItemProvider.kt | 61 +++++++++++++ .../executableitems/ExecutableItemsWrapper.kt | 29 +++++++ .../ExecutableItemsItemProviderTest.kt | 87 +++++++++++++++++++ 9 files changed, 201 insertions(+), 1 deletion(-) create mode 100644 mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsItemProvider.kt create mode 100644 mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsWrapper.kt create mode 100644 mimic-bukkit/src/test/kotlin/impl/executableitems/ExecutableItemsItemProviderTest.kt diff --git a/CHANGELOG.md b/CHANGELOG.md index 8a4e5adc..d5c4980d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,11 @@ See [Blocks](https://github.com/EndlessCodeGroup/Mimic/blob/develop/mimic-bukkit - Config option `disabled-block-registries` is renamed to `disabled-block-providers`, the old value is moved automatically. - `/mimic blocks` requires block IDs with a namespace, `payload` argument of `/mimic blocks set` is renamed to `state`. +### Integrations + +- Added ExecutableItems integration: item provider ([#111](https://github.com/EndlessCodeGroup/Mimic/issues/111)). + ExecutableItems IDs are lowercased: `executableitems:free_hammer`. An item created for a player gets this player as its owner. + ## [v0.10.0] (2026-11-15) ### Levels API diff --git a/MODRINTH.md b/MODRINTH.md index 8c91f29b..412a7c42 100644 --- a/MODRINTH.md +++ b/MODRINTH.md @@ -20,7 +20,7 @@ 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¹ | +| **Items** | Vanilla items, CraftEngine, MMOItems, CustomItems, ExecutableItems, ItemsAdder¹, RPGInventory¹ | | **Blocks** | Vanilla blocks, CraftEngine | | **Inventory** | Vanilla inventory, RPGInventory¹ | diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f864590e..80cbaefb 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" +score = "5.25.3.9" 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-score = { module = "com.github.Ssomar-Developement:SCore", version.ref = "score" } # Test dependencies junit-bom = { module = "org.junit:junit-bom", version.ref = "junit" } @@ -92,4 +94,5 @@ rpgplugins = [ "rpgplugins-fabled", "rpgplugins-codex", "rpgplugins-valhallammo", + "rpgplugins-score", ] diff --git a/mimic-bukkit/README.md b/mimic-bukkit/README.md index 3e281bc7..bb94e683 100644 --- a/mimic-bukkit/README.md +++ b/mimic-bukkit/README.md @@ -101,6 +101,12 @@ Providers marked with _(deprecated API)_ are provided by the plugin itself via t **ID Format:** `customitems:[id]`\ **Payload:** *Not supported* +#### [ExecutableItems] + +**ID Format:** `executableitems:[id]`, ExecutableItems ID is lowercased and characters not allowed in IDs are replaced with `_`\ +**Payload:** *Not supported*\ +An item given to a player is built by ExecutableItems with this player as its owner. + #### [MMOItems] **ID Format:** `mmoitems:[type]/[id]`, for example `mmoitems:sword/excalibur`\ @@ -150,6 +156,7 @@ A block belongs to the provider with the highest priority that recognizes it, va [rpginventory]: https://www.spigotmc.org/resources/12498/ [itemsadder]: https://www.spigotmc.org/resources/73355/ [craftengine]: https://modrinth.com/plugin/craftengine +[executableitems]: https://www.spigotmc.org/resources/77578/ [auraskills]: https://modrinth.com/plugin/auraskills [mcmmo]: https://www.spigotmc.org/resources/64348/ [jobs reborn]: https://www.spigotmc.org/resources/4216/ diff --git a/mimic-bukkit/build.gradle.kts b/mimic-bukkit/build.gradle.kts index 840c3097..6a8595e3 100644 --- a/mimic-bukkit/build.gradle.kts +++ b/mimic-bukkit/build.gradle.kts @@ -33,6 +33,7 @@ bukkit { "Jobs", "Fabled", "ValhallaMMO", + "ExecutableItems", ) } @@ -64,6 +65,7 @@ repositories { maven(url = "https://jitpack.io") { content { includeGroup("com.github.Zrips") + includeGroup("com.github.Ssomar-Developement") } } maven(url = "https://repo.travja.dev/releases/") { @@ -109,6 +111,7 @@ dependencies { testImplementation(libs.rpgplugins.mythiclib) { isTransitive = false } testImplementation(libs.rpgplugins.fabled) { isTransitive = false } testImplementation(libs.rpgplugins.codex) { isTransitive = false } + testImplementation(libs.rpgplugins.score) { isTransitive = false } } kotlin { diff --git a/mimic-bukkit/src/main/kotlin/MimicPlugin.kt b/mimic-bukkit/src/main/kotlin/MimicPlugin.kt index 7f50b192..79655eac 100644 --- a/mimic-bukkit/src/main/kotlin/MimicPlugin.kt +++ b/mimic-bukkit/src/main/kotlin/MimicPlugin.kt @@ -23,6 +23,7 @@ import ru.endlesscode.mimic.impl.craftengine.CraftEngineItemProvider import ru.endlesscode.mimic.impl.craftengine.CraftEngineItemsRegistry import ru.endlesscode.mimic.impl.customitems.CustomItemsItemProvider import ru.endlesscode.mimic.impl.customitems.CustomItemsRegistry +import ru.endlesscode.mimic.impl.executableitems.ExecutableItemsItemProvider import ru.endlesscode.mimic.impl.fabled.FabledClassProvider import ru.endlesscode.mimic.impl.fabled.FabledLevelProvider import ru.endlesscode.mimic.impl.fabled.FabledResourceProvider @@ -126,6 +127,10 @@ public class MimicPlugin : JavaPlugin() { hookBlocks(::CraftEngineBlockProvider) } + pluginHooks("ExecutableItems", "com.ssomar.score.api.executableitems.ExecutableItemsAPI") { + hookItems(::ExecutableItemsItemProvider) + } + pluginHooks("AuraSkills", "dev.aurelium.auraskills.api.AuraSkillsApi") { hookLevels(::AuraSkillsLevelProvider) hookResources(::AuraSkillsResourceProvider) diff --git a/mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsItemProvider.kt b/mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsItemProvider.kt new file mode 100644 index 00000000..c7169fd2 --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsItemProvider.kt @@ -0,0 +1,61 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.executableitems + +import net.kyori.adventure.key.Key +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.internal.Log +import ru.endlesscode.mimic.items.ItemProvider +import java.util.concurrent.ConcurrentHashMap + +/** + * Item provider for ExecutableItems. + * ExecutableItems IDs are lowercased and characters not allowed in item IDs are replaced with `_`: + * `Free_Hammer` -> `free_hammer`. + * If several ExecutableItems IDs become the same item ID, the one equal to the item ID is used, otherwise the first one. + * Takes no payload. An item created for a player is built by ExecutableItems with this player as its owner. + */ +internal class ExecutableItemsItemProvider( + private val executableItems: ExecutableItemsWrapper = ExecutableItemsWrapper(), +) : ItemProvider { + + private val reportedDuplicates = ConcurrentHashMap.newKeySet() + + override val id: String = ID + override val isEnabled: Boolean get() = executableItems.isEnabled + + override val allItems: Collection + get() { + val idsByMimicId = executableItems.itemIds.groupBy { it.toMimicId() } + for ((mimicId, sameIds) in idsByMimicId) { + if (sameIds.size > 1 && reportedDuplicates.add(mimicId)) { + val usedId = if (mimicId in sameIds) mimicId else sameIds.first() + Log.w("[ExecutableItems] IDs $sameIds have the same Mimic ID '$mimicId', only '$usedId' is used.") + } + } + return idsByMimicId.keys.toList() + } + + override fun hasItem(itemId: String): Boolean = findExecutableItemsId(itemId) != null + + override fun getItemId(item: ItemStack): String? = executableItems.getItemId(item)?.toMimicId() + + override fun createItem(itemId: String, amount: Int, player: Player?, payload: String?): ItemStack? { + val executableItemsId = findExecutableItemsId(itemId) ?: return null + return executableItems.createItem(executableItemsId, amount, player) + } + + /** Returns ExecutableItems ID for the given [itemId], or `null` if there is no such item. */ + private fun findExecutableItemsId(itemId: String): String? = + itemId.takeIf(executableItems::hasItem) ?: executableItems.itemIds.find { it.toMimicId() == itemId } + + private fun String.toMimicId(): String = buildString { + for (char in this@toMimicId.lowercase()) append(if (Key.allowedInValue(char)) char else '_') + } + + companion object { + const val ID: String = "executableitems" + } +} diff --git a/mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsWrapper.kt b/mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsWrapper.kt new file mode 100644 index 00000000..543ef199 --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/executableitems/ExecutableItemsWrapper.kt @@ -0,0 +1,29 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.executableitems + +import com.ssomar.score.api.executableitems.ExecutableItemsAPI +import com.ssomar.score.api.executableitems.config.ExecutableItemsManagerInterface +import org.bukkit.Bukkit +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import java.util.Optional +import kotlin.jvm.optionals.getOrNull + +/** Wraps ExecutableItems static API, which is a part of SCore. All IDs are ExecutableItems IDs. */ +internal class ExecutableItemsWrapper { + + val isEnabled: Boolean get() = Bukkit.getPluginManager().isPluginEnabled("ExecutableItems") + + private val manager: ExecutableItemsManagerInterface get() = ExecutableItemsAPI.getExecutableItemsManager() + + val itemIds: List get() = manager.executableItemIdsList + + fun hasItem(id: String): Boolean = manager.isValidID(id) + fun getItemId(item: ItemStack): String? = manager.getExecutableItem(item).getOrNull()?.id + + /** Creates item with the given [amount]. The [player] becomes the owner of the item if it is given. */ + fun createItem(id: String, amount: Int, player: Player?): ItemStack? = + manager.getExecutableItem(id).getOrNull()?.buildItem(amount, Optional.ofNullable(player)) +} diff --git a/mimic-bukkit/src/test/kotlin/impl/executableitems/ExecutableItemsItemProviderTest.kt b/mimic-bukkit/src/test/kotlin/impl/executableitems/ExecutableItemsItemProviderTest.kt new file mode 100644 index 00000000..35d790fb --- /dev/null +++ b/mimic-bukkit/src/test/kotlin/impl/executableitems/ExecutableItemsItemProviderTest.kt @@ -0,0 +1,87 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.executableitems + +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.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 ExecutableItemsItemProviderTest : BukkitTestBase() { + + private val itemIds = mutableListOf("Free_Hammer", "Ruby Sword", "ruby_sword", "Ruby_Sword") + + private val executableItems: ExecutableItemsWrapper = mockk { + every { itemIds } answers { this@ExecutableItemsItemProviderTest.itemIds } + every { hasItem(any()) } answers { firstArg() in this@ExecutableItemsItemProviderTest.itemIds } + every { getItemId(any()) } returns null + every { createItem(any(), any(), any()) } answers { ItemStack(Material.STICK, secondArg()) } + } + + // SUT + private val provider = ExecutableItemsItemProvider(executableItems) + + @Test + fun `when get all items - should return converted ids without duplicates`() { + provider.allItems shouldContainExactly listOf("free_hammer", "ruby_sword") + } + + @ParameterizedTest + @CsvSource( + "free_hammer, Free_Hammer", + "ruby_sword, ruby_sword", + ) + fun `when create item - should use ExecutableItems id`(itemId: String, executableItemsId: String) { + val item = provider.createItem(itemId, amount = 2, player, payload = "ignored") + + item?.amount shouldBe 2 + verify { executableItems.createItem(executableItemsId, 2, player) } + } + + @Test + fun `when create item and ids collide without exact id - should use the first id`() { + itemIds.remove("ruby_sword") + + provider.createItem("ruby_sword", amount = 1, player = null, payload = null) + + verify { executableItems.createItem("Ruby Sword", 1, null) } + } + + @ParameterizedTest + @ValueSource(strings = ["unknown", "free_hammer/extra"]) + fun `when create unknown item - should return null`(itemId: String) { + provider.createItem(itemId, amount = 1, player = null, payload = null).shouldBeNull() + } + + @ParameterizedTest + @CsvSource( + "free_hammer, true", + "ruby_sword, true", + "unknown, false", + ) + fun `when check has item - should return if item exists`(itemId: String, expected: Boolean) { + provider.hasItem(itemId) shouldBe expected + } + + @Test + fun `when get item id - should return converted id`() { + every { executableItems.getItemId(any()) } returns "Free_Hammer" + + provider.getItemId(ItemStack(Material.STICK)) shouldBe "free_hammer" + } + + @Test + fun `when get id of not ExecutableItems item - should return null`() { + provider.getItemId(ItemStack(Material.STICK)).shouldBeNull() + } +}