Help with giving a player permissions

Discussion in 'Plugin Development' started by josh12341, Nov 9, 2013.

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

    josh12341

    Hey guys,
    I couldn't seem to figure out what code you have to use to give a player a permissions, I know you can use
    Code:java
    1. if(p.hasPermission("example.example")
    but I want it to give the player permission.
    So for example:
    If a player has the permission "powers.*" they can do /powers add <group> <name>
    so then if they do that and they have the permission it will give the targetplayer the permission that they wrote.
    Please help me!
    Thanks, Josh
     
  2. Offline

    PixelatedTNT

    Are you trying to edit another plugins permissions file using your plugin? If you are that will be quite difficult.
     
  3. Offline

    josh12341

    No, is it really that hard, all I want to do is when a player types a command, it gives them a permission, how do I code it???
    That's all I want to know
     
  4. Offline

    PixelatedTNT

    You will need a permissions manager to store each players permissions, without one, your command is useless.
     
  5. Offline

    josh12341

    OMG are you actually serious!
    I'm just gonna repost cause none of you guys know what you're doing
     
  6. Offline

    PixelatedTNT

    Lol! Reposting is going to get you nowhere...maybe if you took the time to explain yourself better, people might actually respond to your post with some helpful information.
     
    MrTwisters and GraveThief like this.
  7. Offline

    josh12341

    2 questions:
    1. I want to have it when a player types /wizard it gives them and item with a custom name and when you right click it does lightning where you're looking and when you left click it does a fireball where you're looking. Could you give me an example please.

    2. I couldn't seem to figure out what code you have to use to give a player a permissions, I know you can use
    Code:java
    1. if(p.hasPermission("example.example")
    but I want it to give the player permission.
    So something like this:
    Code:java
    1. if(commandLabel.equalsIgnoreCase("hi")
    2. p.givePermission("example.example")

    so If they did /hi it would give them the permission example.example but I don't know what code you have to use to give a player a permission.
    Please help me!
    Thanks, Josh
     
  8. Offline

    1Rogue

    Well, I replied to the other one, but I suppose I'll put it here:


     
  9. Offline

    np98765

    Merged duplicate threads (also cleaned up a tad).
     
  10. Offline

    Dunsuro

    josh12341
    Here mate, let me answer them one at a time.

    First one, the adding perms:

    You'll need to hook into vault for this example.

    From their javadocs:
    Code:
    boolean    playerAdd(Player player, String permission)
    Add permission to a player ONLY for the world the player is currently on.
     
    abstract boolean playerAdd(String world, String player, String permission)
    Add permission to a player.
     
    boolean    playerAdd(World world, String player, String permission)
    Add permission to a player.
    
    This means, that you could do:

    Code:java
    1. public static Permission permission = null;
    2.  
    3. private boolean setupPermissions()
    4. {
    5. RegisteredServiceProvider<Permission> permissionProvider = getServer().getServicesManager().getRegistration(net.milkbowl.vault.permission.Permission.class);
    6. if (permissionProvider != null) {
    7. permission = permissionProvider.getProvider();
    8. }
    9. return (permission != null);
    10. }
    11.  
    12. public void addPerm(String perm, Player player){
    13. permission.playerAdd(player, perm);
    14. }


    It's that simple :)

    As for question 2:

    You need to use item meta to rename the item, then simply have an event that listens for your interaction with the lightning (PlayerInteractEvent). That should do it
     
  11. Offline

    josh12341

    Is there any way to do it without vault because I want it to be one plugin not having to have Vault for it to run properly and is there any other way?

    And could you give an example of the custom item, as in the player listener, I'm kind of a bit new to coding bukkit......
     
  12. Offline

    Dunsuro


    Actually, there are some other ways to do it, although not as clean. I assumed you wanted vault because that's generally the go to library for making things simple on everyone. :p

    I have never tried the following approach (but it should work). You can read more on it at this link: http://wiki.bukkit.org/Developing_a_permissions_plugin

    Code:java
    1. HashMap<String,PermissionAttachment> attachments = new HashMap<String, PermissionAttachment>();
    2.  
    3. PermissionAttachment attachment = player.addAttachment(plugin);
    4. attachment.setPermission("this.is.a.perm.node",true);

    ^Only define the hashmap once, in your main class. Do the rest in the oncommand

    Be sure to clean up when a player leaves like so:

    Code:
    @EventHandler
    public void pQuit(PlayerQuitEvent event){
    Player player = event.getPlayer();
    player.removeAttachment(attachments.get(player.getName()));
    }
    ^Above goes in a listener class

    I used the example from the developing a permissions plugin page because that's basically what you would have to do if you do not want to use vault. Best of luck and feel free to ask more questions!
     
  13. Offline

    josh12341

    And could you give an example of the custom item, as in the player listener, I'm kind of a bit new to coding bukkit......
    And I still don't really get what you're saying, Is there any way to do /wizard and then it will give you the permission powers.wizard ???
     
  14. Offline

    Dunsuro

    josh12341

    To get a custom item:
    Code:java
    1. ItemStack stack = new ItemStack(Material.STICK, 1);
    2. //Material.STICK is the type of the item
    3. //1 is the amount
    4. ItemMeta meta = stack.getItemMeta();
    5. meta.setDisplayName("Magic Stick");
    6. stack.setItemMeta(meta);
    7.  

    then just give that to players via something like:

    Code:java
    1. player.getInventory().addItem(stack);
    2. player.updateInventory();
    3. //Yes this throws a flag, I generally just go ahead and use it to be safe.


    Then, in your listener:
    Code:java
    1. @EventHandler
    2. public void pInteract(PlayerInteractEvent event){
    3. Player player = event.getPlayer();
    4. if(event.getAction() == Action.RIGHT_CLICK_AIR){
    5. //will only listen to right clicking of the air
    6. ItemStack held = player.getItemInHand();
    7. if(held.getType() == Material.STICK){
    8. ItemMeta meta = held.getItemMeta();
    9. if(meta != null){
    10. if(meta.getDisplayName().equalsIgnoreCase("Magic Stick"){
    11. player.getWorld().strikeLightning(player.getTargetBlock(null, 200).getLocation());
    12. }
    13. }
    14. }
    15. }
    16. }

    ^Sorry for lack of indents.
     
  15. Offline

    josh12341

    Thanks, So much more useful :)
     
Thread Status:
Not open for further replies.

Share This Page