Custom permissions???

Discussion in 'Plugin Development' started by NewDevOnTheBlock, Apr 19, 2015.

Thread Status:
Not open for further replies.
  1. Hello,

    So basically what I am trying to do is make it so the user can input a permission node like... 'test.perm.1', 'test.perm.2', 'test.perm.3'... etc..

    Then I will check if(player.hasPermission("test.perm." + (String) ) {}

    The problem that I am having is thinking of a way to get that (String) part above. I use the plugin.yml to store my permissions currently, and I've heard of other ways to do permissions in a separate class file, but I'm not 100% how to do that. Does anyone have a suggestion on how to do this?

    Thank you for any help!
     
  2. Offline

    mythbusterma

    @NewDevOnTheBlock

    It's string concatenation, if "(string)" was a String, then the code you have will work. I don't understand what your question is.
     
    NewDevOnTheBlock likes this.
  3. This is going to sound like I'm high, but I'm not, but it makes complete sense in one part of my mind, but on the other part it's like, "There has to be something more to this!"

    *Edit @mythbusterma So I understand what you're saying, but I guess I did not describe it enough in the original post.

    Lets say I give a player the permission node "test.perm.50", and want to get that "50" as a String. But I allow for me to change that "50" to anything in the permissions file, and in turn that will change the output. I'm thinking that I could use a substring somewhere in this, but am having more trouble seeing if the player has a permission with any number given after 'perm.'

    How can I give a player a custom permission, and then take that so-called third argument of the permission node and manipulate it?
     
    Last edited: Apr 19, 2015
  4. Offline

    nj2miami

    I am not totally convinced I understand what you want, but let's say you have a string "test.perm.X" where X changes you could always just replace the "test.perm." with "" / remove the common part of your string which remains constant which will leave you with what is left, the X.
     
  5. Offline

    bohafr

    You can write it into onCommand with sender.hasPermission("perm"); but to do this you have to remove permission in plugin yml (if you have this:
    cmd1:
    permission: test)
    Because this in plugin.yml will disallow all users who do not have this permission.
    You can also set permission only when args0 = something

    You can do it in onCommand, check if arg0 == something and then check if sender has permission, when no send him message and return.

    Example code:
    if (args[0].equalsIgnoreCase("kill")){
    if (!sender.hasPermission("plug.kill)){
    sender.sendMessage("For do this you must have permission plug.kill");
    return false;
    }
    //do something
    }
     
  6. I think you're catching what I'm asking about. What you said theoretically sounds like what I need, but how can I do that? Currently all I know is how to check if a player has a permission. How can I check if that player has the permission, and then take the permission and chop off the common part to get the 'X'?
     
  7. Here we go babe
    Code:
    
    private final Map<String, String> testmap = new HashMap<>();
    
    public void fillMap() {
          testmap.put("Hi", "hey mate");
    }
    
    @Override
    public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
       if(cmd.getName().equalsIgnoreCase("message") {
          if(args.length == 0 || args[0].equalsIgnoreCase("help")) {
              if(sender.hasPermission("message.help") {
                //Your fancy help command
              } else {
                      //You have no perms mate
              }
          } else {
             String messageName = args[0];
             String message = testmap.get(messageName);
             if(message != null) {
                if(sender.hasPermission("message." + messageName) {
                    sender.sendMessage(message);
                } else {
                       //You have no perms mate
                }
             } else {
               //That message do not exists
             }
       return true;
    }
    return false;
    }
    
    //Maybe you mean this ?
    String permission = "myplugin.command.test"
    String[] args = permission.split(".");
    String command = args[2];
    
     
  8. Offline

    caderape

    @NewDevOnTheBlock
    You can do many things with string. I think the method 'string.substring' and 'string.indexof' is what you want.
     
  9. Code:
    string.substring(string.lastIndexOf(".")+1);
    will return 50 from some.example.permission.50
     
  10. Offline

    nj2miami

    Explain what it is you are trying to accomplish and I'll try to offer you advice that matches your goals. You have not been very clear so far.
     
  11. @nj2miami I think maybe nobody noticed it, but:
     
  12. Offline

    nj2miami

    I noticed it, but how does that solve his problem? Especially since none of us clearly understand what he is trying to accomplish. Also, your way is pretty "hacky".
     
  13. @nj2miami
     
  14. Offline

    nj2miami

    @NewDevOnTheBlock
    Since so much nonsense has come in between your question and an answer, I'll ask you again if you could clarify what exactly you are trying to get and I will be happy to help you.
     
  15. @nj2miami
    Hey, so I think @FisheyLP understands where I'm trying to go. I have not tested his way of doing it yet, but I intend to do it very soon. Sorry if I have not been very clear.

    And don't worry about that person trying to spoon-feed me. I'm not using this code in an onCommand method, so there won't be any args to take in.

    @FisheyLP So, I understand taking a substring of a String, but how can I get my permission as a String? As I've stated before all I know how to do currently is check if the player has a permission, which returns boolean. Are you suggesting I do this...?

    Code:
    String permission = "some.permission.50";
    int value = Integer.parseInteger( permission.substring( permission.lastIndexOf( "." ) +1 ) );
    Which would get me the value I need, but I'm trying to make it so my permission variable can be any number (String) on the end. So one group can have "some.permission.50", another "some.permission.200", another, "some.permission.450". The number at the end of these permission nodes would be able to be whatever you want, and is not a set number. Then the method will be able to take that number off the end and use it for some algorithms.
     
  16. You would need to loop through all the permissions a player has, and then check if it starts with "some.permission." and with my little code, it gets the String which is after the last dot
     
  17. Hmm.. I've never seen anything on looping through a players permissions. I suppose I could just add a list to the config that would be..
    Code:
    ranks:
        rank_1: 50
        rank_2: 100
    etc..
    Iterate over that...
     
  18. Offline

    mythbusterma

    @NewDevOnTheBlock

    Looking for those permissions would be a lot better of an idea, as you don't have to loop through (what looks to be) hundreds of possible nodes (or use reflection to obtain the permissions tree).
     
Thread Status:
Not open for further replies.

Share This Page