diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b81c923..16852cba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,14 @@ 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 + +- New item and block providers: [ItemsAdder](https://www.spigotmc.org/resources/73355/) ([#49](https://github.com/EndlessCodeGroup/Mimic/issues/49)), + [Oraxen](https://github.com/oraxen/oraxen) ([#51](https://github.com/EndlessCodeGroup/Mimic/issues/51)). +- **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`. + + ## [v0.10.0] (2026-11-15) ### Levels API @@ -93,10 +101,6 @@ 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 03a56523..cc5404db 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, ItemsAdder | +| **Items** | Vanilla items, CraftEngine, Oraxen, MMOItems, CustomItems, ItemsAdder, RPGInventory¹ | +| **Blocks** | Vanilla blocks, CraftEngine, ItemsAdder, Oraxen | | **Inventory** | Vanilla inventory, RPGInventory¹ | ¹ Support is provided by the plugin itself. diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index d448b560..afa4e9fd 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -13,6 +13,7 @@ mmoitems = "6.10.1-SNAPSHOT" mythiclib = "1.7.1-SNAPSHOT" heroes = "1.9.30-RELEASE" craftengine = "26.9.1" +oraxen = "1.220.1" auraskills = "2.4.0" mcmmo = "2.3.002" jobs = "5.2.6.3" @@ -50,6 +51,7 @@ rpgplugins-mythiclib = { module = "io.lumine:MythicLib-dist", version.ref = "myt rpgplugins-heroes = { module = "com.herocraftonline.heroes:Heroes", version.ref = "heroes" } rpgplugins-craftengine-core = { module = "net.momirealms:craft-engine-core", version.ref = "craftengine" } rpgplugins-craftengine-bukkit = { module = "net.momirealms:craft-engine-bukkit", version.ref = "craftengine" } +rpgplugins-oraxen = { module = "io.th0rgal:oraxen", version.ref = "oraxen" } rpgplugins-auraskills = { module = "dev.aurelium:auraskills-api", version.ref = "auraskills" } rpgplugins-mcmmo = { module = "com.gmail.nossr50.mcMMO:mcMMO", version.ref = "mcmmo" } rpgplugins-jobs = { module = "com.github.Zrips:Jobs", version.ref = "jobs" } @@ -88,6 +90,7 @@ rpgplugins = [ "rpgplugins-heroes", "rpgplugins-craftengine-core", "rpgplugins-craftengine-bukkit", + "rpgplugins-oraxen", "rpgplugins-auraskills", "rpgplugins-mcmmo", "rpgplugins-jobs", diff --git a/mimic-bukkit/README.md b/mimic-bukkit/README.md index 402109af..99aaea45 100644 --- a/mimic-bukkit/README.md +++ b/mimic-bukkit/README.md @@ -96,6 +96,11 @@ Providers marked with _(deprecated API)_ are provided by the plugin itself via t **ID Format:** `craftengine:[namespace]/[id]`\ **Payload:** *Not supported* +#### [Oraxen] + +**ID Format:** `oraxen:[id]`\ +**Payload:** *Not supported* + #### [CustomItems] **ID Format:** `customitems:[id]`\ @@ -143,6 +148,11 @@ A block belongs to the provider with the highest priority that recognizes it, va **ID Format:** `itemsadder:[id]` for the default `itemsadder` namespace, `itemsadder:[namespace]/[id]` for others\ **State:** *Not supported* +#### [Oraxen] + +**ID Format:** `oraxen:[id]`, where ID is the ID of the item placing the block\ +**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 @@ -155,6 +165,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 +[oraxen]: https://github.com/oraxen/oraxen [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 02691d99..3d8bf5d7 100644 --- a/mimic-bukkit/build.gradle.kts +++ b/mimic-bukkit/build.gradle.kts @@ -28,6 +28,7 @@ bukkit { "MMOItems", "Heroes", "CraftEngine", + "Oraxen", "AuraSkills", "mcMMO", "Jobs", @@ -57,6 +58,11 @@ repositories { includeGroup("net.momirealms") } } + maven(url = "https://repo.oraxen.com/releases/") { + content { + includeGroup("io.th0rgal") + } + } maven(url = "https://nexus.neetgames.com/repository/maven-releases/") { content { includeGroup("com.gmail.nossr50.mcMMO") @@ -99,18 +105,8 @@ dependencies { compileOnly(":CustomItemsAPI") testImplementation(libs.paperApi) - testImplementation(libs.rpgplugins.auraskills) - testImplementation(libs.rpgplugins.mcmmo) { isTransitive = false } - testImplementation(libs.rpgplugins.jobs) { isTransitive = false } + testImplementation(libs.bundles.rpgplugins) { isTransitive = false } testImplementation(libs.rpgplugins.cmilib) { isTransitive = false } - testImplementation(libs.rpgplugins.valhallammo) { isTransitive = false } - testImplementation(libs.rpgplugins.heroes) { isTransitive = false } - testImplementation(libs.rpgplugins.mmocore) { isTransitive = false } - testImplementation(libs.rpgplugins.mmoitems) { isTransitive = false } - 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/MimicPlugin.kt b/mimic-bukkit/src/main/kotlin/MimicPlugin.kt index 17c456ad..bf1ee574 100644 --- a/mimic-bukkit/src/main/kotlin/MimicPlugin.kt +++ b/mimic-bukkit/src/main/kotlin/MimicPlugin.kt @@ -43,6 +43,8 @@ import ru.endlesscode.mimic.impl.mmocore.MmoCoreLevelSystem import ru.endlesscode.mimic.impl.mmocore.MmoCoreResourceProvider import ru.endlesscode.mimic.impl.mmoitems.MmoItemsItemProvider import ru.endlesscode.mimic.impl.mmoitems.MmoItemsRegistry +import ru.endlesscode.mimic.impl.oraxen.OraxenBlockProvider +import ru.endlesscode.mimic.impl.oraxen.OraxenItemProvider import ru.endlesscode.mimic.impl.valhallammo.ValhallaMmoLevelProvider import ru.endlesscode.mimic.impl.vanilla.* import ru.endlesscode.mimic.internal.Log @@ -128,6 +130,11 @@ public class MimicPlugin : JavaPlugin() { hookBlocks(::CraftEngineBlockProvider) } + pluginHooks("Oraxen", "io.th0rgal.oraxen.api.OraxenBlocks") { + hookItems(::OraxenItemProvider) + hookBlocks(::OraxenBlockProvider) + } + pluginHooks("AuraSkills", "dev.aurelium.auraskills.api.AuraSkillsApi") { hookLevels(::AuraSkillsLevelProvider) hookResources(::AuraSkillsResourceProvider) diff --git a/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenBlockProvider.kt b/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenBlockProvider.kt new file mode 100644 index 00000000..33b53dfd --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenBlockProvider.kt @@ -0,0 +1,79 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.oraxen + +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 +import ru.endlesscode.mimic.internal.OnceWarnings + +/** + * Block provider for Oraxen custom blocks. Furniture is not a block. + * ID of a block is the ID of the Oraxen item placing it, converted in the same way as in [OraxenItemProvider]. + * Variants of a directional block and their items have the ID of their parent block. + * Block states are not supported in [setBlock], every state is invalid. + * + * Drops in [breakBlock] are rolled from the Oraxen drop configured for the tool. + * Without a tool, requirements of the drop are treated as satisfied, + * but the drop is the one configured for an empty hand: + * Oraxen has no API to select a breaking rule requiring a tool without this tool. + * + * Contents of Oraxen storage blocks are dropped on the ground by Oraxen when the block is removed, + * so [breakBlock] doesn't pass them to the drops consumer. + * Oraxen has no API to take these items before removal. + */ +internal class OraxenBlockProvider( + private val oraxen: OraxenWrapper = OraxenWrapper(), +) : BlockProvider { + + private val warnings = OnceWarnings() + + override val id: String = ID + override val isEnabled: Boolean get() = oraxen.isEnabled + + override val allBlocks: Collection + get() = mimicIds(oraxen.blockIds, warnings, TAG) + + override fun hasBlock(blockId: String): Boolean = findBlock(blockId) != null + + override fun getBlockId(block: Block): String? = oraxen.getBlockId(block)?.toMimicId() + + override fun getBlockIdFromItem(item: ItemStack): String? = oraxen.getBlockIdFromItem(item)?.toMimicId() + + override fun createBlockItem(blockId: String, amount: Int): ItemStack? { + val oraxenId = findBlock(blockId) ?: return null + return oraxen.createItem(oraxenId)?.also { it.amount = amount } + } + + override fun setBlock(block: Block, blockId: String, state: String?): Boolean { + val oraxenId = findBlock(blockId) ?: return false + if (state != null) { + Log.w("[$TAG] Invalid state for block $blockId: $state") + return false + } + return oraxen.placeBlock(block, oraxenId) + } + + override fun breakBlock(block: Block, tool: ItemStack?, dropsConsumer: DropsConsumer): Boolean { + val drops = oraxen.getDrops(block, tool) ?: return false + if (!oraxen.removeBlock(block)) return false + + for (item in dropsConsumer.consume(drops)) { + block.world.dropItemNaturally(block.location, item) + } + return true + } + + private fun findBlock(blockId: String): String? { + if (oraxen.isBlock(blockId)) return blockId + return oraxen.itemIds.find { it.toMimicId() == blockId && oraxen.isBlock(it) } + } + + companion object { + const val ID: String = "oraxen" + private const val TAG = "OraxenBlockProvider" + } +} diff --git a/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenItemProvider.kt b/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenItemProvider.kt new file mode 100644 index 00000000..293a39df --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenItemProvider.kt @@ -0,0 +1,45 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.oraxen + +import org.bukkit.entity.Player +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.internal.OnceWarnings +import ru.endlesscode.mimic.items.ItemProvider + +/** + * Item provider for Oraxen custom items. + * Oraxen IDs are lowercased, characters not allowed in item ID are replaced with `_`: `Ruby Sword` -> `ruby_sword`. + * If two IDs become the same, only one of the items is available. + * Takes no payload, items don't depend on a player. + */ +internal class OraxenItemProvider( + private val oraxen: OraxenWrapper = OraxenWrapper(), +) : ItemProvider { + + private val warnings = OnceWarnings() + + override val id: String = ID + override val isEnabled: Boolean get() = oraxen.isEnabled + + override val allItems: Collection + get() = mimicIds(oraxen.itemIds, warnings, TAG) + + override fun hasItem(itemId: String): Boolean = findItem(itemId) != null + + override fun getItemId(item: ItemStack): String? = oraxen.getItemId(item)?.toMimicId() + + override fun createItem(itemId: String, amount: Int, player: Player?, payload: String?): ItemStack? { + val oraxenId = findItem(itemId) ?: return null + return oraxen.createItem(oraxenId)?.also { it.amount = amount } + } + + private fun findItem(itemId: String): String? = + if (oraxen.hasItem(itemId)) itemId else oraxen.itemIds.find { it.toMimicId() == itemId } + + companion object { + const val ID: String = "oraxen" + private const val TAG = "OraxenItemProvider" + } +} diff --git a/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenWrapper.kt b/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenWrapper.kt new file mode 100644 index 00000000..c4597d92 --- /dev/null +++ b/mimic-bukkit/src/main/kotlin/impl/oraxen/OraxenWrapper.kt @@ -0,0 +1,135 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.oraxen + +import io.th0rgal.oraxen.api.OraxenBlocks +import io.th0rgal.oraxen.api.OraxenItems +import io.th0rgal.oraxen.mechanics.provided.gameplay.chorusblock.ChorusBlockMechanic +import io.th0rgal.oraxen.mechanics.provided.gameplay.noteblock.NoteBlockMechanic +import io.th0rgal.oraxen.mechanics.provided.gameplay.noteblock.directional.DirectionalBlock +import io.th0rgal.oraxen.mechanics.provided.gameplay.shaped.ShapedBlockMechanic +import io.th0rgal.oraxen.mechanics.provided.gameplay.stringblock.StringBlockMechanic +import io.th0rgal.oraxen.mechanics.provided.gameplay.stringblock.StringBlockMechanicFactory +import io.th0rgal.oraxen.utils.drops.Drop +import org.bukkit.Bukkit +import org.bukkit.Material +import org.bukkit.block.Block +import org.bukkit.block.data.type.Slab +import org.bukkit.block.data.type.Tripwire +import org.bukkit.enchantments.Enchantment +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.internal.OnceWarnings +import kotlin.random.Random + +/** + * Wraps Oraxen static API. + * [getDrops] repeats the logic of Oraxen's `Drop.spawns`, because Oraxen can only drop items on the ground itself. + */ +internal class OraxenWrapper { + + val isEnabled: Boolean get() = Bukkit.getPluginManager().isPluginEnabled("Oraxen") + + val itemIds: Set get() = OraxenItems.getNames() + + /** IDs of all items placing a block, including the items hidden from Oraxen commands. */ + val blockIds: Set get() = itemIds.filterTo(LinkedHashSet(), OraxenBlocks::isOraxenBlock) + + fun hasItem(id: String): Boolean = OraxenItems.exists(id) + fun isBlock(id: String): Boolean = hasItem(id) && OraxenBlocks.isOraxenBlock(id) + + /** Returns ID of the [item] if this item still exists in Oraxen. */ + fun getItemId(item: ItemStack): String? = OraxenItems.getIdByItem(item)?.takeIf(OraxenItems::exists) + + fun createItem(id: String): ItemStack? = OraxenItems.getItemById(id)?.build() + + fun getBlockId(block: Block): String? { + val mechanic = OraxenBlocks.getOraxenBlock(block.location) ?: return null + return blockId(mechanic.itemID, (mechanic as? NoteBlockMechanic)?.directional) + } + + /** Returns ID of the block placed by the [item], or `null` if the item places no block. */ + fun getBlockIdFromItem(item: ItemStack): String? { + val itemId = getItemId(item)?.takeIf(OraxenBlocks::isOraxenBlock) ?: return null + return blockId(itemId, OraxenBlocks.getNoteBlockMechanic(itemId)?.directional) + } + + fun placeBlock(block: Block, id: String): Boolean { + OraxenBlocks.place(id, block.location) + return OraxenBlocks.isOraxenBlock(block) + } + + /** Removes the [block] without drops. */ + fun removeBlock(block: Block): Boolean = OraxenBlocks.remove(block.location, null, Drop.emptyDrop()) + + /** Returns items dropped by the [block] broken with the [tool], or `null` if it isn't an Oraxen block. */ + fun getDrops(block: Block, tool: ItemStack?): List? { + // Breaking rules of a block are private in Oraxen, so without a tool only the rule for an empty hand is known + val toolOrHand = tool ?: ItemStack(Material.AIR) + val blockData = block.blockData + val drop = when (val mechanic = OraxenBlocks.getOraxenBlock(block.location)) { + is NoteBlockMechanic -> mechanic.getDrop(toolOrHand) + is ChorusBlockMechanic -> mechanic.getDrop(toolOrHand) + is ShapedBlockMechanic -> mechanic.getDrop(toolOrHand) + + // Every level of a stackable block drops its loot + is StringBlockMechanic -> { + val drop = mechanic.getDrop(toolOrHand) + val levels = (blockData as? Tripwire) + ?.let { mechanic.getStackDropMultiplier(StringBlockMechanicFactory.getCode(it)) } ?: 1 + Drop.clone(drop, List(levels) { drop.loots }.flatten()) + } + + else -> return null + } + + // Double slab drops as two slabs + val rolls = if (blockData is Slab && blockData.type == Slab.Type.DOUBLE) 2 else 1 + return List(rolls) { drop.roll(tool) }.flatten() + } +} + +/** Returns ID of the block placed by the item. Variants of a directional block have the ID of their parent block. */ +internal fun blockId(itemId: String, directional: DirectionalBlock?): String = directional?.parentBlock ?: itemId + +/** + * Rolls items dropped with the [tool]. + * If the tool is `null`, requirements of the drop are treated as satisfied and no enchantments are applied. + */ +internal fun Drop.roll(tool: ItemStack?): List { + if (tool == null) return rollLoots(fortuneMultiplier = 1, tool = null) + if (!canDrop(tool)) return emptyList() + if (isSilktouch && tool.containsEnchantment(Enchantment.SILK_TOUCH)) { + return listOfNotNull(OraxenItems.getItemById(sourceID)?.build()) + } + + val fortuneLevel = if (isFortune) tool.getEnchantmentLevel(Enchantment.FORTUNE) else 0 + return rollLoots(fortuneMultiplier = 1 + if (fortuneLevel > 0) Random.nextInt(fortuneLevel) else 0, tool) +} + +private fun Drop.rollLoots(fortuneMultiplier: Int, tool: ItemStack?): List = loots + .filter { Random.nextDouble() <= it.probability } + .mapNotNull { it.getItem(fortuneMultiplier, tool) } + +/** Converts Oraxen ID to Mimic ID: `Ruby Sword` -> `ruby_sword`. */ +internal fun String.toMimicId(): String = lowercase().replace(notAllowedChars, "_") + +/** + * Converts the given Oraxen [ids] to Mimic IDs. + * If two IDs become the same, the ID needing no conversion or the first one is kept, + * the others are skipped with a warning logged once. + */ +internal fun mimicIds(ids: Collection, warnings: OnceWarnings, tag: String): Collection { + val (exactIds, convertedIds) = ids.partition { it == it.toMimicId() } + val result = LinkedHashMap() + for (id in exactIds + convertedIds) { + val mimicId = id.toMimicId() + val keptId = result.putIfAbsent(mimicId, id) + if (keptId != null) { + warnings.warn(id) { "[$tag] '$id' is skipped: its ID '$mimicId' is already used by '$keptId'" } + } + } + return result.keys +} + +private val notAllowedChars = Regex("[^a-z0-9._/-]") diff --git a/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenBlockProviderTest.kt b/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenBlockProviderTest.kt new file mode 100644 index 00000000..d8545db6 --- /dev/null +++ b/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenBlockProviderTest.kt @@ -0,0 +1,136 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.oraxen + +import io.kotest.matchers.booleans.shouldBeFalse +import io.kotest.matchers.booleans.shouldBeTrue +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 ru.endlesscode.mimic.BukkitTestBase +import kotlin.test.Test + +internal class OraxenBlockProviderTest : BukkitTestBase() { + + private val blocks = setOf("ruby_block", "Ruby Ore") + + private val world: World = mockk { + every { dropItemNaturally(any(), any()) } returns mockk() + } + private val block: Block = mockk { + every { world } returns this@OraxenBlockProviderTest.world + every { location } returns Location(this@OraxenBlockProviderTest.world, 0.0, 0.0, 0.0) + } + + private val oraxen: OraxenWrapper = mockk { + every { itemIds } returns blocks + "ruby_sword" + every { blockIds } returns blocks + every { isBlock(any()) } answers { firstArg() in blocks } + every { getBlockId(any()) } returns null + every { getBlockIdFromItem(any()) } returns null + every { getDrops(any(), any()) } returns null + every { createItem(any()) } answers { ItemStack(Material.PAPER) } + every { placeBlock(any(), any()) } returns true + every { removeBlock(any()) } returns true + } + + // SUT + private val provider = OraxenBlockProvider(oraxen) + + @Test + fun `when get all blocks - should return converted ids`() { + provider.allBlocks.toList() shouldContainExactly listOf("ruby_block", "ruby_ore") + } + + @Test + fun `when get all blocks - every block should exist`() { + provider.allBlocks.all(provider::hasBlock).shouldBeTrue() + } + + @Test + fun `when check block exists - should find it by converted id`() { + provider.hasBlock("ruby_ore").shouldBeTrue() + provider.hasBlock("unknown").shouldBeFalse() + } + + @Test + fun `when get block id - should return converted id`() { + every { oraxen.getBlockId(block) } returns "Ruby Ore" + + provider.getBlockId(block) shouldBe "ruby_ore" + } + + @Test + fun `when get id of not Oraxen block - should return null`() { + provider.getBlockId(block).shouldBeNull() + } + + @Test + fun `when get block id from block item - should return converted id`() { + every { oraxen.getBlockIdFromItem(any()) } returns "Ruby Ore" + + provider.getBlockIdFromItem(ItemStack(Material.PAPER)) shouldBe "ruby_ore" + } + + @Test + fun `when get block id from item placing no block - should return null`() { + provider.getBlockIdFromItem(ItemStack(Material.PAPER)).shouldBeNull() + } + + @Test + fun `when create block item - should create item by original id`() { + val item = provider.createBlockItem("ruby_ore", amount = 3) + + item?.amount shouldBe 3 + verify { oraxen.createItem("Ruby Ore") } + } + + @Test + fun `when create item of unknown block - should return null`() { + provider.createBlockItem("unknown", amount = 1).shouldBeNull() + } + + @Test + fun `when set block - should place block by original id`() { + provider.setBlock(block, "ruby_ore", state = null).shouldBeTrue() + + verify { oraxen.placeBlock(block, "Ruby Ore") } + } + + @Test + fun `when set block with state or unknown block - should not place block`() { + provider.setBlock(block, "ruby_ore", state = "[facing=north]").shouldBeFalse() + provider.setBlock(block, "unknown", state = null).shouldBeFalse() + + verify(exactly = 0) { oraxen.placeBlock(any(), any()) } + } + + @Test + fun `when break block - should remove block and drop consumed items`() { + val tool = ItemStack(Material.DIAMOND_PICKAXE) + every { oraxen.getDrops(block, any()) } returns listOf(ItemStack(Material.DIAMOND), ItemStack(Material.STICK)) + + provider.breakBlock(block, tool) { drops -> drops.filter { it.type == Material.DIAMOND } }.shouldBeTrue() + + verify { oraxen.removeBlock(block) } + val dropped = mutableListOf() + verify { world.dropItemNaturally(any(), capture(dropped)) } + dropped.map { it.type } shouldContainExactly listOf(Material.DIAMOND) + } + + @Test + fun `when break not Oraxen block - should not remove block`() { + provider.breakBlock(block, tool = null) { it }.shouldBeFalse() + + verify(exactly = 0) { oraxen.removeBlock(any()) } + } +} diff --git a/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenItemProviderTest.kt b/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenItemProviderTest.kt new file mode 100644 index 00000000..113f326b --- /dev/null +++ b/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenItemProviderTest.kt @@ -0,0 +1,78 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.oraxen + +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 ru.endlesscode.mimic.BukkitTestBase +import kotlin.test.Test + +internal class OraxenItemProviderTest : BukkitTestBase() { + + private val items = mapOf( + "ruby_sword" to Material.DIAMOND_SWORD, + "Ruby Ore" to Material.PAPER, + ) + + private val oraxen: OraxenWrapper = mockk { + every { itemIds } returns items.keys + every { hasItem(any()) } answers { firstArg() in items } + every { getItemId(any()) } returns null + every { createItem(any()) } answers { ItemStack(items.getValue(firstArg())) } + } + + // SUT + private val provider = OraxenItemProvider(oraxen) + + @Test + fun `when get all items - should return converted ids`() { + provider.allItems.toList() shouldContainExactly listOf("ruby_sword", "ruby_ore") + } + + @ParameterizedTest + @CsvSource( + "ruby_sword, true", + "ruby_ore, true", + "unknown, false", + ) + fun `when check item exists - should find it by converted id`(itemId: String, expected: Boolean) { + provider.hasItem(itemId) shouldBe expected + } + + @ParameterizedTest + @CsvSource( + "ruby_sword, DIAMOND_SWORD", + "ruby_ore, PAPER", + ) + fun `when create item - should create it by original id`(itemId: String, expected: Material) { + val item = provider.createItem(itemId, amount = 2, player, payload = "ignored") + + item?.type shouldBe expected + item?.amount shouldBe 2 + } + + @Test + fun `when create unknown item - should return null`() { + provider.createItem("unknown", amount = 1, player = null, payload = null).shouldBeNull() + } + + @Test + fun `when get item id - should return converted id`() { + every { oraxen.getItemId(any()) } returns "Ruby Ore" + + provider.getItemId(ItemStack(Material.PAPER)) shouldBe "ruby_ore" + } + + @Test + fun `when get id of not Oraxen item - should return null`() { + provider.getItemId(ItemStack(Material.PAPER)).shouldBeNull() + } +} diff --git a/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenWrapperTest.kt b/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenWrapperTest.kt new file mode 100644 index 00000000..3c170bf5 --- /dev/null +++ b/mimic-bukkit/src/test/kotlin/impl/oraxen/OraxenWrapperTest.kt @@ -0,0 +1,61 @@ +// Copyright (c) 2026 Osip Fatkullin, EndlessCode Group and contributors +// SPDX-License-Identifier: MIT + +package ru.endlesscode.mimic.impl.oraxen + +import io.kotest.matchers.collections.shouldContainExactly +import io.kotest.matchers.shouldBe +import io.mockk.every +import io.mockk.mockk +import io.th0rgal.oraxen.mechanics.provided.gameplay.noteblock.directional.DirectionalBlock +import io.th0rgal.oraxen.utils.drops.Drop +import io.th0rgal.oraxen.utils.drops.Loot +import org.bukkit.Material +import org.bukkit.configuration.MemoryConfiguration +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.BukkitTestBase +import ru.endlesscode.mimic.internal.Log +import ru.endlesscode.mimic.internal.OnceWarnings +import kotlin.test.Test + +internal class OraxenWrapperTest : BukkitTestBase() { + + @Test + fun `when get id of directional block variant - should return id of parent block`() { + val directional = DirectionalBlock(MemoryConfiguration().apply { set("parent_block", "ruby_log") }) + + blockId("ruby_log_x", directional) shouldBe "ruby_log" + } + + @Test + fun `when get id of block without parent - should return its id`() { + blockId("ruby_log", DirectionalBlock(MemoryConfiguration())) shouldBe "ruby_log" + blockId("ruby_ore", directional = null) shouldBe "ruby_ore" + } + + @Test + fun `when two ids become the same - should keep the id needing no conversion and warn once`() { + val loggedWarnings = mutableListOf() + Log.init({ _, message, _ -> loggedWarnings += message.orEmpty() }) + val warnings = OnceWarnings() + val ids = listOf("Ruby_Sword", "ruby_sword", "Ruby Ore", "RUBY_ORE") + + repeat(2) { mimicIds(ids, warnings, "Tag").toList() shouldContainExactly listOf("ruby_sword", "ruby_ore") } + + loggedWarnings shouldContainExactly listOf( + "[Tag] 'Ruby_Sword' is skipped: its ID 'ruby_sword' is already used by 'ruby_sword'", + "[Tag] 'RUBY_ORE' is skipped: its ID 'ruby_ore' is already used by 'Ruby Ore'", + ) + } + + @Test + fun `when roll drop without tool - should treat tool requirements as satisfied`() { + val loot: Loot = mockk { + every { probability } returns 1.0 + every { getItem(1, null) } returns ItemStack(Material.DIAMOND) + } + val drop = Drop(listOf("WOODEN", "DIAMOND"), listOf(loot), true, true, "ruby_ore", "DIAMOND", listOf("PICKAXE")) + + drop.roll(tool = null).map { it.type } shouldContainExactly listOf(Material.DIAMOND) + } +}