-
-
Notifications
You must be signed in to change notification settings - Fork 50
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
1
commit 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
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
133 changes: 133 additions & 0 deletions
133
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,133 @@ | ||
| 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.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.Map; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| public class LuckPermsCommand extends AbstractCommand { | ||
|
|
||
| public LuckPermsCommand() { | ||
| setName("luckperms"); | ||
| setSyntax("luckperms [{set}/unset] [user:<player>/group:<group>] [permission] (state:{true}/false) (duration:<duration>) (contexts:<map>)"); | ||
| setRequiredArguments(2, 6); | ||
| autoCompile(); | ||
| } | ||
|
|
||
| // <--[command] | ||
| // @Name mcMMO | ||
| // @Syntax luckperms [{set}/unset] [user:<player>/group:<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_expiry[<permission.node>]> | ||
| // <LuckPermsGroupTag.has_permission[<permission.node>]> | ||
| // <LuckPermsGroupTag.permission_expiry[<permission.node>]> | ||
| // | ||
| // @Usage | ||
| // Use to give a player the 'dscript.help' permission. | ||
| // - luckperms set user:<player> dscript.help state:true | ||
| // | ||
| // @Usage | ||
| // Use to give a player the 'dscript.warp' permission for 5 minutes in the main world. | ||
| // - luckperms user:<player> dscript.warp duration:5m contexts:[world=world] | ||
| // | ||
| // @Usage | ||
| // Use to disallow a group access to the 'dscript.spawn' permission in the nether and end. | ||
| // - luckperms group:<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 user:<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 PlayerTag player, | ||
| @ArgName("group") @ArgPrefixed @ArgDefaultNull LuckPermsGroupTag group, | ||
| @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 (player == null && group == null) { | ||
| throw new InvalidArgumentsRuntimeException("Must specify either a player or a group!"); | ||
| } | ||
| if (player != null && group != null) { | ||
| throw new InvalidArgumentsRuntimeException("Cannot specify both a player and a group!"); | ||
| } | ||
| 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: { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we could use that new switch without needing to |
||
| if (player != null) { | ||
| User user = LuckPermsBridge.luckPermsInstance.getUserManager().getUser(player.getUUID()); | ||
| if (user == null) { | ||
| throw new InvalidArgumentsRuntimeException("This user does not exist, have they joined the server before?"); | ||
| } | ||
| user.data().add(node); | ||
| LuckPermsBridge.luckPermsInstance.getUserManager().saveUser(user); | ||
| } | ||
| else { | ||
| group.getGroup().data().add(node); | ||
| LuckPermsBridge.luckPermsInstance.getGroupManager().saveGroup(group.getGroup()); | ||
| } | ||
| break; | ||
| } | ||
| case UNSET: { | ||
| if (player != null) { | ||
| User user = LuckPermsBridge.luckPermsInstance.getUserManager().getUser(player.getUUID()); | ||
| if (user == null) { | ||
| throw new InvalidArgumentsRuntimeException("This user does not exist, have they joined the server before?"); | ||
| } | ||
| user.data().remove(node); | ||
| LuckPermsBridge.luckPermsInstance.getUserManager().saveUser(user); | ||
| } | ||
| else { | ||
| group.getGroup().data().remove(node); | ||
| LuckPermsBridge.luckPermsInstance.getGroupManager().saveGroup(group.getGroup()); | ||
| } | ||
| break; | ||
| } | ||
| } | ||
| } | ||
| } | ||
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 |
|---|---|---|
|
|
@@ -2,12 +2,15 @@ | |
|
|
||
| import com.denizenscript.denizencore.objects.Fetchable; | ||
| import com.denizenscript.denizencore.objects.ObjectTag; | ||
| import com.denizenscript.denizencore.objects.core.DurationTag; | ||
| import com.denizenscript.denizencore.objects.core.ElementTag; | ||
| import com.denizenscript.denizencore.tags.Attribute; | ||
| import com.denizenscript.denizencore.tags.ObjectTagProcessor; | ||
| import com.denizenscript.denizencore.tags.TagContext; | ||
| import com.denizenscript.depenizen.bukkit.bridges.LuckPermsBridge; | ||
| import net.luckperms.api.model.group.Group; | ||
| import net.luckperms.api.node.NodeType; | ||
| import net.luckperms.api.node.types.PermissionNode; | ||
|
|
||
| import java.util.OptionalInt; | ||
|
|
||
|
|
@@ -153,6 +156,37 @@ public static void register() { | |
| tagProcessor.registerTag(ElementTag.class, ElementTag.class, "has_permission", (attribute, object, p) -> { | ||
| return new ElementTag(object.getGroup().getCachedData().getPermissionData().checkPermission(p.asString()).asBoolean()); | ||
| }); | ||
|
|
||
| // <--[tag] | ||
| // @attribute <LuckPermsGroupTag.permission_expiry[<permission.node>]> | ||
| // @returns DurationTag | ||
| // @plugin Depenizen, LuckPerms | ||
| // @description | ||
| // Returns how long a group has a permission for. | ||
| // If the group does not have the specific permission set, this will return the time for the closest wildcard permission, if any. | ||
| // --> | ||
| tagProcessor.registerTag(DurationTag.class, ElementTag.class, "permission_expiry", (attribute, object, p) -> { | ||
| String permission = p.asString(); | ||
| PermissionNode bestNode = null; | ||
| int wildcardLevel = 0; | ||
| for (PermissionNode node : object.getGroup().getNodes(NodeType.PERMISSION)) { | ||
| if (node.getKey().equalsIgnoreCase(permission)) { | ||
| bestNode = node; | ||
| break; | ||
| } | ||
| else if (node.isWildcard() && node.getWildcardLevel().isPresent()) { | ||
| int size = node.getKey().substring(0, node.getKey().length() - 1).length(); | ||
| if (permission.length() < size || !permission.substring(0, size).equalsIgnoreCase(node.getKey().substring(0, size))) { | ||
| continue; | ||
| } | ||
| if (node.getWildcardLevel().getAsInt() > wildcardLevel) { | ||
| bestNode = node; | ||
| wildcardLevel = node.getWildcardLevel().getAsInt(); | ||
| } | ||
| } | ||
| } | ||
| return bestNode != null ? new DurationTag(bestNode.getExpiryDuration() != null ? (int) bestNode.getExpiryDuration().getSeconds() : 0) : null; | ||
|
Comment on lines
+172
to
+188
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could probably extract this to a method since its the same for both tags |
||
| }); | ||
| } | ||
|
|
||
| public static ObjectTagProcessor<LuckPermsGroupTag> tagProcessor = new ObjectTagProcessor<>(); | ||
|
|
||
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 probably make this take
usersandgroupsas ListTag rather than only one value.Which would allow to do easier things like this rather than foreach: