Advertisement
oxguy3

Super-Easy Permissions Checking for Bukkit

Nov 8th, 2011
468
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.07 KB | None | 0 0
  1. import java.util.logging.Logger;
  2. import org.bukkit.command.CommandSender;
  3. import org.bukkit.entity.Player;
  4. import org.bukkit.plugin.Plugin;
  5. import org.bukkit.plugin.java.JavaPlugin;
  6. import com.nijiko.permissions.PermissionHandler;
  7. import com.nijikokun.bukkit.Permissions.Permissions;
  8.  
  9. /**
  10.  * A fake Bukkit plugin that demonstrates methods to make managing multiple
  11.  * permissions systems a lot easier. Supports PermissionsBukkit and Permissions
  12.  * (or anything that can mimic it).
  13.  *
  14.  * Javadocs of this: http://jd.haydencity.net/bukkit/supereasyperms.html
  15.  *
  16.  * This code is released under Creative Commons Attribution 3.0 Unported
  17.  * (CC BY 3.0). You can view the CC BY 3.0 deed at
  18.  * http://creativecommons.org/licenses/by/3.0/
  19.  *
  20.  * @author oxguy3
  21.  */
  22. public class ExamplePlugin extends JavaPlugin {
  23.  
  24.     Logger log = Logger.getLogger("Minecraft");
  25.     public static PermissionHandler permissionHandler;
  26.     public static boolean useperms = false;
  27.    
  28.  
  29.     public void onDisable() {}
  30.    
  31.     public void onEnable() {
  32.         setupPermissions();
  33.     }
  34.    
  35.    
  36.     /**
  37.      * Figures out what permissions system the plugin should use.
  38.      *
  39.      * Checks if Permissions (or a mimic) is running; if it isn't, PermissionsBukkit
  40.      * will be used instead. The boolean useperms reflects the result of this method:
  41.      * if it's true, Permissions will be used; if it's false, PermissionsBukkit will
  42.      * be used. This method should only be called in onEnable().
  43.      *
  44.      * Based on code at https://github.com/TheYeti/Permissions/wiki/API-Reference
  45.      */
  46.     private void setupPermissions() {
  47.         if (permissionHandler != null) {
  48.             return;
  49.         }
  50.  
  51.         Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
  52.  
  53.         if (permissionsPlugin == null) {
  54.             log.info("ExamplePlugin: Permission system not detected, using Bukkit permissions");
  55.             useperms=false;
  56.             return;
  57.         }
  58.  
  59.         useperms=true;
  60.         permissionHandler = ((Permissions) permissionsPlugin).getHandler();
  61.         log.info("ExamplePlugin: Found and will use plugin "+((Permissions)permissionsPlugin).getDescription().getFullName());
  62.     }
  63.    
  64.    
  65.     /**
  66.      * Checks if a player has a permission node.
  67.      *
  68.      * This method checks to see if a CommandSender has a given permission node. It
  69.      * supports PermissionsBukkit and Permissions (as well as anything else that can
  70.      * mimic Permissions). If the CommandSender is an op or is not a Player
  71.      * (meaning it's probably a ConsoleCommandSender), it always returns true.
  72.      *
  73.      * @param sender the CommandSender being checked
  74.      *
  75.      * @param perm the dot-based permission node (i.e. "Plugin.subgroup.node")
  76.      *
  77.      * @return whether or not the sender has the given permission
  78.      */
  79.     public boolean hasPerm(CommandSender sender, String perm) {
  80.         if (sender.isOp()) {
  81.             return true;
  82.         } else if (sender instanceof Player) {
  83.             Player player = (Player)sender;
  84.             if (useperms) {
  85.                 if (!permissionHandler.has(player, perm)) {
  86.                     return false;
  87.                 } else {
  88.                     return true;
  89.                 }
  90.             } else if (!sender.hasPermission(perm)) {
  91.                 return false;
  92.             }
  93.             return true;
  94.         } else {
  95.             return true;
  96.         }
  97.     }
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement