Solved Vault Permissions?

Discussion in 'Plugin Development' started by Bobit, Apr 28, 2014.

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

    Bobit

    I can't seem to make my command work. On typing it, with or without the correct syntax, it does nothing, not even say that the command does not exist.
    I have an oncommand method something like:
    Code:java
    1. public void command(CommandSender sender, Command command, String label, String[] args) {
    2. if(label.equalsequalsIgnoreCase("knowsskill")){
    3. if (args.length >= 3) {
    4. //do something
    5. }else{
    6. System.out.println("Not enough arguments");
    7. }
    8.  
    9. }
    10.  


    in my plugin.yml I have something like:
    Code:
    ##required plugin stuff up here
    commands:
        knowsskill:
    I'm not sure what else I'm supposed to do! I saw new Command() in the API, but I haven't seen anyone else using that.
     
  2. Offline

    Rocoty

    Look at line 2 in the snippet. Find the corresponding line in your code. Does your IDE show errors on that line? Because "equalsequalsIgnoreCase" is not a method in the String class. Did you mean "equalsIgnoreCase"? Do not bother trying to run code with errors. It is bound to fail.
     
  3. Offline

    Bobit

    #Pseudocode
    No errors, anywhere. This isn't the real code.

    Fine. The real code is here

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  4. Offline

    Rocoty

    Is the method called "command" like in the snippet above? What about showing the whole code and plugin.yml?
     
  5. Offline

    Bobit

    Yes, it is. I don't want to show the whole code for now if it can be avoided.

    better formatting of the real method:
    Code:java
    1. public void command(CommandSender sender, Command command, String label, String[] args) {
    2. if (label.equalsIgnoreCase("knowsskill")) {
    3. boolean hasProperPerms = false;
    4. if (!(sender instanceof Player)) {
    5. //yes, the console has proper perms.
    6. hasProperPerms = true;
    7. } else if (sender.hasPermission("ReignRpg.givePermissions")) {
    8. hasProperPerms = true;
    9. } else {
    10. sender.sendMessage("You don't have proper permissions.");
    11. }
    12. if (hasProperPerms) {
    13. if (args.length >= 3) {
    14. if (Bukkit.getPlayer(args[0]) != null) {
    15.  
    16. boolean trueOrFalse;
    17. if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("on") || args[2].equalsIgnoreCase("yes")) {
    18. trueOrFalse = true;
    19. } else if (args[2].equalsIgnoreCase("false") || args[2].equalsIgnoreCase("off") || args[2].equalsIgnoreCase("no")) {
    20. trueOrFalse = true;
    21. } else {
    22. sender.sendMessage("A boolean value wasn't found after " + args[1]);
    23. return;
    24. }
    25.  
    26. PermissionAttachment permissionAttachment = Bukkit.getPlayer(args[0]).addAttachment(main, 1);
    27. //does the skills.___ permission exist?
    28. if (args[1].equalsIgnoreCase("teleswitch")
    29. || args[1].equalsIgnoreCase("mindbomb")
    30. || args[1].equalsIgnoreCase("blaze")
    31. || args[1].equalsIgnoreCase("fireball")
    32. || args[1].equalsIgnoreCase("vortex")
    33. || args[1].equalsIgnoreCase("fantheflames")
    34. || args[1].equalsIgnoreCase("vampirism")
    35. || args[1].equalsIgnoreCase("eruption")) {
    36. permissionAttachment.setPermission("ReignRpg." + args[1], trueOrFalse);
    37. }
    38. } else {
    39. sender.sendMessage("Player not found.");
    40. }
    41. } else {
    42. sender.sendMessage("Not enough parameters.");
    43. }
    44. }
    45. }
    46. }

    The command is /knowsskill [player] [skill] [boolean].
    It gives or takes away that player's permission to use that skill.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  6. Offline

    Rocoty

    Call the method "onCommand" (use @Override to be notified when trying to override a non-existent method)
    If this is not in the main class, make sure to set the executor of the command to the class in question. (The class has to implement CommandExecutor if it is not the main class)
     
  7. Offline

    Bobit

    It's in the main class.
    It has an @EventHandler above it, ofc
    I've done an event called "interaction" before, and it worked onInteraction.
    What are you trying to say about overrides?
     
  8. Bobit Commands aren't events.
     
  9. Offline

    Rocoty

    Bobit A command is not an event, no need to use @EventHandler or register events. The method must be called "onCommand" as it is inherited from JavaPlugin and can be overridden (giving a new implementation). Calling the method "command" won't get you far.
     
    Bobit likes this.
  10. Offline

    Bobit

    Alright, found it on the API.
    I guess the method should return false if the command doesn't exist?
     
  11. Offline

    Rocoty

    Bobit Yes. It should also return false if the command did not execute properly.
     
    Bobit likes this.
  12. Offline

    Bobit

    Alright, I'm lost while trying to use vault permissions.
    1) Can you do perms.playerAdd(player, "ReignRpg." + args[1]) , assuming args[1] is a valid permission extension?
    2) do you need to declare them in your plugin.yml?
    3) player.hasPermission(string) is still used with vault permissions, right?
     
  13. Offline

    Bobit

    It's not working. I'm getting a null error here:
    Code:java
    1. if (perms.playerHas(player, "ReignRpg.fireball")) {
    2. Skillz.fireball(this, player);
    3. } else {
    4. System.out.println("You don't have permission.");
    5. }

    Apparently perms is null. I followed this guide, but it's still not working.

    The current code:
    Code:java
    1. public final class ReignRpg extends JavaPlugin implements Listener {
    2.  
    3. /* this class is to contain:
    4.   * 1) All of the listeners
    5.   * 2) Methods so generic that it is very likely they will be used in multiple (game) classes.
    6.   */
    7. public final Plugin main = this;
    8. public static Random rand = new Random();
    9. public static Permission perms = null;
    10. Skills Skillz = new Skills(); /*Skillz is an instance of the Skills class.
    11.   * Skills is really just a library of skills, but it is made into an object because
    12.   * it's always best to keep it there just in case. Cooldowns and methods
    13.   * using cooldowns could easily be made static, but why bother?
    14.   * That only messes with multi-server operations.
    15.   */
    16.  
    17.  
    18. @Override
    19. public void onEnable() {
    20. System.out.println("Plugin [ReignRpg] has been enabled!");
    21. getServer().getPluginManager().registerEvents(this, this);
    22. this.saveDefaultConfig();
    23. assert !setupPermissions() : "[ERROR] permissions weren't set up!";
    24. }
    25.  
    26. @Override
    27. public void onDisable() {
    28. }
    29.  
    30. private boolean setupPermissions() {
    31. RegisteredServiceProvider<Permission> rsp = getServer().getServicesManager().getRegistration(Permission.class);
    32. perms = rsp.getProvider();
    33. return perms != null;
    34. }
    35.  
    36. @EventHandler
    37. public void interaction(PlayerInteractEvent event) {
    38. //whenever the player right-clicks, this code is done. It should lead to all other "on-right-click" spells, with their conditions inside them.
    39. Player player = event.getPlayer();
    40. /*it's as simple as:
    41.   * Skillz.{skillname}(this, [player]);
    42.   * for almost every single skill you want to maybe activate on right-click.
    43.   */
    44. //to make a new skill: insert listener here. Insert name into long or statement in oncommand. create code. declare permission in plugin.yml. declare config in config.yml.
    45. if (event.getAction() == Action.RIGHT_CLICK_AIR || event.getAction() == Action.RIGHT_CLICK_BLOCK) {
    46. if ((player.getItemInHand().getType()) == Material.FLINT) {
    47. System.out.println("Attempting to cast fireball");
    48. if (perms.playerHas(player, "ReignRpg.fireball")) {
    49. Skillz.fireball(this, player);
    50. } else {
    51. System.out.println("You don't have permission.");
    52. }
    53. } else if ((player.getItemInHand().getType()) == Material.GHAST_TEAR) {
    54. if (player.hasPermission("ReignRpg.mindBomb")) {
    55. Skillz.mindBomb(this, player);
    56. }
    57. } else if ((player.getItemInHand().getType()) == Material.BLAZE_ROD) {
    58. if (player.hasPermission("ReignRpg.eruption")) {
    59. Skillz.eruption(this, player);
    60. }
    61. } else if ((player.getItemInHand().getType()) == Material.BLAZE_POWDER) {
    62. if (player.hasPermission("ReignRpg.blaze")) {
    63. Skillz.blaze(this, player);
    64. }
    65. }
    66.  
    67. MaterialData itemData = player.getItemInHand().getData();
    68. //Check if it's a type of dye
    69. if (itemData instanceof Dye) {
    70. //Get the dye
    71. Dye dye = (Dye) itemData;
    72. if (dye.getColor().equals(DyeColor.WHITE)) {
    73. if (player.hasPermission("ReignRpg.fanTheFlames")) {
    74. Skillz.fanTheFlames(this, player);
    75. }
    76. } else if (dye.getColor().equals(DyeColor.BLUE)) {
    77. if (player.hasPermission("ReignRpg.vortex")) {
    78. Skillz.vortex(this, player);
    79. }
    80. }
    81. }
    82. }
    83. }
    84.  
    85. @EventHandler
    86. public void sneakToggle(PlayerToggleSneakEvent event) {
    87. Player player = event.getPlayer();
    88. //if they're holding string
    89. if (((player.getItemInHand().getType()) == Material.STRING) && (!player.isSneaking())) {
    90. if (player.hasPermission("ReignRpg.teleswitch")) {
    91. Skillz.teleswitch(this, player);
    92. }
    93. }
    94. }
    95.  
    96. @Override
    97. public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
    98. //the main purpose of commands for now is to give the admins the power to instantly change other's permissions. This is especially needed for skills that share the same cast-item.
    99. //knowsskill goobt teleswitch true
    100.  
    101. if (label.equalsIgnoreCase("knowsskill")) {
    102. if (sender instanceof Player && !(sender.hasPermission("ReignRpg.givePermissions"))) {
    103. sender.sendMessage("You don't have proper permissions.");
    104. return false;
    105. }
    106. if (args.length >= 3) {
    107. if (Bukkit.getPlayer(args[0]) != null) {
    108.  
    109. boolean trueOrFalse;
    110. if (args[2].equalsIgnoreCase("true") || args[2].equalsIgnoreCase("on") || args[2].equalsIgnoreCase("yes")) {
    111. trueOrFalse = true;
    112. } else if (args[2].equalsIgnoreCase("false") || args[2].equalsIgnoreCase("off") || args[2].equalsIgnoreCase("no")) {
    113. trueOrFalse = false;
    114. } else {
    115. sender.sendMessage("A boolean value wasn't found after " + args[1]);
    116. return false;
    117. }
    118.  
    119. //does the skills.___ permission exist?
    120. if (args[1].equalsIgnoreCase("teleswitch")
    121. || args[1].equalsIgnoreCase("mindbomb")
    122. || args[1].equalsIgnoreCase("blaze")
    123. || args[1].equalsIgnoreCase("fireball")
    124. || args[1].equalsIgnoreCase("vortex")
    125. || args[1].equalsIgnoreCase("fantheflames")
    126. || args[1].equalsIgnoreCase("vampirism")
    127. || args[1].equalsIgnoreCase("eruption")) {
    128. if (trueOrFalse) {
    129. perms.playerAdd((Player) Bukkit.getPlayer(args[0]), "ReignRpg." + args[1]);
    130. sender.sendMessage("Gave " + args[0] + " ReignRpg." + args[1]);
    131. Bukkit.getPlayer(args[0]).sendMessage("You were given the permission ReignRpg." + args[1]);
    132. } else {
    133. perms.playerRemove((Player) Bukkit.getPlayer(args[0]), "ReignRpg." + args[1]);
    134. sender.sendMessage("Took " + "ReignRpg." + args[1] + " from " + args[0]);
    135. Bukkit.getPlayer(args[0]).sendMessage("Your permission ReignRpg." + args[1] + " was removed.");
    136. }
    137. return true;
    138. } else {
    139. sender.sendMessage("That skill doesn't exist.");
    140. return false;
    141. }
    142.  
    143. } else {
    144. sender.sendMessage("Player not found.");
    145. return false;
    146. }
    147. } else {
    148. sender.sendMessage("Not enough parameters.");
    149. return false;
    150. }
    151. } else {
    152. return false;
    153. }
    154. }
    155.  


    Now it's saying I don't have permissions again, but there's not a null error!

    Edit: Do I need to put something in my server's permission.yml for it to run?

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  14. Bobit Well you'll need the permission if that's what you mean?
     
  15. Offline

    Bobit

    AdamQpzm
    Shouldn't the code I have automatically put it in the permissions.yml??
     
  16. Bobit From a cursory glance, it doesn't look like you've created a plugin that ever assigns a permission, simply checks whether they have it (which, by the way, is a bad idea if that's what you're doing. You shouldn't use vault to check for a permission as bukkit can do that)
     
  17. Offline

    Bobit

    I'm using vault so that it can potentially be hooked into permissions plugins. I mean, I'm going to have to do that sooner or later, right?

    The permission is assigned on line 129:
    Code:java
    1. perms.playerAdd((Player) Bukkit.getPlayer(args[0]), "ReignRpg."+args[1]);
     
  18. Bobit Oh, I didn't see that bit. Are you sure that line is executed and runs successfully? Also, you should still use player.hasPermission("") instead, as it works independent of vault & permission plugins.
     
  19. Offline

    Bobit

    Yes, I'm sure it runs successfully. If you notice line 130, it has a System.out.println(), which outputs variables that make sense.
    2 Questions:

    1) You say "it works independent of vault & permission plugins". Do you need to somehow implement a permissions plugin for basic permissions setting like this to work?

    2) How do you do it without vault? Don't you have to make a temporary permission attachment, and save the permissions to a config file?
     
  20. Bobit 1) It depends what you mean. Vault provides the ability to hook into permissions plugins (as well as other thing) but it's not a permissions plugin itself, nor does it provide you with an interface for making one. In that regard, the server you are testing this on needs a permissions plugin, but it does not matter which one (as long as Vault supports it), and you do not need to adjust your own code to match whichever plugin you're using.

    2) Without vault, I just meant checking for permissions. Think of these 3 similar but different things you can do with permissions:

    - You can check if a player has a permission. This is something which any old plugin is likely to do, and is generally done without vault (e.g. by using something like event.getPlayer().hasPermission("item.use"))
    - Give players a permission. This isn't as common as the above, and would be better done with vault. Generally the best use for this sort of situation is when they want to give a certain permission once something is done, rather than by default. For example, you might want to give players the "item.dropdouble" permission once they complete a certain task. You can do this without vault but each permissions plugin has its own way of giving players permissions. That's what vault does - you tell vault that you want this player to have that permission, and vault will work out what permissions plugin the user is using, and use the correct method for giving the player the permission according to whatever plugin they're using.
    - Fully managing permissions. This is what we call a permissions plugin (like PEX, GroupManager, whatever...) and I believe this is what you're referring to in question 2. If you make a permissions plugin, it would effectively be your responsibility to answer questions like "does this player have this permission?" and to keep track of the permissions. Then you don't need vault at all, although you'd be much better off if you and vault supported each other (all big permissions plugins support vault, as that's what it's made for, and it makes it much easier for the server owners and plugin developers).

    Well that message was a lot longer than I originally intended, how did that happen? Oh well, I hope it's of some use to you and doesn't confuse you further. I guess my question for your problem right now though is: do you have a permissions plugin installed? Or are you trying to make your own permissions plugin included in this?
     
  21. Offline

    Bobit

    I don't have a permissions plugin installed.

    I want a method that gives/takes away a player's permission.
    I want this to be a method I can call, I don't care whether it's in my plugin or the big permissions plugins.

    The reason I want this is that, eventually, I want to make a skill tree, and when you give a player a skill point, they can spend it on unlocking new skills. I don't really want it to be that you're either in the group that has such-and-such skills, or you're in the other group that has different skills.
     
  22. Bobit Then yes, it sounds like you're doing the thing I mentioned - giving a player a permission as a reward for completing a task :) (in this case, buying a skill). In that case you'll need to go with this sort of set-up:

    On your test server: Make sure you have a permissions plugin (it doesn't matter what one, as long as it's one vault is compatible with) and vault

    In your plugin: Hook into vault and assign the permission with playerAdd as you already are.

    See if that works once you've installed the permissions plugin :)
     
    Bobit likes this.
  23. Offline

    Bobit

    Okay. Before I do that, will I have to tell vault which permissions plugin I want it to use?
     
  24. Bobit Nope, it'll do that for you :)
     
    Bobit likes this.
  25. Offline

    Bobit

    Alright, I installed permissionsEX (would've installed bpermissions if it was updated, it looks easier)
    The permissions.yml looks perfect:
    Code:
    groups:
      default:
        default: true
        permissions:
        - modifyworld.*
    users:
      6443e36d-4675-47ae-ae41-90d03c849520:
        worlds:
          world:
            permissions:
            - ReignRpg.fireball
    
    The code's running without any errors, too!
    BUT
    I still don't have permission :'(

    (I typed /knowsskill goobt fireball true, and then right-clicked flint to activate fireball, but it says I don't have permission!)

    But WAIT!
    When I restarted the server, it worked! :D
    Now I'm searching for a method in vault/PEX that automatically reloads permissions. I'll set this to [SOLVED] if I find it.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 7, 2016
  26. Bobit I didn't think that would be necessary. However, if you do for some reason need to do that, I don't think there's a method for it in Vault.

    However, what you could do is use the RegisteredServiceProvider, get the Plugin, and then just disable then enable it. That should work for any of the permissions plugins, but probably isn't the best way to go about things.
     
  27. Offline

    Bobit

    I googled it quickly, and the solution is:
    Code:java
    1. getServer().dispatchCommand(getServer().getConsoleSender(), "pex reload");

    That's a PEX command, so it must be the best way.

    And it works! [SOLVED]

    (Although, the server did encounter a client-side "unexpected" error that disconnected me. But that's probably ok.)
     
  28. Bobit Alright, but be warned that your plugin will no longer support other permissions systems by doing that, only pex :p
     
  29. Offline

    Bobit

    Probably less laggy though, so I'll stick with it for now. Later I could make a decision tree to see what is the least laggy known option for each plugin, but for now it'll just work with PEX.

    Edit: after looking at the source code for /pex reload, yeah, this'll lag waaay less.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page