How you add permission support to your plugin

Discussion in 'Plugin Development' started by Tim Visee, Apr 10, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Tim Visee

    Ok, How to add permission support to your project/plugin,

    First of all, you must add the Permissions.jar plugin (for craftbukkit) to your project, just like you do the same as the bukkit's API jar.

    Go to your main file and add these two import things
    Code:
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    Add the following three things to your main file too
    Code:
    public PermissionHandler Permissions;
    private boolean UsePermissions;
    Code:
    public void onEnable() {
       setupPermissions();
    }
    Code:
    private void setupPermissions() {
        Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
        if (this.Permissions == null) {
            if (test != null) {
                UsePermissions = true;
                this.Permissions = ((Permissions) test).getHandler();
                System.out.println("Permissions enabled.");
            } else {
                log.info("Permission system not detected, defaulting to OP");
                UsePermissions = false;
            }
        }
    }
    For each command you want to implement add one of this on the main file too
    Code:
    public boolean casUseCmdList(Player player) {
        if (UsePermissions) {
            return this.Permissions.has(player, "cmd.list");
        }
        return player.isOp();
    }
    On the command per-say add this if:
    Code:
    if (casCmdlist(player)) {
    } else {
        player.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    }
    Hope this helps!

    Credits: @Sammy
     
    Sammy likes this.
  2. Offline

    eisental

    You might want to give credit to @Sammy for his code ;)
     
  3. Offline

    Sammy

    @eisental no need ^^
    I have to turn one of my plugins public, so I can get one of those sweet "Plugin Developer" tags ihihih
     
  4. Offline

    Tim Visee

    Yes, that's right! I dit it!

    But I get the code from you...
     
  5. Offline

    eisental

    Sorry, I didn't see it...
     
  6. Offline

    Tim Visee

    No problem :)
     
  7. Offline

    Sammy

    I was going to make a tutorial myself no need now ^^
    cheers
     
  8. Offline

    captainawesome7

    For me it says player can not be resolved to a variable...
     
  9. Offline

    captainawesome7

    Seriously this doesn't work..
     
  10. Offline

    Sammy

    Show me your code, or we can't help you !
     
  11. Offline

    Tim Visee

    Yes! Send your code...
     
  12. Offline

    captainawesome7

    This is my main class file:
    Code:
    package me.captain.Annoyer;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    import org.bukkit.plugin.Plugin;
    
    public class CAM extends JavaPlugin {
    
        private boolean UsePermissions;
        public static PermissionHandler Permissions;
        private void setupPermissions() {
            Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
            if (this.Permissions == null) {
                if (test != null) {
                    UsePermissions = true;
                    this.Permissions = ((Permissions) test).getHandler();
                    System.out.println("[Annoyer] Permissions system detected!");
                } else {
                    log.info("Permission system not detected, defaulting to OP");
                    UsePermissions = false;
                }
            }
        }
        public boolean enabled(Player player){
            return this.camUsers.containsKey(player);
        }
    
        public boolean casUseCmdList(Player p) {
            if (UsePermissions) {
                return this.Permissions.has(p, "annoy.self");
            }
            return p.isOp();
        }
    
        private static final Logger log = Logger.getLogger("Minecraft");
    
        private final CAMBlockListener blockListener = new CAMBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> camUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
    
            PluginManager pm = getServer().getPluginManager();
            setupPermissions();
            pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
                    Event.Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener,
                    Event.Priority.Normal, this);
            log.info("[Annoyer] Annoyer has been enabled!");
        }
        ArrayList<String> list = new ArrayList<String>();
    
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("annoy")){
                addTD((Player) sender);
            }
            if(commandLabel.equalsIgnoreCase("unannoy")){
                removeTD((Player) sender);
                    return true;
            }
                return false;
        }
    
        public void onDisable() {
            log.info("[Annoyer] Annoyer has been disabled!");
        }
        public void removeTD(Player player){
                this.camUsers.remove(player);
                player.sendMessage(ChatColor.GREEN + "You are no longer being annoyed!");
        }
        public void addTD(Player player){
                this.camUsers.put(player, null);
                player.sendMessage(ChatColor.RED + "You are now being annoyed!");
        }
    
    }
    
    How do I add the per command thingy for /annoy?
    *EDIT* I had this for the command:
    Code:
    if (casCmdlist(ply)) {
    } else {
        player.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    }
    But it was saying player cannot be resolved, so where do I put this in my file?
     
  13. Offline

    Tim Visee

    You must do this, change ply to tour player varialbe like this;
    Code:
    if (casCmdlist(player)) {
    } else {
        player.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    }
    if that doesn't work try this; (with (Player)...)
    Code:
    if (casCmdlist((Player) player) {
    } else {
        player.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    }
    Or did you have another problem?
     
  14. Offline

    captainawesome7

    I already tried the first suggestion. As for the second, I get this:
    Code:
    Multiple markers at this line
        - Syntax error on token ")", ) expected after this token
        - The method casCmdlist(Player) is undefined for the type
         CAM
        - player cannot be resolved to a variable
    Can you insert the thing yourself so I know exactly where to put it? Imma severe Java n00b

    Alright, I noticed that you didn't capitalize List and forgot Use, so I changed it to these:
    Code:
        public boolean casUseCmdList(Player ply) {
            if (UsePermissions) {
                return this.Permissions.has(ply, "annoy.self");
            }
            return ply.isOp();
        }
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("annoy")){
                if (casUseCmdList(ply)) {
                } else {
                    ply.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                }
                addTD((Player) sender);
            }
            if(commandLabel.equalsIgnoreCase("unannoy")){
                removeTD((Player) sender);
                    return true;
            }
                return false;
        }
    And it says:
    ply cannot be resolved to a
    variable

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  15. Offline

    Tim Visee

    Did it works right now?
     
  16. Offline

    captainawesome7

    No

    An update of the entire file: With errors show in red, and explanations below:
    Code:
    package me.captain.Annoyer;
    
    import java.util.ArrayList;
    
    import java.util.HashMap;
    
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    
    import org.bukkit.command.Command;
    
    import org.bukkit.command.CommandSender;
    
    import org.bukkit.block.Block;
    
    import org.bukkit.entity.Player;
    
    import org.bukkit.event.Event;
    
    import org.bukkit.plugin.PluginManager;
    
    import org.bukkit.plugin.java.JavaPlugin;
    
    import com.nijiko.permissions.PermissionHandler;
    
    import com.nijikokun.bukkit.Permissions.Permissions;
    
    import org.bukkit.plugin.Plugin;
    
    public class CAM extends JavaPlugin {
    
    private boolean UsePermissions;
    
    public static PermissionHandler Permissions;
    
    private void setupPermissions() {
    
    Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
    
    if (this.Permissions == null) {
    
    if (test != null) {
    
    UsePermissions = true;
    
    this.Permissions = ((Permissions) test).getHandler();
    
    System.out.println("[Annoyer] Permissions system detected!");
    
    } else {
    
    log.info("Permission system not detected, defaulting to OP");
    
    UsePermissions = false;
    
    }
    
    }
    
    }
    
    public boolean enabled(Player player){
    
    return this.camUsers.containsKey(player);
    
    }
    
    public boolean canUseAnnoySelf(Player p) {
    
    if (UsePermissions) {
    
    return this.Permissions.has(p, "annoy.self");
    
    }
    
    return p.isOp();
    
    }
    
    private static final Logger log = Logger.getLogger("Minecraft");
    
    private final CAMBlockListener blockListener = new CAMBlockListener(this);
    
    public final HashMap<Player, ArrayList<Block>> camUsers = new HashMap<Player, ArrayList<Block>>();
    
    public void onEnable() {
    
    PluginManager pm = getServer().getPluginManager();
    
    setupPermissions();
    
    pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
    
    Event.Priority.Normal, this);
    
    pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener,
    
    Event.Priority.Normal, this);
    
    log.info("[Annoyer] Annoyer has been enabled!");
    
    }
    
    ArrayList<String> list = new ArrayList<String>();
    
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    
    if(commandLabel.equalsIgnoreCase("annoy")){
    
    if (canUseAnnoySelf(p)) { // Error Message Is = P cannot be resolved to a variable
    
    
    } else {
    
    p.sendMessage(ChatColor.RED + "You don't have permisson to use that command"); // Error Message Is = P cannot be resolved to a variable
    
    }
    
    addTD((Player) sender);
    
    }
    
    if(commandLabel.equalsIgnoreCase("unannoy")){
    
    removeTD((Player) sender);
    
    return true;
    
    }
    
    return false;
    
    }
    
    public void onDisable() {
    
    log.info("[Annoyer] Annoyer has been disabled!");
    
    }
    
    public void removeTD(Player player){
    
    this.camUsers.remove(player);
    
    player.sendMessage(ChatColor.GREEN + "You are no longer being annoyed!");
    
    }
    
    public void addTD(Player player){
    
    this.camUsers.put(player, null);
    
    player.sendMessage(ChatColor.RED + "You are now being annoyed!");
    
    }
    
    }
    
    
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  17. Offline

    Tim Visee

    In your onCommand sub you must change if (casUseCmdList(ply)) { to if (casUseCmdList((Player) sender) {

    I show my suggestion for that first red error in my previous message,

    You must change
    Code:
    p.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    to
    Code:
    Player p = (Player) sender;
    p.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    or if that doesn't work to
    Code:
    Player p = sender;
    p.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  18. Offline

    RazorFlint

    Thanks needed This XD
     
  19. Offline

    captainawesome7

    testing now

    The final working product:
    Code:
    package me.captain.Annoyer;
    
    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.logging.Logger;
    
    import org.bukkit.ChatColor;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandSender;
    import org.bukkit.block.Block;
    import org.bukkit.entity.Player;
    import org.bukkit.event.Event;
    import org.bukkit.plugin.PluginManager;
    import org.bukkit.plugin.java.JavaPlugin;
    import com.nijiko.permissions.PermissionHandler;
    import com.nijikokun.bukkit.Permissions.Permissions;
    import org.bukkit.plugin.Plugin;
      
    public class CAM extends JavaPlugin {
    
        private boolean UsePermissions;
        public static PermissionHandler Permissions;
        private void setupPermissions() {
            Plugin test = this.getServer().getPluginManager().getPlugin("Permissions");
            if (this.Permissions == null) {
                if (test != null) {
                    UsePermissions = true;
                    this.Permissions = ((Permissions) test).getHandler();
                    System.out.println("[Annoyer] Permissions system detected!");
                } else {
                    log.info("Permission system not detected, defaulting to OP");
                    UsePermissions = false;
                }
            }
        }
        public boolean enabled(Player player){
            return this.camUsers.containsKey(player);
        }
    
        public boolean canUseAnnoySelf(Player p) {
            if (UsePermissions) {
                return this.Permissions.has(p, "annoy.self");
            }
            return p.isOp();
        }
    
        private static final Logger log = Logger.getLogger("Minecraft");
    
        private final CAMBlockListener blockListener = new CAMBlockListener(this);
        public final HashMap<Player, ArrayList<Block>> camUsers = new HashMap<Player, ArrayList<Block>>();
        public void onEnable() {
    
            PluginManager pm = getServer().getPluginManager();
            setupPermissions();
            pm.registerEvent(Event.Type.BLOCK_BREAK, blockListener,
                    Event.Priority.Normal, this);
            pm.registerEvent(Event.Type.BLOCK_PLACE, blockListener,
                    Event.Priority.Normal, this);
            log.info("[Annoyer] Annoyer has been enabled!");
        }
        ArrayList<String> list = new ArrayList<String>();
     
        public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if(commandLabel.equalsIgnoreCase("annoy")){
                if (canUseAnnoySelf((Player) sender)) {
                    addTD((Player) sender);
                } else {
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                }
            }
            if(commandLabel.equalsIgnoreCase("unannoy")){
                if (canUseAnnoySelf((Player) sender)) {
                    removeTD((Player) sender);
                } else {
                    Player p = (Player) sender;
                    p.sendMessage(ChatColor.RED + "You don't have permisson to use that command");
                }
                    return true;
            }
                return false;
        }
    
        public void onDisable() {
            log.info("[Annoyer] Annoyer has been disabled!");
        }
        public void removeTD(Player player){
                this.camUsers.remove(player);
                player.sendMessage(ChatColor.GREEN + "You are no longer being annoyed!");
        }
        public void addTD(Player player){
                this.camUsers.put(player, null);
                player.sendMessage(ChatColor.RED + "You are now being annoyed!");
        }
    
    }
    That is not the same as you have in the original post.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 13, 2016
  20. Offline

    Tim Visee

    It works! Great!
     
  21. Offline

    captainawesome7

    @Tim Visee
    Yes it does, and thank you for that, but why doesn't the original post have the correct stuff?
     
  22. Offline

    Tim Visee

    That works, I tested it again 1 hour ago, I don't know what's wrong with yours...
     
  23. Offline

    captainawesome7

    Weird. Well thanks for the help!
     
  24. Offline

    Tim Visee

    No problem! :)
     
Thread Status:
Not open for further replies.

Share This Page