diff options
| author | Sho Sakuma <me@m1sk9.dev> | 2026-01-07 12:57:32 +0900 |
|---|---|---|
| committer | Sho Sakuma <me@m1sk9.dev> | 2026-01-07 12:57:38 +0900 |
| commit | f6f2ec77fa773250b2663491628f14e5baa92f5d (patch) | |
| tree | 29ede64472da8c7431bc4515d9f76b610a1a43c6 /platform-paper | |
| parent | 122af44500427cf61c979896149d1f08233a63d4 (diff) | |
| download | LunaticChat-f6f2ec77fa773250b2663491628f14e5baa92f5d.tar.gz LunaticChat-f6f2ec77fa773250b2663491628f14e5baa92f5d.tar.bz2 LunaticChat-f6f2ec77fa773250b2663491628f14e5baa92f5d.zip | |
feat: Add permission collector
Diffstat (limited to 'platform-paper')
| -rw-r--r-- | platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/common/PermissionCollector.kt | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/common/PermissionCollector.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/common/PermissionCollector.kt new file mode 100644 index 0000000..4edfe34 --- /dev/null +++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/common/PermissionCollector.kt @@ -0,0 +1,60 @@ +package dev.m1sk9.lunaticChat.paper.common + +import dev.m1sk9.lunaticChat.engine.exception.RequirePermissionException +import dev.m1sk9.lunaticChat.engine.permission.LunaticChatPermissionNode +import org.bukkit.entity.Player + +@DslMarker +annotation class PermissionDsl + +@PermissionDsl +class PermissionCollector { + internal val permissions = mutableListOf<LunaticChatPermissionNode>() + + operator fun LunaticChatPermissionNode.unaryPlus() { + permissions.add(this) + } +} + +fun LunaticChatPermissionNode.has(player: Player): Boolean = player.hasPermission(this.permissionNode) + +/** + * Checks if the player has at least one of the specified permissions. + * + * @param block A lambda with receiver to collect permissions. + * @return `true` if the player has at least one of the specified permissions. + */ +fun Player.hasAnyPermission(block: PermissionCollector.() -> Unit): Boolean = + PermissionCollector() + .apply(block) + .permissions + .any { it.has(this) } + +/** + * Checks if the player has all the specified permissions. + * + * @param block A lambda with receiver to collect permissions. + * @return `true` if the player has all the specified permissions. + */ +fun Player.hasAllPermission(block: PermissionCollector.() -> Unit): Boolean = + PermissionCollector() + .apply(block) + .permissions + .all { it.has(this) } + +/** + * Requires the player to have at least one of the specified permissions. + * + * @param block A lambda with receiver to collect permissions. + * @return Throws [RequirePermissionException] if the player lacks all specified permissions. + * @throws RequirePermissionException if the player lacks all specified permissions. + */ +fun Player.requirePermission(block: PermissionCollector.() -> Unit) { + val result = + PermissionCollector() + .apply(block) + .permissions + if (!result.any { it.has(this) }) { + throw RequirePermissionException(result) + } +} |
