Skip to content

Commit

Permalink
Add permission nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
andyksaw committed Nov 28, 2024
1 parent c39f112 commit 427a5b1
Show file tree
Hide file tree
Showing 12 changed files with 50 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -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"),
}
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -55,4 +53,15 @@ fun LiteralArgumentBuilder<CommandSourceStack>.then(
command: BrigadierCommand,
): LiteralArgumentBuilder<CommandSourceStack> {
return then(command.buildLiteral())
}
}


@Suppress("UnstableApiUsage")
fun LiteralArgumentBuilder<CommandSourceStack>.requiresPermission(
permission: PermissionNode,
): LiteralArgumentBuilder<CommandSourceStack> {
return requires { context ->
context.sender.hasPermission(permission.node)
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -30,6 +32,7 @@ class BuildCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("build")
.requiresPermission(PermissionNode.BUILD_TELEPORT)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.suggestsSuspending(plugin, ::suggestBuild)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -35,6 +37,7 @@ class BuildsCommand(
.then(command = buildSetCommand)
.then(command = buildUnvoteCommand)
.then(command = buildVoteCommand)
.requiresPermission(PermissionNode.BUILD_TELEPORT)
.executes(buildListCommand.buildLiteral().command)
.build()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +22,7 @@ class BuildCreateCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("create")
.requiresPermission(PermissionNode.BUILD_MANAGE)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.executesSuspending(plugin, ::execute)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class BuildDeleteCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("delete")
.requiresPermission(PermissionNode.BUILD_MANAGE)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.suggestsSuspending(plugin, ::suggestBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class BuildEditCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("edit")
.requiresPermission(PermissionNode.BUILD_MANAGE)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.suggestsSuspending(plugin, ::suggestBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class BuildListCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("list")
.requiresPermission(PermissionNode.BUILD_TELEPORT)
.executesSuspending(plugin) { context ->
execute(
sender = context.source.sender,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class BuildMoveCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("move")
.requiresPermission(PermissionNode.BUILD_MANAGE)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.suggestsSuspending(plugin, ::suggestBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -23,6 +25,7 @@ class BuildSetCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("set")
.requiresPermission(PermissionNode.BUILD_MANAGE)
.then(
Commands.argument("id", IntegerArgumentType.integer())
.then(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class BuildUnvoteCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("unvote")
.requiresPermission(PermissionNode.BUILD_VOTE)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.suggestsSuspending(plugin, ::suggestBuild)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,6 +24,7 @@ class BuildVoteCommand(
): BrigadierCommand {
override fun buildLiteral(): LiteralCommandNode<CommandSourceStack> {
return Commands.literal("vote")
.requiresPermission(PermissionNode.BUILD_VOTE)
.then(
Commands.argument("name", StringArgumentType.greedyString())
.suggestsSuspending(plugin, ::suggestBuild)
Expand Down

0 comments on commit 427a5b1

Please sign in to comment.