use of me.lucko.luckperms.common.bulkupdate.BulkUpdateBuilder in project LuckPerms by lucko.
the class BulkUpdateCommand method execute.
@Override
public CommandResult execute(LuckPermsPlugin plugin, Sender sender, List<String> args, String label) throws CommandException {
if (args.size() == 2 && args.get(0).equalsIgnoreCase("confirm")) {
String id = args.get(1);
BulkUpdate operation = this.pendingOperations.asMap().remove(id);
if (operation == null) {
Message.BULK_UPDATE_UNKNOWN_ID.send(sender, id);
return CommandResult.INVALID_ARGS;
}
Message.BULK_UPDATE_STARTING.send(sender);
plugin.getStorage().applyBulkUpdate(operation).whenCompleteAsync((v, ex) -> {
if (ex == null) {
plugin.getUpdateTaskBuffer().requestDirectly();
Message.BULK_UPDATE_SUCCESS.send(sender);
} else {
ex.printStackTrace();
Message.BULK_UPDATE_FAILURE.send(sender);
}
}, plugin.getBootstrap().getScheduler().async());
return CommandResult.SUCCESS;
}
if (args.size() < 2) {
throw new ArgumentParser.DetailedUsageException();
}
BulkUpdateBuilder bulkUpdateBuilder = BulkUpdateBuilder.create();
try {
bulkUpdateBuilder.dataType(DataType.valueOf(args.remove(0).toUpperCase()));
} catch (IllegalArgumentException e) {
Message.BULK_UPDATE_INVALID_DATA_TYPE.send(sender);
return CommandResult.INVALID_ARGS;
}
String action = args.remove(0).toLowerCase();
switch(action) {
case "delete":
bulkUpdateBuilder.action(DeleteAction.create());
break;
case "update":
if (args.size() < 2) {
throw new ArgumentParser.DetailedUsageException();
}
String field = args.remove(0);
QueryField queryField = QueryField.of(field);
if (queryField == null) {
throw new ArgumentParser.DetailedUsageException();
}
String value = args.remove(0);
bulkUpdateBuilder.action(UpdateAction.of(queryField, value));
break;
default:
throw new ArgumentParser.DetailedUsageException();
}
for (String constraint : args) {
String[] parts = constraint.split(" ");
if (parts.length != 3) {
Message.BULK_UPDATE_INVALID_CONSTRAINT.send(sender, constraint);
return CommandResult.INVALID_ARGS;
}
QueryField field = QueryField.of(parts[0]);
if (field == null) {
Message.BULK_UPDATE_INVALID_CONSTRAINT.send(sender, constraint);
return CommandResult.INVALID_ARGS;
}
Comparison comparison = StandardComparison.parseComparison(parts[1]);
if (comparison == null) {
Message.BULK_UPDATE_INVALID_COMPARISON.send(sender, parts[1]);
return CommandResult.INVALID_ARGS;
}
String expr = parts[2];
bulkUpdateBuilder.constraint(Constraint.of(field, comparison, expr));
}
String id = String.format("%04d", ThreadLocalRandom.current().nextInt(10000));
BulkUpdate bulkUpdate = bulkUpdateBuilder.build();
this.pendingOperations.put(id, bulkUpdate);
Message.BULK_UPDATE_QUEUED.send(sender, bulkUpdate.buildAsSql().replace("{table}", bulkUpdate.getDataType().getName()));
Message.BULK_UPDATE_CONFIRM.send(sender, label, id);
return CommandResult.SUCCESS;
}
Aggregations