Allow creategroup command to take weight and display name as optional arguments (#2698)

This commit is contained in:
Federico López 2020-11-02 06:48:02 -03:00 committed by GitHub
parent 5d0c578da1
commit 5bdc798237
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 5 deletions

View File

@ -64,7 +64,11 @@ luckperms {
install;
}
creategroup {
name brigadier:string single_word;
name brigadier:string single_word {
weight brigadier:integer {
displayname brigadier:string quotable_phrase;
}
}
}
deletegroup {
name brigadier:string single_word;
@ -631,4 +635,4 @@ luckperms {
}
}
}
}
}

View File

@ -94,7 +94,9 @@ public enum CommandSpec {
),
CREATE_GROUP("/%s creategroup <group>",
arg("name", true)
arg("name", true),
arg("weight", false),
arg("display-name", "displayname", false)
),
DELETE_GROUP("/%s deletegroup <group>",
arg("name", true)

View File

@ -30,8 +30,12 @@ import me.lucko.luckperms.common.command.CommandResult;
import me.lucko.luckperms.common.command.abstraction.SingleCommand;
import me.lucko.luckperms.common.command.access.CommandPermission;
import me.lucko.luckperms.common.command.spec.CommandSpec;
import me.lucko.luckperms.common.command.utils.ArgumentException;
import me.lucko.luckperms.common.command.utils.ArgumentList;
import me.lucko.luckperms.common.locale.Message;
import me.lucko.luckperms.common.model.Group;
import me.lucko.luckperms.common.node.types.DisplayName;
import me.lucko.luckperms.common.node.types.Weight;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.sender.Sender;
import me.lucko.luckperms.common.storage.misc.DataConstraints;
@ -40,10 +44,11 @@ import me.lucko.luckperms.common.util.Predicates;
import net.kyori.adventure.text.Component;
import net.luckperms.api.actionlog.Action;
import net.luckperms.api.event.cause.CreationCause;
import net.luckperms.api.model.data.DataType;
public class CreateGroup extends SingleCommand {
public CreateGroup() {
super(CommandSpec.CREATE_GROUP, "CreateGroup", CommandPermission.CREATE_GROUP, Predicates.not(1));
super(CommandSpec.CREATE_GROUP, "CreateGroup", CommandPermission.CREATE_GROUP, Predicates.notInRange(1, 3));
}
@Override
@ -64,8 +69,32 @@ public class CreateGroup extends SingleCommand {
return CommandResult.INVALID_ARGS;
}
Integer weight = null;
try {
plugin.getStorage().createAndLoadGroup(groupName, CreationCause.COMMAND).get();
weight = args.getPriority(1);
} catch (ArgumentException | IndexOutOfBoundsException e) {
// ignored
}
String displayName = null;
try {
displayName = args.get(weight != null ? 2 : 1);
} catch (IndexOutOfBoundsException e) {
// ignored
}
try {
Group group = plugin.getStorage().createAndLoadGroup(groupName, CreationCause.COMMAND).get();
if (weight != null) {
group.setNode(DataType.NORMAL, Weight.builder(weight).build(), false);
}
if (displayName != null) {
group.setNode(DataType.NORMAL, DisplayName.builder(displayName).build(), false);
}
plugin.getStorage().saveGroup(group);
} catch (Exception e) {
plugin.getLogger().warn("Error whilst creating group", e);
Message.CREATE_ERROR.send(sender, Component.text(groupName));

View File

@ -388,6 +388,8 @@ luckperms.usage.apply-edits.argument.code=the unique code for the data
luckperms.usage.apply-edits.argument.target=who to apply the data to
luckperms.usage.create-group.description=Create a new group
luckperms.usage.create-group.argument.name=the name of the group
luckperms.usage.create-group.argument.weight=the weight of the group
luckperms.usage.create-group.argument.display-name=the display name of the group
luckperms.usage.delete-group.description=Delete a group
luckperms.usage.delete-group.argument.name=the name of the group
luckperms.usage.list-groups.description=List all groups on the platform