From 427a5b1c83c1c6c6444ec2e26f2b37aa8a9fb00c Mon Sep 17 00:00:00 2001 From: andyksaw Date: Thu, 28 Nov 2024 20:48:31 +0900 Subject: [PATCH] Add permission nodes --- .../pcbridge/paper/PermissionNode.kt | 7 +++++++ .../brigadier/ArgumentBuilderExtensions.kt | 17 +++++++++++++---- .../features/builds/commands/BuildCommand.kt | 3 +++ .../features/builds/commands/BuildsCommand.kt | 3 +++ .../commands/builds/BuildCreateCommand.kt | 3 +++ .../commands/builds/BuildDeleteCommand.kt | 3 +++ .../builds/commands/builds/BuildEditCommand.kt | 3 +++ .../builds/commands/builds/BuildListCommand.kt | 3 +++ .../builds/commands/builds/BuildMoveCommand.kt | 3 +++ .../builds/commands/builds/BuildSetCommand.kt | 3 +++ .../commands/builds/BuildUnvoteCommand.kt | 3 +++ .../builds/commands/builds/BuildVoteCommand.kt | 3 +++ 12 files changed, 50 insertions(+), 4 deletions(-) create mode 100644 pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/PermissionNode.kt diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/PermissionNode.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/PermissionNode.kt new file mode 100644 index 00000000..d6dcac24 --- /dev/null +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/PermissionNode.kt @@ -0,0 +1,7 @@ +package com.projectcitybuild.pcbridge.paper + +enum class PermissionNode(val node: String) { + BUILD_TELEPORT("pcbridge.build.teleport"), + BUILD_MANAGE("pcbridge.build.manage"), + BUILD_VOTE("pcbridge.build.vote"), +} \ No newline at end of file diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/core/support/brigadier/ArgumentBuilderExtensions.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/core/support/brigadier/ArgumentBuilderExtensions.kt index 78a363e4..90a38330 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/core/support/brigadier/ArgumentBuilderExtensions.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/core/support/brigadier/ArgumentBuilderExtensions.kt @@ -2,13 +2,11 @@ package com.projectcitybuild.pcbridge.paper.core.support.brigadier import com.github.shynixn.mccoroutine.bukkit.launch import com.mojang.brigadier.Command -import com.mojang.brigadier.builder.ArgumentBuilder import com.mojang.brigadier.builder.LiteralArgumentBuilder import com.mojang.brigadier.builder.RequiredArgumentBuilder import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder -import com.mojang.brigadier.tree.CommandNode -import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import io.papermc.paper.command.brigadier.CommandSourceStack import org.bukkit.plugin.Plugin @@ -55,4 +53,15 @@ fun LiteralArgumentBuilder.then( command: BrigadierCommand, ): LiteralArgumentBuilder { return then(command.buildLiteral()) -} \ No newline at end of file +} + + +@Suppress("UnstableApiUsage") +fun LiteralArgumentBuilder.requiresPermission( + permission: PermissionNode, +): LiteralArgumentBuilder { + return requires { context -> + context.sender.hasPermission(permission.node) + } +} + diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildCommand.kt index d296963e..b5a97158 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildCommand.kt @@ -4,10 +4,12 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.features.warps.events.PlayerPreWarpEvent import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -30,6 +32,7 @@ class BuildCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("build") + .requiresPermission(PermissionNode.BUILD_TELEPORT) .then( Commands.argument("name", StringArgumentType.greedyString()) .suggestsSuspending(plugin, ::suggestBuild) diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildsCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildsCommand.kt index 07f0f6a6..1df5d3ee 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildsCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/BuildsCommand.kt @@ -1,12 +1,14 @@ package com.projectcitybuild.pcbridge.paper.features.builds.commands import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildCreateCommand import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildDeleteCommand import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildListCommand import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildMoveCommand import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildVoteCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.then import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildEditCommand import com.projectcitybuild.pcbridge.paper.features.builds.commands.builds.BuildSetCommand @@ -35,6 +37,7 @@ class BuildsCommand( .then(command = buildSetCommand) .then(command = buildUnvoteCommand) .then(command = buildVoteCommand) + .requiresPermission(PermissionNode.BUILD_TELEPORT) .executes(buildListCommand.buildLiteral().command) .build() } diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildCreateCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildCreateCommand.kt index fe05a5bc..a49870ff 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildCreateCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildCreateCommand.kt @@ -3,9 +3,11 @@ package com.projectcitybuild.pcbridge.paper.features.builds.commands.builds import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack import io.papermc.paper.command.brigadier.Commands @@ -20,6 +22,7 @@ class BuildCreateCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("create") + .requiresPermission(PermissionNode.BUILD_MANAGE) .then( Commands.argument("name", StringArgumentType.greedyString()) .executesSuspending(plugin, ::execute) diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildDeleteCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildDeleteCommand.kt index 8548dfb9..a54d821d 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildDeleteCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildDeleteCommand.kt @@ -4,9 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -22,6 +24,7 @@ class BuildDeleteCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("delete") + .requiresPermission(PermissionNode.BUILD_MANAGE) .then( Commands.argument("name", StringArgumentType.greedyString()) .suggestsSuspending(plugin, ::suggestBuild) diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildEditCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildEditCommand.kt index fb30a505..fb0126cc 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildEditCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildEditCommand.kt @@ -4,9 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -22,6 +24,7 @@ class BuildEditCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("edit") + .requiresPermission(PermissionNode.BUILD_MANAGE) .then( Commands.argument("name", StringArgumentType.greedyString()) .suggestsSuspending(plugin, ::suggestBuild) diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildListCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildListCommand.kt index 4b21ade0..35ed5b1c 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildListCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildListCommand.kt @@ -2,9 +2,11 @@ package com.projectcitybuild.pcbridge.paper.features.builds.commands.builds import com.mojang.brigadier.arguments.IntegerArgumentType import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import io.papermc.paper.command.brigadier.CommandSourceStack import io.papermc.paper.command.brigadier.Commands import net.kyori.adventure.text.Component @@ -22,6 +24,7 @@ class BuildListCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("list") + .requiresPermission(PermissionNode.BUILD_TELEPORT) .executesSuspending(plugin) { context -> execute( sender = context.source.sender, diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildMoveCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildMoveCommand.kt index 2d32bfce..97417c9c 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildMoveCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildMoveCommand.kt @@ -4,9 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -22,6 +24,7 @@ class BuildMoveCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("move") + .requiresPermission(PermissionNode.BUILD_MANAGE) .then( Commands.argument("name", StringArgumentType.greedyString()) .suggestsSuspending(plugin, ::suggestBuild) diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildSetCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildSetCommand.kt index 68013e3b..14579829 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildSetCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildSetCommand.kt @@ -5,9 +5,11 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -23,6 +25,7 @@ class BuildSetCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("set") + .requiresPermission(PermissionNode.BUILD_MANAGE) .then( Commands.argument("id", IntegerArgumentType.integer()) .then( diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildUnvoteCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildUnvoteCommand.kt index 9e0ae3d4..2a10b10e 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildUnvoteCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildUnvoteCommand.kt @@ -4,9 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -22,6 +24,7 @@ class BuildUnvoteCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("unvote") + .requiresPermission(PermissionNode.BUILD_VOTE) .then( Commands.argument("name", StringArgumentType.greedyString()) .suggestsSuspending(plugin, ::suggestBuild) diff --git a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildVoteCommand.kt b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildVoteCommand.kt index 7eea57dd..4f021190 100644 --- a/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildVoteCommand.kt +++ b/pcbridge-paper/src/main/kotlin/com/projectcitybuild/pcbridge/paper/features/builds/commands/builds/BuildVoteCommand.kt @@ -4,9 +4,11 @@ import com.mojang.brigadier.arguments.StringArgumentType import com.mojang.brigadier.context.CommandContext import com.mojang.brigadier.suggestion.SuggestionsBuilder import com.mojang.brigadier.tree.LiteralCommandNode +import com.projectcitybuild.pcbridge.paper.PermissionNode import com.projectcitybuild.pcbridge.paper.features.builds.repositories.BuildRepository import com.projectcitybuild.pcbridge.paper.core.support.brigadier.BrigadierCommand import com.projectcitybuild.pcbridge.paper.core.support.brigadier.executesSuspending +import com.projectcitybuild.pcbridge.paper.core.support.brigadier.requiresPermission import com.projectcitybuild.pcbridge.paper.core.support.brigadier.suggestsSuspending import com.projectcitybuild.pcbridge.paper.core.support.brigadier.traceCommand import io.papermc.paper.command.brigadier.CommandSourceStack @@ -22,6 +24,7 @@ class BuildVoteCommand( ): BrigadierCommand { override fun buildLiteral(): LiteralCommandNode { return Commands.literal("vote") + .requiresPermission(PermissionNode.BUILD_VOTE) .then( Commands.argument("name", StringArgumentType.greedyString()) .suggestsSuspending(plugin, ::suggestBuild)