Solved Permissions and Enums

Discussion in 'Spigot Plugin Development' started by Igasi, Apr 30, 2017.

  1. So yeah. I am still working on a server and now I'm working on ranks. I want them to go by enums just because.
    Code (Text):

    public enum Rank {DEFAULT("sc.default"), WARRIOR("sc.warrior"), CHALLENGER("sc.challenger"), CHAMPION("sc.champion");
     
        public Permission perm;
     
        Rank(Permission l){
            perm = l;
        }
        }
     
    Now, here is where I'm stuck.

    A permission is a string, right? If I do
    player.getPermission("This is a string"){
    //blah
    }
    it will work. However, here it doesn't. It's telling me that there is an error on sc.default and every other string because a permission is not a string. Are there any ways I can fix this?

    Also, I checked the javadocs and there is literally nothing on this.
     
  2. use player.hasPermission instead of player.getPermission
     
  3. Naa that was an example. I didnt even test it in an IDE. The problem is that the enums can have a permission as a peramter because a permission is a string. I'm asking if there is a way around it
     
  4. You'd need to get the parameter of the enum in order to use it in things like Player#hasPermission
     
  5. What do you mean? Like this?
    public enum Ranks{
    DEFAULT( THIS );
    }
     
  6. Make a getter for the permission in the enum, and use that to actually get the permission you're associating it with
     
  7. Aight I'll try that
     
  8. Strings aren't Permission Objects.

    Try it like that:

    Code (Text):
    public enum Rank {DEFAULT("sc.default"), WARRIOR("sc.warrior"), CHALLENGER("sc.challenger"), CHAMPION("sc.champion");

        private String perm;

        Rank(String l){
            perm = l;
        }

       public String getPermission (){
           return perm;
       }
        }
     
    • Like Like x 1
  9. I think you can override the toString method. Other than that you can use constants "static final" variables
     
    • Like Like x 1
  10. Already go it but thanks for trying to help
    Thanks! It helped quite a bit
     
  11. Why don't you use constants instead?

    Code (Text):
    public final static Permission CMDPERM1 = new Permission("perm.cmd1);
    public final static Permission CMDPERM2 = new Permission("perm.cmd2);
    public final static Permission CMDPERM3 = new Permission("perm.cmd3);
     
  12. ;)