From b6e1fd179e7423b3c01ece7bcf76e2c86b09deaa Mon Sep 17 00:00:00 2001 From: Luck Date: Mon, 4 Sep 2017 17:25:59 +0100 Subject: [PATCH] Add type argument to 'meta clear' command (#457) --- .locale/en_US.yml | 60 +++++------- .../delegates/PermissionHolderDelegate.java | 9 +- .../commands/impl/generic/meta/MetaClear.java | 37 +++++++- .../luckperms/common/locale/CommandSpec.java | 3 +- .../luckperms/common/locale/Message.java | 12 +-- .../common/model/PermissionHolder.java | 9 +- .../lucko/luckperms/common/node/MetaType.java | 93 +++++++++++++++++++ 7 files changed, 168 insertions(+), 55 deletions(-) create mode 100644 common/src/main/java/me/lucko/luckperms/common/node/MetaType.java diff --git a/.locale/en_US.yml b/.locale/en_US.yml index 8c436a572..b21ea0893 100644 --- a/.locale/en_US.yml +++ b/.locale/en_US.yml @@ -26,13 +26,10 @@ already-has-temp-permission: "{0} already has this permission set temporarily!" does-not-have-temp-permission: "{0} does not have this permission set temporarily." user-not-found: "&bUser could not be found." user-not-online: "&bUser &a{0}&b is not online." -user-save-success: "&7(User data was saved to storage)" user-save-error: "There was an error whilst saving the user." group-not-found: "&bGroup could not be found." -group-save-success: "&7(Group data was saved to storage)" group-save-error: "There was an error whilst saving the group." track-not-found: "&bTrack could not be found." -track-save-success: "&7(Track data was saved to storage)" track-save-error: "There was an error whilst saving the track." user-invalid-entry: "&d{0}&c is not a valid username/uuid." group-invalid-entry: "Group names can only contain alphanumeric characters." @@ -95,34 +92,22 @@ update-task-push-success: "&aOther servers were notified via &b{0} Messaging &as update-task-push-failure: "&cError whilst pushing changes to other servers." update-task-push-failure-not-setup: "&cError whilst pushing changes to other servers. A messaging service has not been configured." reload-config-success: "&aThe configuration file was reloaded. &7(some options will only apply after the server has restarted.)" -info: > +info-top: > {PREFIX}&2Running &bLuckPerms v{0}&2 by &bLuck&2.\n {PREFIX}&f- &3Platform: &f{1}\n - {PREFIX}&f- &3Storage Method: &f{2}\n - {PREFIX}&f- &3Server Name: &f{3}\n - {PREFIX}&f- &3Sync Interval: &a{4} &fminutes\n - {PREFIX}&f- &3Messaging Service: &f{5}\n - {PREFIX}&f- &bCounts:\n - {PREFIX}&f- &3Online Players: &a{6}\n - {PREFIX}&f- &3Loaded Users: &a{7}\n - {PREFIX}&f- &3Loaded Groups: &a{8}\n - {PREFIX}&f- &3Loaded Tracks: &a{9}\n - {PREFIX}&f- &3Log size: &a{10}\n - {PREFIX}&f- &3UUID Cache size: &a{11}\n - {PREFIX}&f- &3Translations loaded: &a{12}\n - {PREFIX}&f- &3Pre-process contexts: &a{13}\n - {PREFIX}&f- &3Context Calculators: &a{14}\n - {PREFIX}&f- &3Unique permissions: &a{15}\n - {PREFIX}&f- &bConfiguration:\n - {PREFIX}&f- &3Use Server UUIDs: {16}\n - {PREFIX}&f- &bPermission Calculation:\n - {PREFIX}&f- &3Including Global: {17}\n - {PREFIX}&f- &3Including Global World: {18}\n - {PREFIX}&f- &3Applying Global Groups: {19}\n - {PREFIX}&f- &3Applying Global World Groups: {20}\n - {PREFIX}&f- &3Applying Wildcards: {21}\n - {PREFIX}&f- &3Applying Regex: {22}\n - {PREFIX}&f- &3Applying Shorthand: {23} + {PREFIX}&f- &3Server Brand: &f{2}\n + {PREFIX}&f- &3Server Version: &f{3} +info-middle: > + {PREFIX}&f- &bMessaging Type: &f{0}\n + {PREFIX}&f- &bInstance:\n + {PREFIX}&f- &3Server Name: &f{1}\n + {PREFIX}&f- &3Online Players: &a{2}\n + {PREFIX}&f- &3Unique Connections: &a{3}\n + {PREFIX}&f- &3Uptime: &7{4}\n + {PREFIX}&f- &3Local Data: &a{5} &7users, &a{6} &7groups, &a{7} &7tracks\n + {PREFIX}&f- &3Context Calculators: &a{8}\n + {PREFIX}&f- &3Known permissions: &a{9}\n + {PREFIX}&f- &3Active processors: &7{10} create-group-error: "There was an error whilst creating the group." delete-group-error: "There was an error whilst deleting the group." delete-group-error-default: "You cannot delete the default group." @@ -147,6 +132,7 @@ context-pair--global-inline: "&eglobal" context-pair-sep: "&a, " context-pair: "&8(&7{0}=&f{1}&8)" check-permission: "&b{0}&a has permission &b{1}&a set to {2}&a in context {3}&a." +check-inherits-permission: "&b{0}&a has permission &b{1}&a set to {2}&a in context {3}&a. &7(inherited from &a{4}&7)" setpermission-success: "&aSet &b{0}&a to &b{1}&a for &b{2}&a in context {3}&a." setpermission-temp-success: "&aSet &b{0}&a to &b{1}&a for &b{2}&a for a duration of &b{3}&a in context {4}&a." unsetpermission-success: "&aUnset &b{0}&a for &b{1}&a in context {2}&a." @@ -159,12 +145,12 @@ unset-inherit-success: "&b{0}&a no longer inherits permissions from &b{1}&a in c unset-temp-inherit-success: "&b{0}&a no longer temporarily inherits permissions from &b{1}&a in context {2}&a." clear-success: "&b{0}&a's permissions were cleared in context {1}&a. (&b{2}&a nodes were removed.)" clear-success-singular: "&b{0}&a's permissions were cleared in context {1}&a. (&b{2}&a node was removed.)" -parent-clear-success: "&b{0}&a's parents were cleared in context {1}&a. &b{2}&a nodes were removed.)" -parent-clear-success-singular: "&b{0}&a's parents were cleared in context {1}&a. &b{2}&a node was removed.)" -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.)" -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.)" -meta-clear-success: "&b{0}&a's meta was cleared in context {1}&a. &b{2}&a nodes were removed.)" -meta-clear-success-singular: "&b{0}&a's meta was cleared in context {1}&a. &b{2}&a node was removed.)" +parent-clear-success: "&b{0}&a's parents were cleared in context {1}&a. (&b{2}&a nodes were removed.)" +parent-clear-success-singular: "&b{0}&a's parents were cleared in context {1}&a. (&b{2}&a node was removed.)" +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.)" +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.)" +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.)" +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.)" illegal-date-error: "Could not parse date '{0}'." past-date-error: "You cannot set a date in the past!" chat-meta-prefix-header: "&b{0}'s Prefixes" @@ -261,6 +247,7 @@ log-invalid-page: "Invalid page number." log-invalid-page-range: "Invalid page number. Please enter a value between 1 and {0}." log-no-entries: "&bNo log entries to show." log-entry: "&b#{0} -> &8(&7{1} ago&8) {2}" +log-notify-console: "&cCannot toggle notifications for console." log-notify-toggle-on: "&aEnabled&b logging output." log-notify-toggle-off: "&cDisabled&b logging output." log-notify-already-on: "You are already receiving notifications." @@ -606,8 +593,9 @@ command-specs: "suffix": "the suffix string" "context...": "the contexts to remove the suffix in" meta-clear: - description: "Clears all chat meta" + description: "Clears all meta" args: + "type": "the type of meta to remove" "context...": "the contexts to filter by" track-info: description: "Gives info about the track" diff --git a/common/src/main/java/me/lucko/luckperms/common/api/delegates/PermissionHolderDelegate.java b/common/src/main/java/me/lucko/luckperms/common/api/delegates/PermissionHolderDelegate.java index 8e6d01492..9c0a159ef 100644 --- a/common/src/main/java/me/lucko/luckperms/common/api/delegates/PermissionHolderDelegate.java +++ b/common/src/main/java/me/lucko/luckperms/common/api/delegates/PermissionHolderDelegate.java @@ -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 diff --git a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java index efe21c056..ba27164d8 100644 --- a/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java +++ b/common/src/main/java/me/lucko/luckperms/common/commands/impl/generic/meta/MetaClear.java @@ -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) diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/CommandSpec.java b/common/src/main/java/me/lucko/luckperms/common/locale/CommandSpec.java index a75e64829..dc87ed187 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/CommandSpec.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/CommandSpec.java @@ -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") ) ), diff --git a/common/src/main/java/me/lucko/luckperms/common/locale/Message.java b/common/src/main/java/me/lucko/luckperms/common/locale/Message.java index f48c57a9c..744a166cb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/locale/Message.java +++ b/common/src/main/java/me/lucko/luckperms/common/locale/Message.java @@ -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), diff --git a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java index 7d8bc6253..113987f22 100644 --- a/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java +++ b/common/src/main/java/me/lucko/luckperms/common/model/PermissionHolder.java @@ -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 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 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; } diff --git a/common/src/main/java/me/lucko/luckperms/common/node/MetaType.java b/common/src/main/java/me/lucko/luckperms/common/node/MetaType.java new file mode 100644 index 000000000..0a8f02ced --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/common/node/MetaType.java @@ -0,0 +1,93 @@ +/* + * This file is part of LuckPerms, licensed under the MIT License. + * + * Copyright (c) lucko (Luck) + * 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); + +}