Allow temporary-add-behaviour to be defined as an argument to the add/set commands (#747)

This commit is contained in:
Luck 2018-02-10 22:04:53 +00:00
parent d14310768c
commit 02ac4bc48b
No known key found for this signature in database
GPG Key ID: EFA9B3EC5FD90F8B
5 changed files with 21 additions and 4 deletions

View File

@ -81,8 +81,8 @@ public class MetaAddTempChatMeta extends SharedSubCommand {
int priority = ArgumentUtils.handlePriority(0, args);
String meta = ArgumentUtils.handleString(1, args);
long duration = ArgumentUtils.handleDuration(2, args);
TemporaryModifier modifier = ArgumentUtils.handleTemporaryModifier(3, args).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
Message.COMMAND_NO_PERMISSION.send(sender);

View File

@ -69,8 +69,8 @@ public class MetaSetTemp extends SharedSubCommand {
String key = args.get(0);
String value = args.get(1);
long duration = ArgumentUtils.handleDuration(2, args);
TemporaryModifier modifier = ArgumentUtils.handleTemporaryModifier(3, args).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
Message.COMMAND_NO_PERMISSION.send(sender);

View File

@ -68,8 +68,8 @@ public class ParentAddTemp extends SharedSubCommand {
String groupName = ArgumentUtils.handleName(0, args);
long duration = ArgumentUtils.handleDuration(1, args);
TemporaryModifier modifier = ArgumentUtils.handleTemporaryModifier(2, args).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
MutableContextSet context = ArgumentUtils.handleContext(2, args, plugin);
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
Group group = plugin.getStorage().loadGroup(groupName).join().orElse(null);
if (group == null) {

View File

@ -69,6 +69,7 @@ public class PermissionSetTemp extends SharedSubCommand {
String node = ArgumentUtils.handleString(0, args);
boolean value = ArgumentUtils.handleBoolean(1, args);
long duration = ArgumentUtils.handleDuration(2, args);
TemporaryModifier modifier = ArgumentUtils.handleTemporaryModifier(3, args).orElseGet(() -> plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR));
MutableContextSet context = ArgumentUtils.handleContext(3, args, plugin);
if (ArgumentPermissions.checkContext(plugin, sender, permission, context)) {
@ -81,7 +82,6 @@ public class PermissionSetTemp extends SharedSubCommand {
return CommandResult.NO_PERMISSION;
}
TemporaryModifier modifier = plugin.getConfiguration().get(ConfigKeys.TEMPORARY_ADD_BEHAVIOUR);
Map.Entry<DataMutateResult, Node> result = holder.setPermission(NodeFactory.builder(node).setValue(value).withExtraContext(context).setExpiry(duration).build(), modifier);
if (result.getKey().asBoolean()) {

View File

@ -29,12 +29,14 @@ import me.lucko.luckperms.api.Contexts;
import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.commands.CommandException;
import me.lucko.luckperms.common.model.TemporaryModifier;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.storage.DataConstraints;
import me.lucko.luckperms.common.utils.DateUtil;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
/**
* Utility class to help process arguments, and throw checked exceptions if the arguments are invalid.
@ -112,6 +114,21 @@ public class ArgumentUtils {
return duration;
}
public static Optional<TemporaryModifier> handleTemporaryModifier(int index, List<String> args) {
if (index < 0 || index >= args.size()) {
return Optional.empty();
}
String s = args.get(index);
try {
Optional<TemporaryModifier> ret = Optional.of(TemporaryModifier.valueOf(s.toUpperCase()));
args.remove(index);
return ret;
} catch (IllegalArgumentException e) {
return Optional.empty();
}
}
public static MutableContextSet handleContext(int fromIndex, List<String> args, LuckPermsPlugin plugin) throws CommandException {
if (args.size() > fromIndex) {
MutableContextSet set = MutableContextSet.create();