diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java index 3859a446..5b11a14d 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/group/GroupMainCommand.java @@ -60,6 +60,7 @@ public class GroupMainCommand extends MainCommand { .add(new GroupAddTempSuffix()) .add(new GroupRemoveTempPrefix()) .add(new GroupRemoveTempSuffix()) + .add(new GroupBulkChange()) .add(new GroupClear()) .add(new GroupRename()) .build() diff --git a/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupBulkChange.java b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupBulkChange.java new file mode 100644 index 00000000..80c4a71a --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/group/subcommands/GroupBulkChange.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.commands.group.subcommands; + +import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.commands.CommandResult; +import me.lucko.luckperms.commands.Predicate; +import me.lucko.luckperms.commands.Sender; +import me.lucko.luckperms.commands.SubCommand; +import me.lucko.luckperms.constants.Message; +import me.lucko.luckperms.constants.Permission; +import me.lucko.luckperms.groups.Group; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +public class GroupBulkChange extends SubCommand { + public GroupBulkChange() { + super("bulkchange", "Applies a bulk permission change to the groups permissions", " ", + Permission.GROUP_BULKCHANGE, Predicate.not(3)); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Group group, List args, String label) { + String type = args.get(0).toLowerCase(); + String from = args.get(1); + String to = args.get(2); + if (to.equals("null")) { + to = null; + } + + Set toAdd = new HashSet<>(); + + if (!type.equals("world") && !type.equals("server")) { + Message.BULK_CHANGE_TYPE_ERROR.send(sender); + return CommandResult.FAILURE; + } + + Iterator iterator = group.getNodes().iterator(); + if (type.equals("world")) { + while (iterator.hasNext()) { + Node element = iterator.next(); + String world = element.getWorld().orElse("null"); + if (!world.equals(from)) { + continue; + } + + iterator.remove(); + toAdd.add(me.lucko.luckperms.core.Node.builderFromExisting(element).setWorld(to).build()); + } + } else { + while (iterator.hasNext()) { + Node element = iterator.next(); + String server = element.getServer().orElse("global"); + if (!server.equals(from)) { + continue; + } + + iterator.remove(); + toAdd.add(me.lucko.luckperms.core.Node.builderFromExisting(element).setServer(to).build()); + } + } + + group.getNodes().addAll(toAdd); + save(group, sender, plugin); + Message.BULK_CHANGE_SUCCESS.send(sender, toAdd.size()); + return CommandResult.SUCCESS; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java b/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java index a105a938..2277d0d7 100644 --- a/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java +++ b/common/src/main/java/me/lucko/luckperms/commands/user/UserMainCommand.java @@ -67,6 +67,7 @@ public class UserMainCommand extends MainCommand { .add(new UserAddTempSuffix()) .add(new UserRemoveTempPrefix()) .add(new UserRemoveTempSuffix()) + .add(new UserBulkChange()) .add(new UserClear()) .build() ); diff --git a/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserBulkChange.java b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserBulkChange.java new file mode 100644 index 00000000..e58180f5 --- /dev/null +++ b/common/src/main/java/me/lucko/luckperms/commands/user/subcommands/UserBulkChange.java @@ -0,0 +1,92 @@ +/* + * Copyright (c) 2016 Lucko (Luck) + * + * 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.commands.user.subcommands; + +import me.lucko.luckperms.LuckPermsPlugin; +import me.lucko.luckperms.api.Node; +import me.lucko.luckperms.commands.CommandResult; +import me.lucko.luckperms.commands.Predicate; +import me.lucko.luckperms.commands.Sender; +import me.lucko.luckperms.commands.SubCommand; +import me.lucko.luckperms.constants.Message; +import me.lucko.luckperms.constants.Permission; +import me.lucko.luckperms.users.User; + +import java.util.HashSet; +import java.util.Iterator; +import java.util.List; +import java.util.Set; + +public class UserBulkChange extends SubCommand { + public UserBulkChange() { + super("bulkchange", "Applies a bulk permission change to the users permissions", " ", + Permission.USER_BULKCHANGE, Predicate.not(3)); + } + + @Override + public CommandResult execute(LuckPermsPlugin plugin, Sender sender, User user, List args, String label) { + String type = args.get(0).toLowerCase(); + String from = args.get(1); + String to = args.get(2); + if (to.equals("null")) { + to = null; + } + + Set toAdd = new HashSet<>(); + + if (!type.equals("world") && !type.equals("server")) { + Message.BULK_CHANGE_TYPE_ERROR.send(sender); + return CommandResult.FAILURE; + } + + Iterator iterator = user.getNodes().iterator(); + if (type.equals("world")) { + while (iterator.hasNext()) { + Node element = iterator.next(); + String world = element.getWorld().orElse("null"); + if (!world.equals(from)) { + continue; + } + + iterator.remove(); + toAdd.add(me.lucko.luckperms.core.Node.builderFromExisting(element).setWorld(to).build()); + } + } else { + while (iterator.hasNext()) { + Node element = iterator.next(); + String server = element.getServer().orElse("global"); + if (!server.equals(from)) { + continue; + } + + iterator.remove(); + toAdd.add(me.lucko.luckperms.core.Node.builderFromExisting(element).setServer(to).build()); + } + } + + user.getNodes().addAll(toAdd); + save(user, sender, plugin); + Message.BULK_CHANGE_SUCCESS.send(sender, toAdd.size()); + return CommandResult.SUCCESS; + } +} diff --git a/common/src/main/java/me/lucko/luckperms/constants/Message.java b/common/src/main/java/me/lucko/luckperms/constants/Message.java index 5c2ab197..f4c23b6e 100644 --- a/common/src/main/java/me/lucko/luckperms/constants/Message.java +++ b/common/src/main/java/me/lucko/luckperms/constants/Message.java @@ -214,7 +214,10 @@ public enum Message { REMOVE_TEMP_SUFFIX_SUCCESS("&b{0}&a had temporary suffix &f\"{1}&f\"&a at priority &b{1}&a removed.", true), REMOVE_TEMP_SUFFIX_SERVER_SUCCESS("&b{0}&a had temporary suffix &f\"{1}&f\"&a at priority &b{2}&a removed on server &b{3}&a.", true), REMOVE_TEMP_SUFFIX_SERVER_WORLD_SUCCESS("&b{0}&a had temporary suffix &f\"{1}&f\"&a at priority &b{2}&a removed on server &b{3}&a, world &b{4}&a.", true), - + + BULK_CHANGE_TYPE_ERROR("Invalid type. Was expecting 'server' or 'world'.", true), + BULK_CHANGE_SUCCESS("&aApplied bulk change successfully. {0} records were changed.", true), + USER_INFO( PREFIX + "&d-> &eUser: &6{0}" + "\n" + PREFIX + "&d-> &eUUID: &6{1}" + "\n" + diff --git a/common/src/main/java/me/lucko/luckperms/constants/Permission.java b/common/src/main/java/me/lucko/luckperms/constants/Permission.java index b632c9c9..c59af7f1 100644 --- a/common/src/main/java/me/lucko/luckperms/constants/Permission.java +++ b/common/src/main/java/me/lucko/luckperms/constants/Permission.java @@ -72,6 +72,7 @@ public enum Permission { USER_REMOVE_TEMP_PREFIX("removetempprefix", "user"), USER_ADD_TEMP_SUFFIX("addtempsuffix", "user"), USER_REMOVE_TEMP_SUFFIX("removetempsuffix", "user"), + USER_BULKCHANGE("bulkchange", "user"), USER_CLEAR("clear", "user"), GROUP_INFO("info", "group"), @@ -97,6 +98,7 @@ public enum Permission { GROUP_REMOVE_TEMP_PREFIX("removetempprefix", "group"), GROUP_ADD_TEMP_SUFFIX("addtempsuffix", "group"), GROUP_REMOVE_TEMP_SUFFIX("removetempsuffix", "group"), + GROUP_BULKCHANGE("bulkchange", "group"), GROUP_CLEAR("clear", "group"), GROUP_RENAME("rename", "group"), diff --git a/common/src/main/java/me/lucko/luckperms/core/Node.java b/common/src/main/java/me/lucko/luckperms/core/Node.java index 5c847480..92be1272 100644 --- a/common/src/main/java/me/lucko/luckperms/core/Node.java +++ b/common/src/main/java/me/lucko/luckperms/core/Node.java @@ -87,6 +87,10 @@ public class Node implements me.lucko.luckperms.api.Node { } } + public static me.lucko.luckperms.api.Node.Builder builderFromExisting(me.lucko.luckperms.api.Node other) { + return new Builder(other); + } + @Getter private final String permission; @@ -697,6 +701,15 @@ public class Node implements me.lucko.luckperms.api.Node { } } + Builder(me.lucko.luckperms.api.Node other) { + this.permission = other.getPermission(); + this.value = other.getValue(); + this.override = other.isOverride(); + this.server = other.getServer().orElse(null); + this.world = other.getWorld().orElse(null); + this.expireAt = other.getExpiryUnixTime(); + } + @Override public me.lucko.luckperms.api.Node.Builder setNegated(boolean negated) { value = !negated; diff --git a/default-lang.yml b/default-lang.yml index 8c8bc126..c19a8725 100644 --- a/default-lang.yml +++ b/default-lang.yml @@ -171,6 +171,9 @@ remove-temp-suffix-success: "&b{0}&a had temporary suffix &f\"{1}&f\"&a at prior remove-temp-suffix-server-success: "&b{0}&a had temporary suffix &f\"{1}&f\"&a at priority &b{2}&a removed on server &b{3}&a." remove-temp-suffix-server-world-success: "&b{0}&a had temporary suffix &f\"{1}&f\"&a at priority &b{2}&a removed on server &b{3}&a, world &b{4}&a." +bulk-change-type-error: "Invalid type. Was expecting 'server' or 'world'." +bulk-change-success: "&aApplied bulk change successfully. {0} records were changed." + user-info: > {PREFIX}&d-> &eUser: &6{0}\n {PREFIX}&d-> &eUUID: &6{1}\n