Temporarily Granting the Player a Permission

As the title says I’m trying to grant the player a temporary permission, the general idea is that I would grant them the permission, execute a command from the player that requires that permission, and then unset said permission what I’ve tried so far is something like the code below:

player.getSubjectData().setPermission(SubjectData.GLOBAL_CONTEXT, "some.permission", Tristate.TRUE);
Sponge.getCommandManager().process(player, "command requiring permission");
player.getSubjectData().setPermission(SubjectData.GLOBAL_CONTEXT, "some.permission", Tristate.UNDEFINED);

However, this doesn’t seem to work as when the command is attempted to be executed it gives the “You don’t have permission!” message.

Any Ideas?

Are you using a permission plugin to test it?

Yes Lucky Perms

Check if the permission is tested with the debug mode of LuckPerms.

It is being tested, but when the command is executed it’s still undefined, even though the permission is in the player’s subjectData (verified with Intellij Debugger).

Though doing some more reading this may actually be an issue with Lucky Perms itself.

You should test with PEX or PermissionManager to see if it is a bug with Lucky Perms.

Just did, so it seems the issue is that there is a delay between the permission being added to the SubjectData and it being reflected by the permission service and the delay is large enough that it interferes with the command being executed by the subsequent line of code, not really sure if there’s a way to get around this.

It depends how @Luck implemented it.

Similar weird behavior also occurs in pex, I’d rather not depend on the implementation of the permissions service to dictate whether or not my code will work, I suppose I’ll have to keep searching for another way.

Yes - you’ll have issues with it. The calls to add permissions are ran on separate threads. I believe PEX does the same.

You could try setting the permission, waiting 1000ms or so, and then executing the command.

Another idea that may or may not work: Try creating an implementation of ProxyCommandSource and passing that to Sponge.getCommandManager().process().

I thought about that, I’ve already provided functionally to my users to execute the command from the console. The idea is that for my CommandBooks plugin, there might be cases where the command cannot be executed by the console on the player’s behalf, i.e. the command doesn’t take a player as an argument, and so command books would be used as a way to allow players to execute commands on themselves that they otherwise wouldn’t have permission to, but to do that I would need to temporarily grant the player the appropriate permissions to execute the commands in the book and then revoke them one the execution completes.

@zml is working on some improvements to the permissions API. You could suggest that setting a permission returns a CompletableFuture so you can do actions once the action is completed