mirror of
https://github.com/LuckPerms/LuckPerms.git
synced 2024-12-28 03:57:36 +01:00
Add some more bulk edit commands
This commit is contained in:
parent
4c9abba5fa
commit
b9ee206908
@ -39,6 +39,7 @@ import me.lucko.luckperms.commands.track.DeleteTrack;
|
|||||||
import me.lucko.luckperms.commands.track.ListTracks;
|
import me.lucko.luckperms.commands.track.ListTracks;
|
||||||
import me.lucko.luckperms.commands.track.TrackMainCommand;
|
import me.lucko.luckperms.commands.track.TrackMainCommand;
|
||||||
import me.lucko.luckperms.commands.user.UserMainCommand;
|
import me.lucko.luckperms.commands.user.UserMainCommand;
|
||||||
|
import me.lucko.luckperms.commands.usersbulkedit.UsersBulkEditMainCommand;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
@ -64,6 +65,7 @@ public class CommandManager {
|
|||||||
.add(new ExportCommand())
|
.add(new ExportCommand())
|
||||||
.add(new QueueCommand())
|
.add(new QueueCommand())
|
||||||
.add(new MigrationMainCommand())
|
.add(new MigrationMainCommand())
|
||||||
|
.add(new UsersBulkEditMainCommand())
|
||||||
.add(new CreateGroup())
|
.add(new CreateGroup())
|
||||||
.add(new DeleteGroup())
|
.add(new DeleteGroup())
|
||||||
.add(new ListGroups())
|
.add(new ListGroups())
|
||||||
|
@ -63,6 +63,11 @@ public class UserBulkChange extends SubCommand<User> {
|
|||||||
if (type.equals("world")) {
|
if (type.equals("world")) {
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Node element = iterator.next();
|
Node element = iterator.next();
|
||||||
|
|
||||||
|
if (element.isGroupNode()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String world = element.getWorld().orElse("null");
|
String world = element.getWorld().orElse("null");
|
||||||
if (!world.equals(from)) {
|
if (!world.equals(from)) {
|
||||||
continue;
|
continue;
|
||||||
@ -74,6 +79,11 @@ public class UserBulkChange extends SubCommand<User> {
|
|||||||
} else {
|
} else {
|
||||||
while (iterator.hasNext()) {
|
while (iterator.hasNext()) {
|
||||||
Node element = iterator.next();
|
Node element = iterator.next();
|
||||||
|
|
||||||
|
if (element.isGroupNode()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
String server = element.getServer().orElse("global");
|
String server = element.getServer().orElse("global");
|
||||||
if (!server.equals(from)) {
|
if (!server.equals(from)) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -0,0 +1,94 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||||
|
*
|
||||||
|
* 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.usersbulkedit;
|
||||||
|
|
||||||
|
import com.google.common.collect.ImmutableList;
|
||||||
|
import me.lucko.luckperms.LuckPermsPlugin;
|
||||||
|
import me.lucko.luckperms.commands.MainCommand;
|
||||||
|
import me.lucko.luckperms.commands.Sender;
|
||||||
|
import me.lucko.luckperms.commands.SubCommand;
|
||||||
|
import me.lucko.luckperms.commands.usersbulkedit.subcommands.BulkEditGroup;
|
||||||
|
import me.lucko.luckperms.commands.usersbulkedit.subcommands.BulkEditPermission;
|
||||||
|
import me.lucko.luckperms.storage.Datastore;
|
||||||
|
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
public class UsersBulkEditMainCommand extends MainCommand<Datastore> {
|
||||||
|
|
||||||
|
public UsersBulkEditMainCommand() {
|
||||||
|
super("UsersBulkEdit", "/%s usersbulkedit", 1, ImmutableList.<SubCommand<Datastore>>builder()
|
||||||
|
.add(new BulkEditGroup())
|
||||||
|
.add(new BulkEditPermission())
|
||||||
|
.build()
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected Datastore getTarget(String target, LuckPermsPlugin plugin, Sender sender) {
|
||||||
|
return plugin.getDatastore();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void cleanup(Datastore datastore, LuckPermsPlugin plugin) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<String> getObjects(LuckPermsPlugin plugin) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected List<String> onTabComplete(Sender sender, List<String> args, LuckPermsPlugin plugin) {
|
||||||
|
final List<SubCommand<Datastore>> subs = getSubCommands().stream()
|
||||||
|
.filter(s -> s.isAuthorized(sender))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
|
||||||
|
if (args.size() <= 1) {
|
||||||
|
if (args.isEmpty() || args.get(0).equalsIgnoreCase("")) {
|
||||||
|
return subs.stream()
|
||||||
|
.map(m -> m.getName().toLowerCase())
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
return subs.stream()
|
||||||
|
.map(m -> m.getName().toLowerCase())
|
||||||
|
.filter(s -> s.toLowerCase().startsWith(args.get(0).toLowerCase()))
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
}
|
||||||
|
|
||||||
|
Optional<SubCommand<Datastore>> o = subs.stream()
|
||||||
|
.filter(s -> s.getName().equalsIgnoreCase(args.get(0)))
|
||||||
|
.limit(1)
|
||||||
|
.findAny();
|
||||||
|
|
||||||
|
if (!o.isPresent()) {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
|
||||||
|
return o.get().onTabComplete(plugin, sender, args.subList(1, args.size()));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,128 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||||
|
*
|
||||||
|
* 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.usersbulkedit.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.storage.Datastore;
|
||||||
|
import me.lucko.luckperms.users.User;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class BulkEditGroup extends SubCommand<Datastore> {
|
||||||
|
public BulkEditGroup() {
|
||||||
|
super("group", "Bulk edit group memberships", "<group|null> <server|world> <from> <to>", Permission.USER_BULKCHANGE,
|
||||||
|
Predicate.not(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Datastore datastore, List<String> args, String label) {
|
||||||
|
String group = args.get(0);
|
||||||
|
String type = args.get(1).toLowerCase();
|
||||||
|
String from = args.get(2);
|
||||||
|
String to = args.get(3);
|
||||||
|
if (to.equals("null")) {
|
||||||
|
to = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!type.equals("world") && !type.equals("server")) {
|
||||||
|
Message.BULK_CHANGE_TYPE_ERROR.send(sender);
|
||||||
|
return CommandResult.FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<UUID> uuids = datastore.getUniqueUsers();
|
||||||
|
|
||||||
|
for (UUID u : uuids) {
|
||||||
|
plugin.getDatastore().loadUser(u, "null");
|
||||||
|
User user = plugin.getUserManager().get(u);
|
||||||
|
if (user == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Node> toAdd = new HashSet<>();
|
||||||
|
Iterator<Node> iterator = user.getNodes().iterator();
|
||||||
|
if (type.equals("world")) {
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Node element = iterator.next();
|
||||||
|
|
||||||
|
if (!element.isGroupNode()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.getGroupName().equals(user.getPrimaryGroup())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!group.equals("null") && !element.getGroupName().equals(group)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
if (!element.isGroupNode()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (element.getGroupName().equals(user.getPrimaryGroup())) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!group.equals("null") && !element.getGroupName().equals(group)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
plugin.getUserManager().cleanup(user);
|
||||||
|
plugin.getDatastore().saveUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
Message.BULK_CHANGE_SUCCESS.send(sender, uuids.size());
|
||||||
|
return CommandResult.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,120 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2016 Lucko (Luck) <luck@lucko.me>
|
||||||
|
*
|
||||||
|
* 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.usersbulkedit.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.storage.Datastore;
|
||||||
|
import me.lucko.luckperms.users.User;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
|
||||||
|
public class BulkEditPermission extends SubCommand<Datastore> {
|
||||||
|
public BulkEditPermission() {
|
||||||
|
super("permission", "Bulk edit permissions", "<node|null> <server|world> <from> <to>", Permission.USER_BULKCHANGE,
|
||||||
|
Predicate.not(4));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, Datastore datastore, List<String> args, String label) {
|
||||||
|
String node = args.get(0);
|
||||||
|
String type = args.get(1).toLowerCase();
|
||||||
|
String from = args.get(2);
|
||||||
|
String to = args.get(3);
|
||||||
|
if (to.equals("null")) {
|
||||||
|
to = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!type.equals("world") && !type.equals("server")) {
|
||||||
|
Message.BULK_CHANGE_TYPE_ERROR.send(sender);
|
||||||
|
return CommandResult.FAILURE;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<UUID> uuids = datastore.getUniqueUsers();
|
||||||
|
|
||||||
|
for (UUID u : uuids) {
|
||||||
|
plugin.getDatastore().loadUser(u, "null");
|
||||||
|
User user = plugin.getUserManager().get(u);
|
||||||
|
if (user == null) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
Set<Node> toAdd = new HashSet<>();
|
||||||
|
Iterator<Node> iterator = user.getNodes().iterator();
|
||||||
|
if (type.equals("world")) {
|
||||||
|
while (iterator.hasNext()) {
|
||||||
|
Node element = iterator.next();
|
||||||
|
|
||||||
|
if (element.isGroupNode()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!node.equals("null") && !element.getPermission().equals(node)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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();
|
||||||
|
|
||||||
|
if (element.isGroupNode()) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!node.equals("null") && !element.getPermission().equals(node)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
plugin.getUserManager().cleanup(user);
|
||||||
|
plugin.getDatastore().saveUser(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
Message.BULK_CHANGE_SUCCESS.send(sender, uuids.size());
|
||||||
|
return CommandResult.SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
@ -74,10 +74,13 @@ public abstract class UserManager extends AbstractManager<UUID, User> {
|
|||||||
*/
|
*/
|
||||||
public void giveDefaultIfNeeded(User user, boolean save) {
|
public void giveDefaultIfNeeded(User user, boolean save) {
|
||||||
boolean hasGroup = false;
|
boolean hasGroup = false;
|
||||||
for (Node node : user.getPermissions(false)) {
|
|
||||||
if (node.isGroupNode()) {
|
if (user.getPrimaryGroup() != null && !user.getPrimaryGroup().isEmpty()) {
|
||||||
hasGroup = true;
|
for (Node node : user.getPermissions(false)) {
|
||||||
break;
|
if (node.isGroupNode()) {
|
||||||
|
hasGroup = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user