From 83deafc5ea187390a847610fe4b0fe6fe15716dd Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Thu, 30 Nov 2023 02:02:04 +0100 Subject: [PATCH 1/4] style: Add regions to Mimic --- mimic-bukkit-api/src/main/kotlin/Mimic.kt | 8 ++++++++ mimic-bukkit/src/main/kotlin/MimicImpl.kt | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/mimic-bukkit-api/src/main/kotlin/Mimic.kt b/mimic-bukkit-api/src/main/kotlin/Mimic.kt index 9d5ffc9c..bbb65343 100644 --- a/mimic-bukkit-api/src/main/kotlin/Mimic.kt +++ b/mimic-bukkit-api/src/main/kotlin/Mimic.kt @@ -14,6 +14,7 @@ import ru.endlesscode.mimic.level.BukkitLevelSystem /** Mimic provides access to abstraction APIs. */ public interface Mimic { + // region Class System /** * Registers the given [provider] for [BukkitClassSystem] with normal priority. * @@ -62,7 +63,9 @@ public interface Mimic { /** Returns map containing all known [BukkitClassSystem.Provider]s, where key is a provider ID. */ public fun getAllClassSystemProviders(): Map + // endregion + // region Inventory Provider /** * Registers the given [provider] for [BukkitPlayerInventory] with normal priority. * @@ -133,7 +136,9 @@ public interface Mimic { @ExperimentalMimicApi @ApiStatus.Experimental public fun getAllPlayerInventoryProviders(): Map + // endregion + // region Items Registry /** * Registers the given [registry] with normal priority. * @@ -176,7 +181,9 @@ public interface Mimic { /** Returns map containing all known [BukkitItemsRegistry], where key is an item registry ID. */ public fun getAllItemsRegistries(): Map + // endregion + // region Level System /** * Registers the given [provider] for [BukkitLevelSystem] with normal priority. * @@ -225,6 +232,7 @@ public interface Mimic { /** Returns map containing all known [BukkitLevelSystem.Provider]s, where key is a provider ID. */ public fun getAllLevelSystemProviders(): Map + // endregion public companion object { /** diff --git a/mimic-bukkit/src/main/kotlin/MimicImpl.kt b/mimic-bukkit/src/main/kotlin/MimicImpl.kt index ce66f32a..b661e771 100644 --- a/mimic-bukkit/src/main/kotlin/MimicImpl.kt +++ b/mimic-bukkit/src/main/kotlin/MimicImpl.kt @@ -23,6 +23,7 @@ internal class MimicImpl( private val config: MimicConfig, ) : Mimic { + // region Class System override fun registerClassSystem( provider: BukkitClassSystem.Provider, apiLevel: Int, @@ -34,7 +35,9 @@ internal class MimicImpl( override fun getClassSystemProvider(): BukkitClassSystem.Provider = loadService(config.classSystem) override fun getAllClassSystemProviders(): Map = loadAllServices() + // endregion + // region Inventory Provider @ExperimentalMimicApi override fun registerPlayerInventoryProvider( provider: PlayerInventoryProvider, @@ -50,7 +53,9 @@ internal class MimicImpl( @ExperimentalMimicApi override fun getAllPlayerInventoryProviders(): Map = loadAllServices() + // endregion + // region Items Registry override fun registerItemsRegistry( registry: BukkitItemsRegistry, apiLevel: Int, @@ -62,7 +67,9 @@ internal class MimicImpl( override fun getItemsRegistry(): BukkitItemsRegistry = loadService() override fun getAllItemsRegistries(): Map = loadAllServices() + // endregion + // region Level System override fun registerLevelSystem( provider: BukkitLevelSystem.Provider, apiLevel: Int, @@ -74,6 +81,7 @@ internal class MimicImpl( override fun getLevelSystemProvider(): BukkitLevelSystem.Provider = loadService(config.levelSystem) override fun getAllLevelSystemProviders(): Map = loadAllServices() + // endregion private inline fun tryRegisterService( apiLevel: Int, From be7712644e679d618ddcb138ee537731e439a0f6 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Thu, 30 Nov 2023 02:24:00 +0100 Subject: [PATCH 2/4] version: 0.9.0-SNAPSHOT --- build.gradle.kts | 2 +- mimic-api/src/main/kotlin/MimicApiLevel.kt | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/build.gradle.kts b/build.gradle.kts index 932337f2..89db5a80 100644 --- a/build.gradle.kts +++ b/build.gradle.kts @@ -7,7 +7,7 @@ plugins { // Common configurations for all Mimic projects subprojects { - version = "0.8.0" + version = "0.9.0-SNAPSHOT" group = "ru.endlesscode.mimic" } diff --git a/mimic-api/src/main/kotlin/MimicApiLevel.kt b/mimic-api/src/main/kotlin/MimicApiLevel.kt index 16d36776..abe8894e 100644 --- a/mimic-api/src/main/kotlin/MimicApiLevel.kt +++ b/mimic-api/src/main/kotlin/MimicApiLevel.kt @@ -52,6 +52,12 @@ public object MimicApiLevel { */ public const val VERSION_0_8: Int = 4 + /** + * # Version 0.9 + * - Experimental API: BlocksRegistry + */ + public const val VERSION_0_9: Int = 5 + /** * The latest version at the moment of Mimic **COMPILATION**. * @@ -60,7 +66,7 @@ public object MimicApiLevel { * Use [checkApiLevel] if you want to check that the current **RUNNING** * Mimic API level meets to the required API level. */ - public const val CURRENT: Int = VERSION_0_8 + public const val CURRENT: Int = VERSION_0_9 /** * Returns `true` if the current **RUNNING** Mimic API level is equal From f9144836796a24269bb32e9a7dde361cba2f83b2 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Thu, 30 Nov 2023 02:13:10 +0100 Subject: [PATCH 3/4] feat(blocks): Add interface --- .../src/main/kotlin/blocks/BlocksRegistry.kt | 81 +++++++++++++++++++ .../kotlin/blocks/BukkitBlocksRegistry.kt | 13 +++ 2 files changed, 94 insertions(+) create mode 100644 mimic-api/src/main/kotlin/blocks/BlocksRegistry.kt create mode 100644 mimic-bukkit-api/src/main/kotlin/blocks/BukkitBlocksRegistry.kt diff --git a/mimic-api/src/main/kotlin/blocks/BlocksRegistry.kt b/mimic-api/src/main/kotlin/blocks/BlocksRegistry.kt new file mode 100644 index 00000000..544d811f --- /dev/null +++ b/mimic-api/src/main/kotlin/blocks/BlocksRegistry.kt @@ -0,0 +1,81 @@ +package blocks + +import ru.endlesscode.mimic.ExperimentalMimicApi +import ru.endlesscode.mimic.MimicService + +/** + * Registry to get and place blocks. + * @since 0.9.0 + */ +@ExperimentalMimicApi +public interface BlocksRegistry : MimicService { + + /** Returns all known block IDs. */ + public val knownIds: Collection + + /** Returns `true` if given [block] represented with given [blockId]. */ + public fun isSameBlock(block: BlockT, blockId: String): Boolean = getBlockId(block) == blockId + + /** Returns `true` if given [blockItem] is an item stack containing the block represented with given [blockId]. */ + public fun isSameBlockItem(blockItem: ItemStackT, blockId: String): Boolean = getBlockItemId(blockItem) == blockId + + /** Returns `true` if block with given [blockId] exists. */ + public fun isBlockExists(blockId: String): Boolean + + /** Returns ID representing given [block], or `null` if the ID not found in this registry. */ + public fun getBlockId(block: BlockT): String? + + /** + * Returns ID representing the block contained in given [blockItem], + * or `null` if the ID not found in this registry. + */ + public fun getBlockItemId(blockItem: ItemStackT): String? + + /** Returns item containing block by given [blockId], or `null` if the ID not found in this registry. */ + public fun getBlockItem(blockId: String): ItemStackT? = getBlockItem(blockId, payload = null, amount = 1) + + /** + * Returns item containing block with specified [payload] by given [blockId], + * or `null` if the ID not found in this registry. + * + * If [payload] is not `null`, block will be configured using it. + */ + public fun getBlockItem(blockId: String, payload: Any?): ItemStackT? = getBlockItem(blockId, payload, amount = 1) + + /** + * Returns item containing block with specified [amount] by given [blockId], + * or `null` if ID not found in this registry. + * + * If the given [amount] is greater than maximum possible, will use maximum possible amount. + * Amount shouldn't be less than `1`. + */ + public fun getBlockItem(blockId: String, amount: Int): ItemStackT? = getBlockItem(blockId, payload = null, amount) + + /** + * Returns item containing block with specified [amount] and [payload] by given [blockId], + * or `null` if ID not found in this registry. + * + * If the given [amount] is greater than maximum possible, will use maximum possible amount. + * Amount shouldn't be less than `1`. + * + * Given [payload] may be used to configure block. + */ + public fun getBlockItem(blockId: String, payload: Any?, amount: Int): ItemStackT? + + /** + * Places block by given [blockId] at [destination]. + * + * Returns `true` if the block was placed, and `false` if ID not found in this registry. + */ + public fun placeBlock(blockId: String, destination: BlockT): Boolean = + placeBlock(blockId, payload = null, destination) + + /** + * Places block with specified [payload] by given [blockId] at [destination]. + * + * Given [payload] may be used to configure block. + * + * Returns `true` if the block was placed, and `false` if ID not found in this registry. + */ + public fun placeBlock(blockId: String, payload: Any?, destination: BlockT): Boolean +} \ No newline at end of file diff --git a/mimic-bukkit-api/src/main/kotlin/blocks/BukkitBlocksRegistry.kt b/mimic-bukkit-api/src/main/kotlin/blocks/BukkitBlocksRegistry.kt new file mode 100644 index 00000000..1a5f4804 --- /dev/null +++ b/mimic-bukkit-api/src/main/kotlin/blocks/BukkitBlocksRegistry.kt @@ -0,0 +1,13 @@ +package ru.endlesscode.mimic.blocks + +import blocks.BlocksRegistry +import org.bukkit.block.Block +import org.bukkit.inventory.ItemStack +import ru.endlesscode.mimic.ExperimentalMimicApi + +/** + * [BlocksRegistry] for Bukkit. + * @since 0.9.0 + */ +@ExperimentalMimicApi +public interface BukkitBlocksRegistry : BlocksRegistry From 2bd87bd906c6862768528edd302c7cbbbf9ac7d6 Mon Sep 17 00:00:00 2001 From: Osip Fatkullin Date: Thu, 30 Nov 2023 02:24:16 +0100 Subject: [PATCH 4/4] feat(blocks): Add accessors to Mimic interface --- mimic-bukkit-api/src/main/kotlin/Mimic.kt | 62 +++++++++++++++++++++++ mimic-bukkit/src/main/kotlin/MimicImpl.kt | 21 ++++++++ 2 files changed, 83 insertions(+) diff --git a/mimic-bukkit-api/src/main/kotlin/Mimic.kt b/mimic-bukkit-api/src/main/kotlin/Mimic.kt index bbb65343..b39abe27 100644 --- a/mimic-bukkit-api/src/main/kotlin/Mimic.kt +++ b/mimic-bukkit-api/src/main/kotlin/Mimic.kt @@ -5,6 +5,7 @@ import org.bukkit.entity.Player import org.bukkit.plugin.Plugin import org.bukkit.plugin.ServicePriority import org.jetbrains.annotations.ApiStatus +import ru.endlesscode.mimic.blocks.BukkitBlocksRegistry import ru.endlesscode.mimic.bukkit.load import ru.endlesscode.mimic.classes.BukkitClassSystem import ru.endlesscode.mimic.inventory.BukkitPlayerInventory @@ -14,6 +15,67 @@ import ru.endlesscode.mimic.level.BukkitLevelSystem /** Mimic provides access to abstraction APIs. */ public interface Mimic { + // region Blocks Registry + /** + * Registers the given [registry] with normal priority. + * + * @param registry The [BukkitBlocksRegistry] implementation + * @param apiLevel Minimal required API level for this item registry implementation: + * - if required API level is higher than installed Mimic, provider will not be registered, + * - if required API level is lower - will be enabled compatibility mode. + * Specify `MimicApiLevel.CURRENT` to use API level of Mimic dependency used on compile time. + * @param plugin The plugin implementing this item registry. + * @return registered registry or `null` if it was not registered. + * @since 0.9.0 + */ + @ExperimentalMimicApi + @ApiStatus.Experimental + public fun registerBlocksRegistry( + registry: BukkitBlocksRegistry, + apiLevel: Int, + plugin: Plugin, + ): BukkitBlocksRegistry? = registerBlocksRegistry(registry, apiLevel, plugin, ServicePriority.Normal) + + /** + * Registers the given [registry]. + * + * @param registry The [BukkitBlocksRegistry] implementation + * @param apiLevel Minimal required API level for this item registry implementation: + * - if required API level is higher than installed Mimic, provider will not be registered, + * - if required API level is lower - will be enabled compatibility mode. + * Specify `MimicApiLevel.CURRENT` to use API level of Mimic dependency used on compile time. + * @param plugin The plugin implementing this item registry. + * @param priority Default priority. Service with higher priority will be used if user have not configured + * preferred service in config. + * @return registered registry or `null` if it was not registered. + * @since 0.9.0 + */ + @ExperimentalMimicApi + @ApiStatus.Experimental + public fun registerBlocksRegistry( + registry: BukkitBlocksRegistry, + apiLevel: Int, + plugin: Plugin, + priority: ServicePriority, + ): BukkitBlocksRegistry? + + /** + * Returns [BukkitBlocksRegistry] implementation. + * @since 0.9.0 + */ + @ExperimentalMimicApi + @ApiStatus.Experimental + public fun getBlocksRegistry(): BukkitBlocksRegistry + + /** + * Returns map containing all known [BukkitBlocksRegistry], where key is an item registry ID. + * @since 0.9.0 + */ + @ExperimentalMimicApi + @ApiStatus.Experimental + public fun getAllBlocksRegistries(): Map + // endregion + // region Class System /** * Registers the given [provider] for [BukkitClassSystem] with normal priority. diff --git a/mimic-bukkit/src/main/kotlin/MimicImpl.kt b/mimic-bukkit/src/main/kotlin/MimicImpl.kt index b661e771..f7f743a8 100644 --- a/mimic-bukkit/src/main/kotlin/MimicImpl.kt +++ b/mimic-bukkit/src/main/kotlin/MimicImpl.kt @@ -3,6 +3,7 @@ package ru.endlesscode.mimic import org.bukkit.plugin.Plugin import org.bukkit.plugin.ServicePriority import org.bukkit.plugin.ServicesManager +import ru.endlesscode.mimic.blocks.BukkitBlocksRegistry import ru.endlesscode.mimic.bukkit.loadAll import ru.endlesscode.mimic.bukkit.register import ru.endlesscode.mimic.classes.BukkitClassSystem @@ -23,6 +24,25 @@ internal class MimicImpl( private val config: MimicConfig, ) : Mimic { + // region Blocks Registry + @ExperimentalMimicApi + override fun registerBlocksRegistry( + registry: BukkitBlocksRegistry, + apiLevel: Int, + plugin: Plugin, + priority: ServicePriority + ): BukkitBlocksRegistry? = tryRegisterService(apiLevel, plugin, priority) { + TODO("Not implemented yet") + // WrappedBlocksRegistry(registry, config, plugin) + } + + @ExperimentalMimicApi + override fun getBlocksRegistry(): BukkitBlocksRegistry = loadService() + + @ExperimentalMimicApi + override fun getAllBlocksRegistries(): Map = loadAllServices() + // endregion + // region Class System override fun registerClassSystem( provider: BukkitClassSystem.Provider, @@ -146,6 +166,7 @@ internal class MimicImpl( BukkitLevelSystem.Provider::class -> "LevelSystem" BukkitPlayerInventory.Provider::class -> "PlayerInventory" BukkitItemsRegistry::class -> "ItemsRegistry" + BukkitBlocksRegistry::class -> "BlocksRegistry" else -> error("Unknown service: ${this.java.name}") } }