Give a Perm

Discussion in 'Plugin Development' started by geoff0217, Sep 17, 2012.

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

    geoff0217

    How do I give a player a permission though a command.
    ex. /give would give the player essentials.fly
     
  2. Offline

    DirtyStarfish

    You need to use PermissionAttachment. If you can get the player variable, through an event or command, you can use "PermissionAttachment attachment = Player.addAttachment(JavaPlugin)".

    For example if the command was in your main class:
    Code:
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args)
    {
        Player player = null;
        if (sender instanceof Player)
        {
            player = (Player) sender;
        }
     
        if (command.getName().equalsIgnoreCase("give")
        {
            if (player != null)
            {
                PermissionAttachment attachment = player.getAttachment(this);
                attachment.setPermission("essentials.fly", true);
            }
        }
    }
    
    You can add more to this to add command permissions or to add arguments, so you can set the permission for other players.

    You can read more about this here: http://wiki.bukkit.org/Developing_a_permissions_plugin
     
  3. Offline

    desht

    Unfortunately your code won't work, since there is no player.getAttachment() method. In any case, using just Bukkit calls is only useful for adding transient permissions to players - those nodes won't survive a server restart. To do that, you need to get the permission node into the player's permission plugin so it can be saved.

    geoff0217 I would recommend either looking at the way Vault does it, or just hook Vault and use it (if you want to add permissions which survive a server restart you'll want to hook Vault anyway).

    For adding a transient permission with Bukkit calls only: https://github.com/MilkBowl/Vault/blob/master/src/net/milkbowl/vault/permission/Permission.java#L219

    For adding persistent permission nodes (surviving a server restart), use Vault's Permission.playerAdd() call: http://mythcraft.dyndns.org/javadoc/vault/net/milkbowl/vault/permission/Permission.html
     
Thread Status:
Not open for further replies.

Share This Page