Você está na página 1de 10

Bukkit working with Permissions

by Urs P. Stettler
Image Source

Bukkit's Permission API


Add permission handling to your plugin in two steps: Declare permissions in plugin.yml
Including default permissions, if no permission plugin is present

Query permissions on Player Object


hasPermission(name :: String) :: boolean

Word of Warning
Keep in Mind... Bukkit defines a method to query a player's permissions It does not implement a way to set player's permissions Therefore, Bukkit/CraftBukkit needs a plugin to load/persist and set permissions!
E.g., PermissionsBukkit

Define Permission Nodes


plugin.yml example
# plugin.yml # ... other entries ... permissions: # global permission node tele.*: description: Player can use all aspects of the plugin children: tele.home.*: true # group permission node tele.home.*: description: Player can use [set]home command children: tele.home.use: true tele.home.set: true # a single permission node tele.home.use: description: Player can teleport to her home location default: true # other nodes...

default possible values: true, false, op

Permission Handling
Tip: Create an interface containing the names of the permission nodes.
/** Interface defining permission node names used in plugin.yml. */ public interface PermissionNodes { /** Permission node allowing players to set their home location. */ String NODE_SET_HOME = "tele.home.set" ; /** Permission node allowing players to teleport to their home. */ String NODE_USE_HOME = "tele.home.use" ; }

Permissible Interface
Bukkit offers the Permissible interface (package org.bukkit.permissions) to access permissions. Player implements Permissible Notable methods:
/** Gets the value of the specified permission, if set. */ boolean hasPermission(String name); /** Checks if this object is a server operator. */ boolean isOp();

Permission Check
Accessing permissions on a Player object:
// assuming the current player is given, e.g., from a command call boolean onCommand (CommandSender sender, Command command, String label, String[] args) { Player player; if (sender instanceof Player) { player = (Player)sender; } else { return false; } if ("home".equals(label)){ // player issued the "/home" command if (player.hasPermission(PermissionNodes. NODE_HOME_USE )) { // player has permission to use the command } else { // warn player that she cannot use the command } return true ; } return false ; }

Points to consider
Who uses your plugin?
players console (issuing commands)

How is their state?


survival mode creative mode adventure mode op/none-op

Conclusion
Permissions allow server admins to group players Admins can manage plugin aspects at the granularity they see fit This means
You do not have to implement player groups You do not have to consider every single situation in which your plugin may be used You have to document permissions / permission nodes clearly!

References
Minecraft (www.minecraft.net) A certain sandbox game Mojang (www.mojang.com) The company @notch founded that continues to develop and improve Minecraft Bukkit (www.bukkit.org) An extensible server implementation and API for Minecraft

Você também pode gostar