YTG123

Untitled

Jan 5th, 2021 (edited)
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.61 KB | None | 0 0
  1. /**
  2.  * A simple utility class to manage commands
  3.  * with permissions.
  4.  *
  5.  * @param literal the command name
  6.  * @param permNode the permission node required for the command
  7.  * @param permLevel the operator permission level required if node is not present
  8.  *
  9.  * @constructor Configures the class to use the correct settings.
  10.  *
  11.  * @author YTG1234
  12.  */
  13. abstract class PermedCommand(val literal: String, val permNode: String, val permLevel: Int) {
  14.     /**
  15.      * Takes a [LiteralArgumentBuilder], applies some modifications (example: Calling [then][com.mojang.brigadier.builder.ArgumentBuilder.then]), and
  16.      * returns a builder.
  17.      */
  18.     abstract val cmd: (LiteralArgumentBuilder<ServerCommandSource>) -> LiteralArgumentBuilder<ServerCommandSource>
  19.  
  20.     private fun register(dispatcher: CommandDispatcher<ServerCommandSource>) {
  21.         dispatcher.register(
  22.             cmd(
  23.                 CommandManager.literal(literal).requires {
  24.                     it.hasPermissionLevel(permLevel)
  25.                 }
  26.             )
  27.         )
  28.     }
  29.  
  30.     private fun registerWithPerms(dispatcher: CommandDispatcher<ServerCommandSource>) {
  31.         dispatcher.register(
  32.             cmd(
  33.                 CommandManager.literal(literal)
  34.                     .requires(Permissions.require(permNode, permLevel))
  35.             )
  36.         )
  37.     }
  38.  
  39.     companion object {
  40.         fun PermedCommand.registerCmd(dispatcher: CommandDispatcher<ServerCommandSource>) {
  41.             if (FabricLoader.getInstance().isModLoaded("fabric-permissions-api-v0")) registerWithPerms(dispatcher)
  42.             else register(dispatcher)
  43.         }
  44.     }
  45. }
Add Comment
Please, Sign In to add comment