Solved How to set permission nodes for commands in a seperate class?

Discussion in 'Spigot Plugin Development' started by ToonBasic, May 16, 2016.

  1. Hi, I am currently making a plugin and would like to know how would I go on about setting all my permissions for commands on a separate class file. I am trying to keep all my classes and code as neat and professional as possible. Thanks :)
  2. No matter the class, you can do if(player.hasPermission("yourperm"))
  3. Problem is, I don't know how to implement it in an entirely new and fresh class. Does it implement something? Does it extend something?
  4. Well since the hasPermission()# method takes a string as a parameter, you will obviously need to create a String variable for your permission.

    I would suggest creating a class called <yourPlugin> and begin writing your permissions into it.
  5. What are you trying to do? If you wish to check the perm again just use player.hasPermission("Same perm here") { }
  6. It's clear what he wants to do..
    He wants to have a separate class file that will hold all of his permissions for whatever his plugin is doing.
  7. Okay, so all my commands are located in one class named Commands, it's home to where store all my commands for my players and staff, but I'm trying to keep my code as clean and simple as possible, so I created a new class named "Permissions". What I'm trying to accomplish here is when a player/staff executed a command the permissions class will first check if they have the permission to do so, but if they don't it returns an error message.
  8. Put your permissions as public static final and you can access them from your commands class
    • Winner Winner x 1
  9. Here's a little hint on how I do my permissions...

    Code (Text):
                if(!p.hasPermission(CorePermissions.CORE_ADMIN_CMD_PERM)) //My CorePermissions class is where I store all my permissions.
                    //Custom message from config file.. Not really relevent.
                //Do stuff.
  10. That was my original idea, but not the answer I was looking for. I don't want my permissions class to be an command executing class. I want it to store all my permissions for the commands. I mean, I can just ditch the permissions idea and throw that into the commands class, but it won't look as clean.
  11. I'll try that soon, thanks.
  12. You do realise that @Ferdz and I are talking about the same thing right..?
  13. Oh, whoops. Didn't realize, sorry. I'll try this solution soon. Thanks!
    • Informative Informative x 1
  14. There's a lot of people in this forum that hate on static for some reason. You could also make your Permissions class a singleton to please their mighty fury, if you know what a singleton is :p
  15. The problem is that a lot of people hate on static without knowing the benefit of static. If used properly it is an extremely powerful tool!
    • Agree Agree x 1
  16. I know, I have no clue why so many of them hate on it. Of course having a static instance of your main plugin might be a bad idea, but most of the time it's extremely powerful and will boost your workflow
    • Like Like x 1
  17. And import static is also a very nice thing, if you have pretty clear names for them, that is xD

    Something like they'd do in C++, call it something like pCmdAdmin, and static import that.
  18. The best way to do this is using an enum class.
    Code (Text):
    public enum Permissions {
        PLUGIN_DEFAULT ("plugin.default"),
        PLUGIN_ADMIN ("plugin.admin"),
        PLUGIN_DONATOR_1 ("plugin.donator.1"),
        PLUGIN_DONATOR_2 ("plugin.donator.2");

        private String key;

        Permissions(String key) {
            this.key = key;

        public boolean checkPermission(Player p){
            return p.hasPermission(this.getKey());

        public String getKey() {
            return key;
    Using this, in your command class you would call
    Code (Text):
            if (Permissions.PLUGIN_DEFAULT.checkPermission(p)){
                // do stuff
    Read more here
  19. Class with perms
    Code (Text):

    public static Permission permname = new Permission("you.perm");
    Class using perms
    Code (Text):

    That is how i do it.
  20. Forgot to mark this as solved, thanks guys!