Solved Creating permissions in code.

Discussion in 'Plugin Development' started by bfgbfggf, May 26, 2013.

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

    bfgbfggf

    I want to create some permissions for commands.
    Eg: Player want to spawn book. And must have permissions: Plugin.SpawnBook.<BookFileName>
    and that must have parent to Plugin.SpawnBook.*

    For use command player must have
    if (sender.hasPermission(" Plugin.SpawnBook")) {

    and then I use the
    Permission permission = new Permission("Plugin.LoadBook."+args[1], PermissionDefault.OP);
    permission.addParent("Plugin.LoadBook.*", true);

    And then.... add Plugin.LoadBook.* to plugin.yml with children Plugin.SpawnBook ?
    or also add that to code?

    And that will be works?
     
  2. Offline

    caseif

    Why not just define the permissions in the plugin.yml?
     
  3. Offline

    Jogy34

    I think it's so that the users can create their own books and have permissions for each one.

    Anyways that should work either way you do it either in code or in the plugin.yml. And don't forget to save the permission endings to your config file or something like that so that you can retrieve them when your plugin starts up again so that the players don't have to keep setting up every permission for all of their books. If you have a specific set of books though you should just be able to add of the permission to the plugin.yml and that would make it a lot easier.
     
  4. Offline

    Wingzzz

    You could make a Book class that contains some information such as creator and other miscellaneous information. Do permissions like normal but use it something more like this:
    Code:java
    1.  
    2. Book book = new Book(player.getName(), "book-name-or-identifier"); // could make the name args they type
    3. if(player.hasPermission("Plugin.SpawnBook." + book.getName()) {
    4. // do something (ie: give the player the book)
    5. }
     
  5. Offline

    Jogy34

    You still have to add the permission in the code first before you can successfully check if a player has the permission.
     
  6. Offline

    Wingzzz

    Jogy34
    In what code?
    Not sure if you mean how the permission might not work when putting the book's name on the end of it as it'd need to be in the plugin.yml... Not sure, never tested anything of the sort. Although if that does become a problem, reflection should do the trick.
     
  7. Offline

    Jogy34

    As long as I am understanding what he is trying to do, if someone creates a book that needs a permission to use a command to get it you have to create that permission in code before you can check if a player has that permission otherwise it will always return true.
     
  8. Offline

    Wingzzz

    Like if I did /book create lebook we need to create a new permission for this book (ie: Permission permission = new Permission(...))... Alright well in the sense of registering a permission like the plugin.yml or reflection? Or simply giving a permission (could be handled with Vault if so)?
     
  9. Offline

    Jogy34

    Never use vault for permissions. It's completely useless and is even advised against in the common mistakes sticky thread. And you don't have to use reflection to register a permission. You can just do:
    Code:
    plugin.getServer().getPluginManager().addPermission(new Permission("perm.ission"));
     
  10. Offline

    Wingzzz

    Well I don't use vault for anything other than hooking into permission plugins, I still use player.hasPermission() from bukkit etc...
    Fantastic! Thanks for letting me know! I never know you could do that, this will open a lot for me :)
     
  11. Offline

    Jogy34

    Vault just calls the bukkit methods for permissions and the permission plugins can automatically sync in with the bukkit permission system so you don't have to use vault.
     
    WiredWingzzz likes this.
  12. Offline

    bfgbfggf

    Hmm ok. I can explain problem :)
    I have two command one for save book to file(You choose name of file). (just saved ItemStack to config config.set(Book, ItemStack))
    And second to get/load that book.
    To use just command you must have Plugin.LoadBook permissions.
    But to get book with that command you must have Plugin.LoadBook.<FileName> or Plugin.LoadBook.* or just Plugin.*

    Soo... My code will be work for that? (also write if I must add something to MainClass etc)

    Code:java
    1.  
    2. if (player.hasPermission("Plugin.LoadBook")){
    3. //some code here
    4. Permission perm = new Permission("Plugin.LoadBook."+args[1], PermissionDefault.OP); //args[1] is file Name (without .yml)
    5. perm.addParent("Plugin.LoadBook.*", true);
    6. if (player.hasPermission(perm)){
    7. //some code here
    8. }
    9. }
    10.  


    Plugin.LoadBook and Plugin.LoadBook.* is in plugin.yml
    OK. I go to sleep. 00:10 am...
     
  13. Offline

    Jogy34

    First, You have to add this in order to register the permission:
    Code:
    plugin.getServer().getPluginManager().addPermission(permission);
    
    Second you aren't going to be able to save/retrieve the book just by saving the ItemStack. I think you're going to have to save each page.
     
  14. Offline

    rsod

    You don't really need mess around with it, you can just check permissions and don't care about anything
     
  15. Offline

    bfgbfggf

    Saving(And load) books works greats :)
    Example of file:
    http://pastebin.com/sDJ2dk04

    Soo.. about permissions.
    Where add that
    plugin.getServer().getPluginManager().addPermission(permission);
    just create

    Code:java
    1.  
    2. if (player.hasPermission("Plugin.LoadBook")){
    3. //some code here
    4. Permission perm = new Permission("Plugin.LoadBook."+args[1], PermissionDefault.OP);
    5. perm.addParent("Plugin.LoadBook.*", true);
    6. plugin.getServer().getPluginManager().addPermission(perm);
    7. if (player.hasPermission(perm)){
    8. //some code here
    9. }
    10. }
    11.  

    ?
     
  16. Offline

    Jogy34

    You want to add the permission when you create the book that needs the permission
     
  17. Offline

    bfgbfggf

    Jogy34 rsod
    Sorry for no response I have some thing to do ;/
    And... I just test "idea" of rsod
    if (player.hasPermission("KillLevels.LoadBook." + args[1]) || player.hasPermission("KillLevels.LoadBook.*"))
    And work... :p
     
  18. Offline

    rsod

    It's better to not use * in permissions as plugins could parse it as regular expression
     
  19. Offline

    bfgbfggf

    Hyym. ok thanks, but I first test it :p
     
  20. Offline

    Jogy34

    There isn't any problem with plugins using a * in their permission nodes.
     
Thread Status:
Not open for further replies.

Share This Page