oxguy3 icon

Super-Easy Permissions Checking for Bukkit

oxguy3 | PRO | 11/09/11 01:21:13 AM UTC | 0 ⭐ | 657 👁️ | Never ⏰ | []
Java |

3.07 KB

|

None

|

0 👍

/

0 👎

import java.util.logging.Logger;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.java.JavaPlugin;
import com.nijiko.permissions.PermissionHandler;
import com.nijikokun.bukkit.Permissions.Permissions;
 
/**
 * A fake Bukkit plugin that demonstrates methods to make managing multiple
 * permissions systems a lot easier. Supports PermissionsBukkit and Permissions
 * (or anything that can mimic it).
 * 
 * Javadocs of this: http://jd.haydencity.net/bukkit/supereasyperms.html
 * 
 * This code is released under Creative Commons Attribution 3.0 Unported
 * (CC BY 3.0). You can view the CC BY 3.0 deed at
 * http://creativecommons.org/licenses/by/3.0/
 * 
 * @author oxguy3
 */
public class ExamplePlugin extends JavaPlugin {
 
    Logger log = Logger.getLogger("Minecraft");
    public static PermissionHandler permissionHandler;
    public static boolean useperms = false;
    
 
    public void onDisable() {}
    
    public void onEnable() {
        setupPermissions();
    }
    
    
    /**
     * Figures out what permissions system the plugin should use.
     * 
     * Checks if Permissions (or a mimic) is running; if it isn't, PermissionsBukkit
     * will be used instead. The boolean useperms reflects the result of this method:
     * if it's true, Permissions will be used; if it's false, PermissionsBukkit will
     * be used. This method should only be called in onEnable().
     * 
     * Based on code at https://github.com/TheYeti/Permissions/wiki/API-Reference
     */
    private void setupPermissions() {
        if (permissionHandler != null) {
            return;
        }
 
        Plugin permissionsPlugin = this.getServer().getPluginManager().getPlugin("Permissions");
 
        if (permissionsPlugin == null) {
            log.info("ExamplePlugin: Permission system not detected, using Bukkit permissions");
            useperms=false;
            return;
        }
 
        useperms=true;
        permissionHandler = ((Permissions) permissionsPlugin).getHandler();
        log.info("ExamplePlugin: Found and will use plugin "+((Permissions)permissionsPlugin).getDescription().getFullName());
    }
    
    
    /**
     * Checks if a player has a permission node.
     * 
     * This method checks to see if a CommandSender has a given permission node. It
     * supports PermissionsBukkit and Permissions (as well as anything else that can
     * mimic Permissions). If the CommandSender is an op or is not a Player
     * (meaning it's probably a ConsoleCommandSender), it always returns true.
     * 
     * @param sender the CommandSender being checked
     * 
     * @param perm the dot-based permission node (i.e. "Plugin.subgroup.node")
     * 
     * @return whether or not the sender has the given permission
     */
    public boolean hasPerm(CommandSender sender, String perm) {
        if (sender.isOp()) {
            return true;
        } else if (sender instanceof Player) {
            Player player = (Player)sender;
            if (useperms) {
                if (!permissionHandler.has(player, perm)) {
                    return false;
                } else {
                    return true;
                }
            } else if (!sender.hasPermission(perm)) {
                return false;
            }
            return true;
        } else {
            return true;
        }
    }
}

Comments