use of me.lucko.luckperms.common.node.types.Inheritance in project LuckPerms by lucko.
the class GroupRename method execute.
@Override
public void execute(LuckPermsPlugin plugin, Sender sender, Group target, ArgumentList args, String label) {
if (ArgumentPermissions.checkModifyPerms(plugin, sender, getPermission().get(), target)) {
Message.COMMAND_NO_PERMISSION.send(sender);
return;
}
String newGroupName = args.get(0).toLowerCase(Locale.ROOT);
if (!DataConstraints.GROUP_NAME_TEST.test(newGroupName)) {
Message.GROUP_INVALID_ENTRY.send(sender, newGroupName);
return;
}
if (plugin.getStorage().loadGroup(newGroupName).join().isPresent()) {
Message.ALREADY_EXISTS.send(sender, newGroupName);
return;
}
Group newGroup;
try {
newGroup = plugin.getStorage().createAndLoadGroup(newGroupName, CreationCause.COMMAND).get();
} catch (Exception e) {
plugin.getLogger().warn("Error whilst creating group", e);
Message.CREATE_ERROR.send(sender, Component.text(newGroupName));
return;
}
try {
plugin.getStorage().deleteGroup(target, DeletionCause.COMMAND).get();
} catch (Exception e) {
plugin.getLogger().warn("Error whilst deleting group", e);
Message.DELETE_ERROR.send(sender, target.getFormattedDisplayName());
return;
}
newGroup.setNodes(DataType.NORMAL, target.normalData().asList(), false);
Message.RENAME_SUCCESS.send(sender, target.getFormattedDisplayName(), newGroup.getFormattedDisplayName());
LoggedAction.build().source(sender).target(target).description("rename", newGroup.getName()).build().submit(plugin, sender);
StorageAssistant.save(newGroup, sender, plugin).thenCompose((v) -> {
if (args.remove("--update-parent-lists")) {
// the group is now renamed, proceed to update its representing inheritance nodes
BulkUpdate operation = BulkUpdateBuilder.create().trackStatistics(false).dataType(me.lucko.luckperms.common.bulkupdate.DataType.ALL).action(UpdateAction.of(QueryField.PERMISSION, Inheritance.key(newGroupName))).query(Query.of(QueryField.PERMISSION, Constraint.of(StandardComparison.EQUAL, Inheritance.key(target.getName())))).build();
return plugin.getStorage().applyBulkUpdate(operation);
} else {
return CompletableFuture.completedFuture(v);
}
}).whenCompleteAsync((v, ex) -> {
if (ex != null) {
ex.printStackTrace();
}
plugin.getSyncTaskBuffer().requestDirectly();
}, plugin.getBootstrap().getScheduler().async());
}
Aggregations