Search in sources :

Example 1 with QueryField

use of me.lucko.luckperms.common.bulkupdate.query.QueryField in project LuckPerms by lucko.

the class BulkUpdateCommand method execute.

@Override
public void execute(LuckPermsPlugin plugin, Sender sender, ArgumentList args, String label) throws CommandException {
    if (plugin.getConfiguration().get(ConfigKeys.DISABLE_BULKUPDATE)) {
        Message.BULK_UPDATE_DISABLED.send(sender);
        return;
    }
    if (!sender.isConsole()) {
        Message.BULK_UPDATE_MUST_USE_CONSOLE.send(sender);
        return;
    }
    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;
        }
        runOperation(operation, plugin, sender);
        return;
    }
    if (args.size() < 2) {
        throw new ArgumentException.DetailedUsage();
    }
    BulkUpdateBuilder bulkUpdateBuilder = BulkUpdateBuilder.create();
    bulkUpdateBuilder.trackStatistics(!args.remove("-s"));
    try {
        bulkUpdateBuilder.dataType(DataType.valueOf(args.remove(0).toUpperCase(Locale.ROOT)));
    } catch (IllegalArgumentException e) {
        Message.BULK_UPDATE_INVALID_DATA_TYPE.send(sender);
        return;
    }
    String action = args.remove(0).toLowerCase(Locale.ROOT);
    switch(action) {
        case "delete":
            bulkUpdateBuilder.action(DeleteAction.create());
            break;
        case "update":
            if (args.size() < 2) {
                throw new ArgumentException.DetailedUsage();
            }
            String field = args.remove(0);
            QueryField queryField = QueryField.of(field);
            if (queryField == null) {
                throw new ArgumentException.DetailedUsage();
            }
            String value = args.remove(0);
            bulkUpdateBuilder.action(UpdateAction.of(queryField, value));
            break;
        default:
            throw new ArgumentException.DetailedUsage();
    }
    for (String constraint : args) {
        String[] parts = constraint.split(" ");
        if (parts.length != 3) {
            Message.BULK_UPDATE_INVALID_CONSTRAINT.send(sender, constraint);
            return;
        }
        QueryField field = QueryField.of(parts[0]);
        if (field == null) {
            Message.BULK_UPDATE_INVALID_CONSTRAINT.send(sender, constraint);
            return;
        }
        Comparison comparison = StandardComparison.parseComparison(parts[1]);
        if (comparison == null) {
            Message.BULK_UPDATE_INVALID_COMPARISON.send(sender, parts[1]);
            return;
        }
        String expr = parts[2];
        bulkUpdateBuilder.query(Query.of(field, Constraint.of(comparison, expr)));
    }
    BulkUpdate bulkUpdate = bulkUpdateBuilder.build();
    if (plugin.getConfiguration().get(ConfigKeys.SKIP_BULKUPDATE_CONFIRMATION)) {
        runOperation(bulkUpdate, plugin, sender);
    } else {
        String id = String.format("%04d", ThreadLocalRandom.current().nextInt(10000));
        this.pendingOperations.put(id, bulkUpdate);
        Message.BULK_UPDATE_QUEUED.send(sender, bulkUpdate.buildAsSql().toReadableString().replace("{table}", bulkUpdate.getDataType().getName()));
        Message.BULK_UPDATE_CONFIRM.send(sender, label, id);
    }
}
Also used : BulkUpdateBuilder(me.lucko.luckperms.common.bulkupdate.BulkUpdateBuilder) QueryField(me.lucko.luckperms.common.bulkupdate.query.QueryField) StandardComparison(me.lucko.luckperms.common.bulkupdate.comparison.StandardComparison) Comparison(me.lucko.luckperms.common.bulkupdate.comparison.Comparison) BulkUpdate(me.lucko.luckperms.common.bulkupdate.BulkUpdate)

Aggregations

BulkUpdate (me.lucko.luckperms.common.bulkupdate.BulkUpdate)1 BulkUpdateBuilder (me.lucko.luckperms.common.bulkupdate.BulkUpdateBuilder)1 Comparison (me.lucko.luckperms.common.bulkupdate.comparison.Comparison)1 StandardComparison (me.lucko.luckperms.common.bulkupdate.comparison.StandardComparison)1 QueryField (me.lucko.luckperms.common.bulkupdate.query.QueryField)1