Solved Help with this code

Discussion in 'Spigot Plugin Development' started by ShimeHD, Feb 13, 2019.

  1. So i made plugin and everything is okay but when player gets permission for command he needs to relog on server to be able to use command. So i made this

    Code (Text):
    public boolean checkPerms(CommandSender s)
      {
          Set<String> name = Main.get().getConfig().getConfigurationSection("ranks").getKeys(false);
        String path = "ranks." + name + ".";
        if (s.hasPermission(path + ".permission")) {
          return true;
        }
        return false;
      }
    I thought this would check if he have defined permission in config which looks like this

    Code (Text):
    ranks:
      Warrior:
        space: 3
        permission: "warrior.3"
        op: true
    the problem is that after this code player still needs to relog to use given permission?
    what to do now? or is there anything wrong with the code, thanks?!
     
    #1 ShimeHD, Feb 13, 2019
    Last edited: Feb 14, 2019
  2. You'll still have to apply to permission to the user after checking if he does, PermissionAttachment
     
  3. Couple things wrong:
    Code (Text):
    public boolean checkPerms(CommandSender s)
      {
          Set<String> name = Main.get().getConfig().getConfigurationSection("ranks").getKeys(false); //Not needed, you can loop through configuration section
        String path = "ranks." + name + ".";
        if (s.hasPermission(path + ".permission")) { //You're not actually getting the permission within the config.
          return true;
        }
        return false;
      }
    A way to do this would be:
    Code (Text):

    for(String key : Main.get().getConfig().getConfigurationSection("ranks").getKeys(false)){ //Looping through configuration section

       if(s.hasPermission(Main.get().getConfig().getString("ranks."+key+".permission"))){ //Checking if the player has permission node
           return true; //Returning true
       }
    }
     
     
  4. Or if you since you have that already just do
    Code (Text):

    for (String keyValues : keys) {
    }
     
    • Agree Agree x 1
  5. Code (Java):
    public boolean checkPerms(CommandSender s) {
    return Main.get().getConfig().getConfigurationSection("ranks").getKeys(false).stream().anyMatch(str -> s.hasPermission("ranks." + str + ".permission");
      }
    Shouldn't this suffice? Just get all ranks, and if the player has any permission from them, return true. If the player is found to have none of the listed permissions, returns false.
     
    • Agree Agree x 1
  6. Yeah i just looped through all ranks, checked if sender has permission and and it still gives me same problem. Player needs to relog, my head is bleeding dont know what to do anymore.
     
  7. Sounds like that's something that doesn't have to do with the method/plugin you're making but rather the permission plugin on your server.
     
  8. Also tried multiple perms plugins
     
  9. When are you checking a users perms?
     
  10. Code (Text):


    import org.apache.commons.lang.math.NumberUtils;
    import org.bukkit.Bukkit;
    import org.bukkit.command.Command;
    import org.bukkit.command.CommandExecutor;
    import org.bukkit.command.CommandSender;
    import org.bukkit.entity.Player;

    public class VarCMD
      implements CommandExecutor
    {
      public boolean checkPerms(CommandSender s)
      {
          for(String name : Main.get().getConfig().getConfigurationSection("ranks").getKeys(false)){
        if (s.hasPermission(Main.get().getConfig().getString("ranks."+name+".permission"))) {
          return true;
            }
        }
        return false;
    }
     
     
       
     
      public boolean onCommand(CommandSender s, Command cmd, String label, String[] args)
      {
        if (!(s instanceof Player))
        {
          s.sendMessage("Only players!");
          return true;

    Here is the rest of commands code
        }
     
  11. Must be something to do with how you're giving the player the permission node, or your permission plugin.
     
  12. Tried permissionex giving permission manualy through command /pex user add "", and tried LuckPerms also adding manually permission
     
  13. Ok i made progress

    config that needs to be checked

    Code (Text):
    ranks:
      Warrior:
        space: 3
        permission: "var.god"
        op: true
    so this code checks player permission and player does not need to relog

    Code (Text):
    public boolean checkPerms(CommandSender s)
      {
     
        if (s.hasPermission("var.god")) {
          return true;
        }
        return false;
        }
    but if i make it like this which i have to do, i mean i need to loop through section it does not work
    Code (Text):
    for(String key : Main.get().getConfig().getConfigurationSection("ranks").getKeys(false)){

       if(s.hasPermission(Main.get().getConfig().getString("ranks."+key+".permission"))){
           return true;
       }
    }
    return false;
        }
     
  14. Probably not helpful to try to introduce streams to a beginner.
     
  15. On your loop, try debugging the key and whether the if statement is true or not. If in any case, you see that the player has the permission being debugged but in your if statement it doesn't return true, let us know.
     
  16. Wow i did not saw this stream option i tried and it works with stream, thanks for pointing to this

    also thank you @Shin1gamiX for helping
     
  17. ¯\_(ツ)_/¯ not sure why using a stream fixed the issue but since it worked out... although I'm curious to why the enhanced for loop didn't.
     

Share This Page