Sporech
Sporech

Reputation: 35

How do I add permissions on command?

I can't find how to give a player certain permissions when they enter a command. The Bukkit API is very helpful in showing me possible methods when I just do player or permission. But nothing comes up which will give a player a certain permission. Here is the code I have to make it work:

(at the top of my main class)

public Permission blue = new Permission("Blue.allowed");

(In onEnable())

PluginManager pm = getServer().getPluginManager();
pm.addPermission(blue);

I have this simple command that I want to give the player who types it in to get the Blue permission:

    public boolean onCommandAllowRed(CommandSender sender, Command cmd, String label, String[] args) {
    if (cmd.getName().equalsIgnoreCase("give red") && sender instanceof Player) {
        Player player = (Player) sender;

        return true;
    }
    return false;

I just don't get the object model for perms, can anyone help with this?

Upvotes: 1

Views: 2503

Answers (3)

Airbornz
Airbornz

Reputation: 3

I had issues with this at first as well too, I found it out by myself, i also suggest a way of saving this otherwise on logout the permission will be lost.

This is how I manage permissions.

private static PermissionAttachment fetchPlayer(UUID uuid){
    Plugin plugin = Core.getPlugin();
    Player player = Bukkit.getPlayer(uuid);
    if (player != null){
        PermissionAttachment perm = player.addAttachment(plugin);
        return perm;
    }
    else{
        return null;
    }

}

public static void addPermission(UUID uuid, String permission){
    PermissionAttachment perm = fetchPlayer(uuid);
    perm.setPermission(permission, true);
}

public static void removePermission(UUID uuid, String permission){
    PermissionAttachment perm = fetchPlayer(uuid);
    perm.unsetPermission(permission);
}

Upvotes: 0

Ethan Z
Ethan Z

Reputation: 228

To add on to baseball435's answer (which is completely correct), the easiest and most efficient way of doing this is by hooking into the Vault API rather than any individual plugin. It is an adapter for almost every well-written permission plugin for Bukkit and will keep you from having to deal with different APIs from different systems.

Upvotes: 2

Jmrapp
Jmrapp

Reputation: 520

Bukkit doesn't make it easy to add permissions programmatically to a single player. Doing so would require a few headaches.

To make it easier I'd recommend using a plugin for permissions like bPermissions or PEX and then hook into their plugin and use the given methods to add the permission to the player.

To "hook in" you need to include the plugin into your build path and then get an instance of it. Typically plugins will tell you how to hook into then on their bukkit dev pages.

Upvotes: 2

Related Questions