From 9507f212204078b8d31a03e1a2244df31bea6835 Mon Sep 17 00:00:00 2001 From: Polyana Date: Thu, 21 Dec 2023 22:44:07 -0300 Subject: [PATCH] PC-1070 Fixes initialization error --- .../br/com/gamemods/minecity/api/MineCity.kt | 16 ++++----------- .../minecity/api/id/ClaimPermissionId.kt | 2 +- .../minecity/api/service/MineCityInternal.kt | 15 ++++++++++++++ .../gamemods/minecity/core/MineCityCore.kt | 2 ++ .../permission/CorePermissionService.kt | 20 +++++++++++++++++++ .../minecity/fabric/MineCityFabric.kt | 10 +++++++--- 6 files changed, 49 insertions(+), 16 deletions(-) create mode 100644 api/src/main/kotlin/br/com/gamemods/minecity/api/service/MineCityInternal.kt create mode 100644 core/src/main/kotlin/br/com/gamemods/minecity/core/service/permission/CorePermissionService.kt diff --git a/api/src/main/kotlin/br/com/gamemods/minecity/api/MineCity.kt b/api/src/main/kotlin/br/com/gamemods/minecity/api/MineCity.kt index 6468400..1b17c39 100644 --- a/api/src/main/kotlin/br/com/gamemods/minecity/api/MineCity.kt +++ b/api/src/main/kotlin/br/com/gamemods/minecity/api/MineCity.kt @@ -1,10 +1,10 @@ package br.com.gamemods.minecity.api import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi +import br.com.gamemods.minecity.api.service.MineCityInternal import br.com.gamemods.minecity.api.service.namedplayer.NamedPlayerService import br.com.gamemods.minecity.api.service.permission.PermissionService -private lateinit var currentInstance: MineCity /** * Interface that allows other mods to interact with MineCity. @@ -27,16 +27,8 @@ public interface MineCity { /** * This companion object allows MineCity interface to be used directly in kotlin delegating all API calls to - * the instance that is set at [instance]. - * - * @property instance Access to the MineCity API implementation, must be modified only by MineCity itself, can be accessed freely. + * the instance that is set at [MineCityInternal.implementation]. */ - public companion object: MineCity by currentInstance { - public var instance: MineCity - get() = currentInstance - @InternalMineCityApi - set(value) { - currentInstance = value - } - } + @OptIn(InternalMineCityApi::class) + public companion object: MineCity by MineCityInternal.implementation } diff --git a/api/src/main/kotlin/br/com/gamemods/minecity/api/id/ClaimPermissionId.kt b/api/src/main/kotlin/br/com/gamemods/minecity/api/id/ClaimPermissionId.kt index 25e3c0f..1591daf 100644 --- a/api/src/main/kotlin/br/com/gamemods/minecity/api/id/ClaimPermissionId.kt +++ b/api/src/main/kotlin/br/com/gamemods/minecity/api/id/ClaimPermissionId.kt @@ -31,7 +31,7 @@ public value class ClaimPermissionId(private val id: String) { /** * The regex pattern that valid ClaimPermissionId instances must match. */ - private val VALID_REGEX = Regex("^([a-z][a-z0-9]*)(_[a-z][a-z0-9]*)*:/([a-z][a-z0-9]*)(_[a-z][a-z0-9]*)*$") + private val VALID_REGEX = Regex("^([a-z][a-z0-9]*)(_[a-z][a-z0-9]*)*:([a-z][a-z0-9]*)(_[a-z][a-z0-9]*)*$") /** * ClaimPermissionId instance for door-related permissions. diff --git a/api/src/main/kotlin/br/com/gamemods/minecity/api/service/MineCityInternal.kt b/api/src/main/kotlin/br/com/gamemods/minecity/api/service/MineCityInternal.kt new file mode 100644 index 0000000..2c3d9f4 --- /dev/null +++ b/api/src/main/kotlin/br/com/gamemods/minecity/api/service/MineCityInternal.kt @@ -0,0 +1,15 @@ +package br.com.gamemods.minecity.api.service + +import br.com.gamemods.minecity.api.MineCity +import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi + +/** + * Internal MineCity states and services, should not be used by API users. + */ +@InternalMineCityApi +public object MineCityInternal { + /** + * Access to the MineCity API implementation, must be modified only by MineCity itself, can be accessed freely. + */ + public lateinit var implementation: MineCity +} diff --git a/core/src/main/kotlin/br/com/gamemods/minecity/core/MineCityCore.kt b/core/src/main/kotlin/br/com/gamemods/minecity/core/MineCityCore.kt index faee2b5..544fed9 100644 --- a/core/src/main/kotlin/br/com/gamemods/minecity/core/MineCityCore.kt +++ b/core/src/main/kotlin/br/com/gamemods/minecity/core/MineCityCore.kt @@ -5,6 +5,7 @@ import br.com.gamemods.minecity.api.MineCityPlatform import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi import br.com.gamemods.minecity.api.annotation.side.ServerSideOnly import br.com.gamemods.minecity.api.service.namedplayer.NamedPlayerService +import br.com.gamemods.minecity.api.service.permission.PermissionService import br.com.gamemods.minecity.core.service.world.WorldService import com.github.michaelbull.logging.InlineLogger @@ -21,6 +22,7 @@ class MineCityCore( override val platform: MineCityPlatform, val worlds: WorldService, override val players: NamedPlayerService, + override val permission: PermissionService, ): MineCity { /** diff --git a/core/src/main/kotlin/br/com/gamemods/minecity/core/service/permission/CorePermissionService.kt b/core/src/main/kotlin/br/com/gamemods/minecity/core/service/permission/CorePermissionService.kt new file mode 100644 index 0000000..665bcbe --- /dev/null +++ b/core/src/main/kotlin/br/com/gamemods/minecity/core/service/permission/CorePermissionService.kt @@ -0,0 +1,20 @@ +package br.com.gamemods.minecity.core.service.permission + +import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi +import br.com.gamemods.minecity.api.id.ClaimPermissionId +import br.com.gamemods.minecity.api.service.permission.ClaimPermission +import br.com.gamemods.minecity.api.service.permission.PermissionService + +@InternalMineCityApi +class CorePermissionService: PermissionService { + private val permissions = mutableMapOf() + + override fun get(id: ClaimPermissionId): ClaimPermission = permissions[id] ?: error("Unknown permission $id") + + override fun plusAssign(permission: ClaimPermission) { + permissions.compute(permission.id) { _, old -> + if(old == null) permission + else error("Permission ${permission.id} already registered") + } + } +} diff --git a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt index 03572e9..d6a783d 100644 --- a/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt +++ b/platform/fabric/src/main/kotlin/br/com/gamemods/minecity/fabric/MineCityFabric.kt @@ -5,9 +5,11 @@ import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi import br.com.gamemods.minecity.api.annotation.side.ServerSideOnly import br.com.gamemods.minecity.api.client.MineCityClient import br.com.gamemods.minecity.api.math.pos.* +import br.com.gamemods.minecity.api.service.MineCityInternal import br.com.gamemods.minecity.core.MineCityCore import br.com.gamemods.minecity.core.dispatchers.Async import br.com.gamemods.minecity.core.dispatchers.Sync +import br.com.gamemods.minecity.core.service.permission.CorePermissionService import br.com.gamemods.minecity.fabric.math.pos.FabricBlockLocation import br.com.gamemods.minecity.fabric.math.pos.FabricChunkLocation import br.com.gamemods.minecity.fabric.math.pos.FabricEntityLocation @@ -25,6 +27,8 @@ import com.github.michaelbull.logging.InlineLogger import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.asCoroutineDispatcher import net.fabricmc.api.ModInitializer +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerChunkEvents +import net.fabricmc.fabric.api.event.lifecycle.v1.ServerLifecycleEvents import net.minecraft.server.MinecraftServer import net.minecraft.server.world.ServerWorld import net.minecraft.world.chunk.WorldChunk @@ -58,16 +62,16 @@ object MineCityFabric : ModInitializer, MineCityPlatform { platform = this, worlds = FabricWorldService(this), players = FabricNamedPlayerService(this), + permission = CorePermissionService(), ) - //FIXME - /*MineCity.instance = core + MineCityInternal.implementation = core core.onInitialize() ServerLifecycleEvents.SERVER_STARTING.register(this::handleServerStarting) ServerLifecycleEvents.SERVER_STARTED.register(this::handleServerStarted) ServerLifecycleEvents.SERVER_STOPPING.register(this::handleServerStopping) ServerLifecycleEvents.SERVER_STOPPED.register(this::handleServerStopped) ServerChunkEvents.CHUNK_LOAD.register(this::handleChunkLoad) - ServerChunkEvents.CHUNK_UNLOAD.register(this::handleChunkUnload)*/ + ServerChunkEvents.CHUNK_UNLOAD.register(this::handleChunkUnload) registerPermissions() }