Add type argument to 'meta clear' command (#457)

This commit is contained in:
Luck
2017-09-04 17:25:59 +01:00
Unverified
parent 754c7d8e3b
commit b6e1fd179e
7 changed files with 168 additions and 55 deletions
@@ -43,6 +43,7 @@ import me.lucko.luckperms.api.context.ImmutableContextSet;
import me.lucko.luckperms.api.context.MutableContextSet;
import me.lucko.luckperms.common.contexts.ExtractedContexts;
import me.lucko.luckperms.common.model.User;
import me.lucko.luckperms.common.node.MetaType;
import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.exceptions.ObjectAlreadyHasException;
import me.lucko.luckperms.exceptions.ObjectLacksException;
@@ -381,12 +382,12 @@ public class PermissionHolderDelegate implements PermissionHolder {
@Override
public void clearMeta() {
handle.clearMeta();
handle.clearMeta(MetaType.ANY);
}
@Override
public void clearMeta(@NonNull ContextSet contextSet) {
handle.clearMeta(contextSet);
handle.clearMeta(MetaType.ANY, contextSet);
}
@Override
@@ -396,7 +397,7 @@ public class PermissionHolderDelegate implements PermissionHolder {
set.add("server", server);
}
handle.clearMeta(set);
handle.clearMeta(MetaType.ANY, set);
}
@Override
@@ -409,7 +410,7 @@ public class PermissionHolderDelegate implements PermissionHolder {
set.add("world", world);
}
handle.clearMeta(set);
handle.clearMeta(MetaType.ANY, set);
}
@Override
@@ -39,6 +39,7 @@ import me.lucko.luckperms.common.locale.CommandSpec;
import me.lucko.luckperms.common.locale.LocaleManager;
import me.lucko.luckperms.common.locale.Message;
import me.lucko.luckperms.common.model.PermissionHolder;
import me.lucko.luckperms.common.node.MetaType;
import me.lucko.luckperms.common.plugin.LuckPermsPlugin;
import me.lucko.luckperms.common.utils.Predicates;
@@ -57,6 +58,34 @@ public class MetaClear extends SharedSubCommand {
return CommandResult.NO_PERMISSION;
}
MetaType type = null;
if (args.size() > 0) {
String typeId = args.get(0).toLowerCase();
if (typeId.equals("any") || typeId.equals("all") || typeId.equals("*")) {
type = MetaType.ANY;
}
if (typeId.equals("chat") || typeId.equals("chatmeta")) {
type = MetaType.CHAT;
}
if (typeId.equals("meta")) {
type = MetaType.META;
}
if (typeId.equals("prefix") || typeId.equals("prefixes")) {
type = MetaType.PREFIX;
}
if (typeId.equals("suffix") || typeId.equals("suffixes")) {
type = MetaType.SUFFIX;
}
if (type != null) {
args.remove(0);
}
}
if (type == null) {
type = MetaType.ANY;
}
int before = holder.getEnduringNodes().size();
MutableContextSet context = ArgumentUtils.handleContext(0, args, plugin);
@@ -67,16 +96,16 @@ public class MetaClear extends SharedSubCommand {
}
if (context.isEmpty()) {
holder.clearMeta();
holder.clearMeta(type);
} else {
holder.clearMeta(context);
holder.clearMeta(type, context);
}
int changed = before - holder.getEnduringNodes().size();
if (changed == 1) {
Message.META_CLEAR_SUCCESS_SINGULAR.send(sender, holder.getFriendlyName(), Util.contextSetToString(context), changed);
Message.META_CLEAR_SUCCESS_SINGULAR.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), Util.contextSetToString(context), changed);
} else {
Message.META_CLEAR_SUCCESS.send(sender, holder.getFriendlyName(), Util.contextSetToString(context), changed);
Message.META_CLEAR_SUCCESS.send(sender, holder.getFriendlyName(), type.name().toLowerCase(), Util.contextSetToString(context), changed);
}
ExtendedLogEntry.build().actor(sender).acted(holder)
@@ -368,8 +368,9 @@ public enum CommandSpec {
Arg.create("context...", false, "the contexts to remove the suffix in")
)
),
META_CLEAR("Clears all chat meta",
META_CLEAR("Clears all meta",
Arg.list(
Arg.create("type", false, "the type of meta to remove"),
Arg.create("context...", false, "the contexts to filter by")
)
),
@@ -230,14 +230,14 @@ public enum Message {
CLEAR_SUCCESS("&b{0}&a's permissions were cleared in context {1}&a. (&b{2}&a nodes were removed.)", true),
CLEAR_SUCCESS_SINGULAR("&b{0}&a's permissions were cleared in context {1}&a. (&b{2}&a node was removed.)", true),
PARENT_CLEAR_SUCCESS("&b{0}&a's parents were cleared in context {1}&a. &b{2}&a nodes were removed.)", true),
PARENT_CLEAR_SUCCESS_SINGULAR("&b{0}&a's parents were cleared in context {1}&a. &b{2}&a node was removed.)", true),
PARENT_CLEAR_SUCCESS("&b{0}&a's parents were cleared in context {1}&a. (&b{2}&a nodes were removed.)", true),
PARENT_CLEAR_SUCCESS_SINGULAR("&b{0}&a's parents were cleared in context {1}&a. (&b{2}&a node was removed.)", true),
PARENT_CLEAR_TRACK_SUCCESS("&b{0}&a's parents on track &b{1}&a were cleared in context {2}&a. &b{3}&a nodes were removed.)", true),
PARENT_CLEAR_TRACK_SUCCESS_SINGULAR("&b{0}&a's parents on track &b{1}&a were cleared in context {2}&a. &b{3}&a node was removed.)", true),
PARENT_CLEAR_TRACK_SUCCESS("&b{0}&a's parents on track &b{1}&a were cleared in context {2}&a. (&b{3}&a nodes were removed.)", true),
PARENT_CLEAR_TRACK_SUCCESS_SINGULAR("&b{0}&a's parents on track &b{1}&a were cleared in context {2}&a. (&b{3}&a node was removed.)", true),
META_CLEAR_SUCCESS("&b{0}&a's meta was cleared in context {1}&a. &b{2}&a nodes were removed.)", true),
META_CLEAR_SUCCESS_SINGULAR("&b{0}&a's meta was cleared in context {1}&a. &b{2}&a node was removed.)", true),
META_CLEAR_SUCCESS("&b{0}&a's meta matching type &b{1}&a was cleared in context {2}&a. (&b{3}&a nodes were removed.)", true),
META_CLEAR_SUCCESS_SINGULAR("&b{0}&a's meta matching type &b{1}&a was cleared in context {2}&a. (&b{3}&a node was removed.)", true),
ILLEGAL_DATE_ERROR("Could not parse date '{0}'.", true),
PAST_DATE_ERROR("You cannot set a date in the past!", true),
@@ -53,6 +53,7 @@ import me.lucko.luckperms.common.contexts.ContextSetComparator;
import me.lucko.luckperms.common.contexts.ExtractedContexts;
import me.lucko.luckperms.common.node.ImmutableLocalizedNode;
import me.lucko.luckperms.common.node.InheritanceInfo;
import me.lucko.luckperms.common.node.MetaType;
import me.lucko.luckperms.common.node.NodeComparator;
import me.lucko.luckperms.common.node.NodeFactory;
import me.lucko.luckperms.common.node.NodeTools;
@@ -1361,12 +1362,12 @@ public abstract class PermissionHolder {
return true;
}
public boolean clearMeta() {
public boolean clearMeta(MetaType type) {
ImmutableCollection<Node> before = getEnduringNodes().values();
nodesLock.lock();
try {
if (!nodes.values().removeIf(n -> n.isMeta() || n.isPrefix() || n.isSuffix())) {
if (!nodes.values().removeIf(type::matches)) {
return false;
}
} finally {
@@ -1379,7 +1380,7 @@ public abstract class PermissionHolder {
return true;
}
public boolean clearMeta(ContextSet contextSet) {
public boolean clearMeta(MetaType type, ContextSet contextSet) {
ImmutableCollection<Node> before = getEnduringNodes().values();
nodesLock.lock();
@@ -1389,7 +1390,7 @@ public abstract class PermissionHolder {
return false;
}
boolean b = nodes.removeIf(n -> n.isMeta() || n.isPrefix() || n.isSuffix());
boolean b = nodes.removeIf(type::matches);
if (!b) {
return false;
}
@@ -0,0 +1,93 @@
/*
* This file is part of LuckPerms, licensed under the MIT License.
*
* Copyright (c) lucko (Luck) <luck@lucko.me>
* Copyright (c) contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package me.lucko.luckperms.common.node;
import me.lucko.luckperms.api.Node;
/**
* Represents a type of meta
*/
public enum MetaType {
/**
* Represents any meta type
*/
ANY {
@Override
public boolean matches(Node node) {
return META.matches(node) || PREFIX.matches(node) || SUFFIX.matches(node);
}
},
/**
* Represents any chat meta type
*/
CHAT {
@Override
public boolean matches(Node node) {
return PREFIX.matches(node) || SUFFIX.matches(node);
}
},
/**
* Represents a meta key-value pair
*/
META {
@Override
public boolean matches(Node node) {
return node.isMeta();
}
},
/**
* Represents a prefix
*/
PREFIX {
@Override
public boolean matches(Node node) {
return node.isPrefix();
}
},
/**
* Represents a suffix
*/
SUFFIX {
@Override
public boolean matches(Node node) {
return node.isSuffix();
}
};
/**
* Returns if the passed node matches the type
*
* @param node the node to test
* @return true if the node has the same type
*/
public abstract boolean matches(Node node);
}