-
-
Notifications
You must be signed in to change notification settings - Fork 49
LuckPerms Command #456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MC-Samuel
wants to merge
2
commits into
DenizenScript:master
Choose a base branch
from
MC-Samuel:feature/luckperms_command
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
LuckPerms Command #456
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
143 changes: 143 additions & 0 deletions
143
src/main/java/com/denizenscript/depenizen/bukkit/commands/luckperms/LuckPermsCommand.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| package com.denizenscript.depenizen.bukkit.commands.luckperms; | ||
|
|
||
| import com.denizenscript.denizen.objects.PlayerTag; | ||
| import com.denizenscript.denizencore.exceptions.InvalidArgumentsRuntimeException; | ||
| import com.denizenscript.denizencore.objects.ObjectTag; | ||
| import com.denizenscript.denizencore.objects.core.*; | ||
| import com.denizenscript.denizencore.scripts.ScriptEntry; | ||
| import com.denizenscript.denizencore.scripts.commands.AbstractCommand; | ||
| import com.denizenscript.denizencore.scripts.commands.generator.*; | ||
| import com.denizenscript.denizencore.utilities.debugging.Debug; | ||
| import com.denizenscript.denizencore.utilities.text.StringHolder; | ||
| import com.denizenscript.depenizen.bukkit.bridges.LuckPermsBridge; | ||
| import com.denizenscript.depenizen.bukkit.objects.luckperms.LuckPermsGroupTag; | ||
| import net.luckperms.api.model.user.User; | ||
| import net.luckperms.api.node.Node; | ||
| import net.luckperms.api.node.NodeBuilder; | ||
|
|
||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class LuckPermsCommand extends AbstractCommand { | ||
|
|
||
| public LuckPermsCommand() { | ||
| setName("luckperms"); | ||
| setSyntax("luckperms ({set}/unset) [users:<player>|.../groups:<group>|...] [permission] (state:{true}/false) (duration:<duration>) (contexts:<map>)"); | ||
| setRequiredArguments(2, 6); | ||
| autoCompile(); | ||
| } | ||
|
|
||
| // <--[command] | ||
| // @Name mcMMO | ||
| // @Syntax luckperms ({set}/unset) [users:<player>|.../groups:<group>|...] [permission] (state:{true}/false) (duration:<duration>) (contexts:<map>) | ||
| // @Group Depenizen | ||
| // @Plugin Depenizen, mcMMO | ||
| // @Required 2 | ||
| // @Maximum 6 | ||
| // @Short Edits LuckPerms permissions. | ||
| // | ||
| // @Description | ||
| // This command allows you to add or unset permission nodes from players and LuckPerms groups. | ||
| // | ||
| // The 'duration' argument specifies how long a player or group will have the specified permission | ||
| // before it is removed. Not using this argument will result in the permission staying until manually removed. | ||
| // | ||
| // The 'contexts' argument sets the contexts in which the permission and state specified is active. | ||
| // Format is in MapTag format: 'type=value'. | ||
| // The 'value' accepts a ListTag for multiple entries of the same key. | ||
| // | ||
| // @Tags | ||
| // <PlayerTag.has_permission[<permission.node>]> | ||
| // <PlayerTag.luckperms_permission_expiration[<permission.node>]> | ||
| // <LuckPermsGroupTag.has_permission[<permission.node>]> | ||
| // <LuckPermsGroupTag.permission_expiration[<permission.node>]> | ||
| // | ||
| // @Usage | ||
| // Use to give two players the 'dscript.help' permission. | ||
| // - luckperms set users:<[player]>|<[other_player]> dscript.help state:true | ||
| // | ||
| // @Usage | ||
| // Use to give a player the 'dscript.warp' permission for 5 minutes in the main world. | ||
| // - luckperms users:<player> dscript.warp duration:5m contexts:[world=world] | ||
| // | ||
| // @Usage | ||
| // Use to disallow two groups access to the 'dscript.spawn' permission in the nether and end. | ||
| // - luckperms groups:<[group]>|<[other_group]> dscript.spawn state:false contexts:[dimension_type=the_nether|the_end] | ||
| // | ||
| // @Usage | ||
| // Use to unset the 'dscript.ban' permission back to the default for a player when in survival mode on the hub server. | ||
| // - luckperms unset users:<player> dscript.ban contexts:[gamemode=survival;server=hub] | ||
| // | ||
| // --> | ||
|
|
||
| public enum Action {SET, UNSET} | ||
|
|
||
| public static void autoExecute(ScriptEntry scriptEntry, | ||
| @ArgName("action") @ArgDefaultText("set") Action action, | ||
| @ArgName("user") @ArgPrefixed @ArgDefaultNull @ArgSubType(PlayerTag.class) List<PlayerTag> players, | ||
| @ArgName("group") @ArgPrefixed @ArgDefaultNull @ArgSubType(LuckPermsGroupTag.class) List<LuckPermsGroupTag> groups, | ||
| @ArgName("permission") @ArgLinear String permission, | ||
| @ArgName("state") @ArgPrefixed @ArgDefaultText("true") boolean state, | ||
| @ArgName("duration") @ArgPrefixed @ArgDefaultNull DurationTag duration, | ||
| @ArgName("contexts") @ArgPrefixed @ArgDefaultNull MapTag contexts) { | ||
| if (players == null && groups == null) { | ||
| throw new InvalidArgumentsRuntimeException("Must specify either players or groups!"); | ||
| } | ||
| if (players != null && groups != null) { | ||
| throw new InvalidArgumentsRuntimeException("Cannot specify both players and groups!"); | ||
| } | ||
| NodeBuilder<?, ?> nodeBuilder = Node.builder(permission).value(state); | ||
| if (duration != null) { | ||
| nodeBuilder = nodeBuilder.expiry((long) duration.getSeconds(), TimeUnit.SECONDS); | ||
| } | ||
| if (contexts != null) { | ||
| for (Map.Entry<StringHolder, ObjectTag> entry : contexts.entrySet()) { | ||
| for (String value : entry.getValue().asType(ListTag.class, scriptEntry.context)) { | ||
| nodeBuilder = nodeBuilder.withContext(entry.getKey().low, value); | ||
| } | ||
| } | ||
| } | ||
| Node node = nodeBuilder.build(); | ||
| switch (action) { | ||
| case SET: { | ||
| if (players != null) { | ||
| for (PlayerTag player : players) { | ||
| User user = LuckPermsBridge.luckPermsInstance.getUserManager().getUser(player.getUUID()); | ||
| if (user == null) { | ||
| Debug.echoError("User " + player.getUUID() + " does not exist, have they joined the server before?"); | ||
| continue; | ||
| } | ||
| user.data().add(node); | ||
| LuckPermsBridge.luckPermsInstance.getUserManager().saveUser(user); | ||
| } | ||
| } | ||
| else { | ||
| for (LuckPermsGroupTag group : groups) { | ||
| group.getGroup().data().add(node); | ||
| LuckPermsBridge.luckPermsInstance.getGroupManager().saveGroup(group.getGroup()); | ||
| } | ||
| } | ||
| } | ||
| case UNSET: { | ||
| if (players != null) { | ||
| for (PlayerTag player : players) { | ||
| User user = LuckPermsBridge.luckPermsInstance.getUserManager().getUser(player.getUUID()); | ||
| if (user == null) { | ||
| Debug.echoError("User " + player.getUUID() + " does not exist, have they joined the server before?"); | ||
| continue; | ||
| } | ||
| user.data().remove(node); | ||
| LuckPermsBridge.luckPermsInstance.getUserManager().saveUser(user); | ||
| } | ||
| } | ||
| else { | ||
| for (LuckPermsGroupTag group : groups) { | ||
| group.getGroup().data().remove(node); | ||
| LuckPermsBridge.luckPermsInstance.getGroupManager().saveGroup(group.getGroup()); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we could use that new switch without needing to
breakit