Skip to content

Commit

Permalink
PC-1075 Creates an abstraction for ClaimPermission
Browse files Browse the repository at this point in the history
  • Loading branch information
Polyana committed Dec 22, 2023
1 parent a50f10b commit ef58bd2
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 1 deletion.
6 changes: 6 additions & 0 deletions api/src/main/kotlin/br/com/gamemods/minecity/api/MineCity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package br.com.gamemods.minecity.api

import br.com.gamemods.minecity.api.annotation.internal.InternalMineCityApi
import br.com.gamemods.minecity.api.service.namedplayer.NamedPlayerService
import br.com.gamemods.minecity.api.service.permission.PermissionService

private lateinit var currentInstance: MineCity

Expand All @@ -19,6 +20,11 @@ public interface MineCity {
*/
public val players: NamedPlayerService

/**
* Allows to get the claim permissions with easy
*/
public val permission: PermissionService

/**
* This companion object allows MineCity interface to be used directly in kotlin delegating all API calls to
* the instance that is set at [instance].
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package br.com.gamemods.minecity.api.id

import br.com.gamemods.minecity.api.id.ClaimPermissionId.Companion.VALID_REGEX
import kotlinx.serialization.Serializable

/**
* Represents a unique identifier for claim permissions.
* This ID is used to manage and identify different types of claim permissions.
* It follows a specific pattern as defined in [VALID_REGEX].
*
* @property id The unique string identifier for the permission.
*/
@Serializable
@JvmInline
public value class ClaimPermissionId(private val id: String) {

init {
require(id.matches(VALID_REGEX)) {
"Invalid PermissionId pattern: $id (PermissionId pattern must match: $VALID_REGEX)"
}
}

/**
* Returns the string representation of the claim permission ID.
*
* @return The claim permission ID as a string.
*/
override fun toString(): String = id

public companion object {
/**
* 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]*)*$")

/**
* ClaimPermissionId instance for door-related permissions.
*/
public val DOORS: ClaimPermissionId = ClaimPermissionId("minecity:doors")

/**
* ClaimPermissionId instance for button-related permissions.
*/
public val BUTTONS: ClaimPermissionId = ClaimPermissionId("minecity:buttons")

/**
* ClaimPermissionId instance for computer screen-related permissions.
*/
public val COMPUTER_SCREEN: ClaimPermissionId = ClaimPermissionId("minecity:computer_screen")

/**
* ClaimPermissionId instance for machine-related permissions.
*/
public val MACHINES: ClaimPermissionId = ClaimPermissionId("minecity:machines")

/**
* ClaimPermissionId instance for panel-related permissions.
*/
public val PANELS: ClaimPermissionId = ClaimPermissionId("minecity:panels")

/**
* ClaimPermissionId instance for computer-related permissions.
*/
public val COMPUTERS: ClaimPermissionId = ClaimPermissionId("minecity:computers")

/**
* ClaimPermissionId instance for building-related permissions.
*/
public val BUILD: ClaimPermissionId = ClaimPermissionId("minecity:build")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package br.com.gamemods.minecity.api.service.permission

import br.com.gamemods.minecity.api.id.ClaimPermissionId
import net.kyori.adventure.text.Component

/**
* Represents a permission that can be granted to a player in a claim.
*
* @property id The unique identifier for the permission.
* @property name The name of the permission.
* @property description The description of the permission.
*/
public abstract class ClaimPermission(
public val id: ClaimPermissionId,
public val name: Component,
public val description: Component,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package br.com.gamemods.minecity.api.service.permission

import br.com.gamemods.minecity.api.id.ClaimPermissionId
import br.com.gamemods.minecity.api.id.NamedPlayer

/**
* Manages the [NamedPlayer] objects.
*/
public interface PermissionService {
/**
* Gets [ClaimPermission] for the given [id]
*/
public operator fun get(id: ClaimPermissionId): ClaimPermission

/**
* Registers a new [ClaimPermission], the [ClaimPermission.id] must be unique.
*/
public operator fun plusAssign(permission: ClaimPermission)
}
2 changes: 1 addition & 1 deletion platform/fabric/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ dependencies {
// Fabric API. This is technically optional, but you probably want it anyway.
modImplementation("net.fabricmc.fabric-api:fabric-api:$fabricVersion")
modImplementation("net.fabricmc:fabric-language-kotlin:$fabricKotlinVersion")
//modImplementation(include("net.kyori:adventure-platform-fabric:5.9.0")!!)
modImplementation(include("net.kyori:adventure-platform-fabric:5.9.0")!!)
// Uncomment the following line to enable the deprecated Fabric API modules.
// These are included in the Fabric API production distribution and allow you to update your mod to the latest modules at a later more convenient time.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import br.com.gamemods.minecity.fabric.math.pos.FabricEntityLocation
import br.com.gamemods.minecity.fabric.server.MineCityFabricServer
import br.com.gamemods.minecity.fabric.service.FabricNamedPlayerService
import br.com.gamemods.minecity.fabric.service.FabricWorldService
import br.com.gamemods.minecity.fabric.service.permission.FabricDoorClaimPermission
import br.com.gamemods.minecity.fabric.wrapper.FabricBlockPosWrapper
import br.com.gamemods.minecity.fabric.wrapper.FabricChunkPosWrapper
import br.com.gamemods.minecity.fabric.wrapper.FabricEntityPosWrapper
Expand Down Expand Up @@ -67,6 +68,12 @@ object MineCityFabric : ModInitializer, MineCityPlatform {
ServerLifecycleEvents.SERVER_STOPPED.register(this::handleServerStopped)
ServerChunkEvents.CHUNK_LOAD.register(this::handleChunkLoad)
ServerChunkEvents.CHUNK_UNLOAD.register(this::handleChunkUnload)*/

registerPermissions()
}

private fun registerPermissions() {
core.permission += FabricDoorClaimPermission()
}

@ServerSideOnly
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package br.com.gamemods.minecity.fabric.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 net.kyori.adventure.text.Component

@InternalMineCityApi
class FabricDoorClaimPermission: ClaimPermission(
id = ClaimPermissionId.DOORS,
name = Component.text("Doors"),
description = Component.text("Allows the player to open and close doors in the claim.")
)

0 comments on commit ef58bd2

Please sign in to comment.