Search in sources :

Example 1 with MatchRule

use of me.botsko.prism.api.actions.MatchRule in project Prism-Bukkit by prism.

the class PreprocessArgs method parseParam.

/**
 * Parse a set of params.
 * @param plugin Prism
 * @param sender CommandSender
 * @param parameters QueryParameters
 * @param registeredParams Map
 * @param foundArgsNames Collection
 * @param foundArgsList Collection
 * @param arg String
 * @return ParseResult.
 */
private static ParseResult parseParam(Plugin plugin, CommandSender sender, QueryParameters parameters, Map<String, PrismParameterHandler> registeredParams, Collection<String> foundArgsNames, Collection<MatchedParam> foundArgsList, String arg) {
    ParseResult result = ParseResult.NotFound;
    // Match command argument to parameter handler
    for (final Entry<String, PrismParameterHandler> entry : registeredParams.entrySet()) {
        PrismParameterHandler parameterHandler = entry.getValue();
        if (!parameterHandler.applicable(arg, sender)) {
            continue;
        }
        if (!parameterHandler.hasPermission(arg, sender)) {
            result = ParseResult.NoPermission;
            continue;
        }
        result = ParseResult.Found;
        foundArgsList.add(new MatchedParam(parameterHandler, arg));
        foundArgsNames.add(parameterHandler.getName().toLowerCase());
        break;
    }
    // Reject argument that doesn't match anything
    if (result == ParseResult.NotFound) {
        // We support an alternate player syntax so that people
        // can use the tab-complete
        // feature of minecraft. Using p: prevents it.
        final Player autoFillPlayer = plugin.getServer().getPlayer(arg);
        if (autoFillPlayer != null) {
            MatchRule match = MatchRule.INCLUDE;
            if (arg.startsWith("!")) {
                match = MatchRule.EXCLUDE;
            }
            result = ParseResult.Found;
            parameters.addPlayerName(arg.replace("!", ""), match);
        }
    }
    switch(result) {
        case NotFound:
            if (sender != null) {
                Prism.messenger.sendMessage(sender, Prism.messenger.playerError("Unrecognized parameter '" + arg + "'. Use /prism ? for help."));
            } else {
                Prism.log("Unrecognized parameter '" + arg + "'");
            }
            break;
        case NoPermission:
            if (sender != null) {
                Prism.messenger.sendMessage(sender, Prism.messenger.playerError("No permission for parameter '" + arg + "', skipped."));
            } else {
                Prism.log("No permission for parameter '" + arg + "'");
            }
            break;
        default:
            break;
    }
    return result;
}
Also used : Player(org.bukkit.entity.Player) MatchRule(me.botsko.prism.api.actions.MatchRule) PrismParameterHandler(me.botsko.prism.parameters.PrismParameterHandler)

Example 2 with MatchRule

use of me.botsko.prism.api.actions.MatchRule in project Prism-Bukkit by prism.

the class ActionParameter method process.

@Override
public void process(QueryParameters query, String alias, String input, CommandSender sender) {
    // Check match type
    MatchRule match = MatchRule.INCLUDE;
    if (input.startsWith("!")) {
        match = MatchRule.EXCLUDE;
    }
    final String[] actions = input.split(",");
    if (actions.length > 0) {
        for (final String action : actions) {
            // Find all actions that match the action provided - whether the
            // full name or
            // short name.
            final ArrayList<ActionTypeImpl> actionTypes = Prism.getActionRegistry().getActionsByShortName(action.replace("!", ""));
            if (!actionTypes.isEmpty()) {
                List<String> noPermission = new ArrayList<>();
                for (final ActionTypeImpl actionType : actionTypes) {
                    // Ensure the action allows this process type
                    if ((query.getProcessType().equals(PrismProcessType.ROLLBACK) && !actionType.canRollback()) || (query.getProcessType().equals(PrismProcessType.RESTORE) && !actionType.canRestore())) {
                        // );
                        continue;
                    }
                    if (sender != null && !sender.hasPermission(getPermission() + "." + actionType.getName())) {
                        noPermission.add(actionType.getName());
                        continue;
                    }
                    query.addActionType(actionType.getName(), match);
                }
                if (!noPermission.isEmpty()) {
                    String message = "Ignoring action '" + action + "' because you don't have permission for ";
                    if (noPermission.size() != 1) {
                        message += "any of " + Joiner.on(',').join(noPermission) + ".";
                    } else if (noPermission.get(0).equals(action)) {
                        message += "it.";
                    } else {
                        message += noPermission.get(0) + ".";
                    }
                    Prism.messenger.sendMessage(sender, Prism.messenger.playerError(message));
                }
            } else {
                if (sender != null) {
                    Prism.messenger.sendMessage(sender, Prism.messenger.playerError("Ignoring action '" + action.replace("!", "") + "' because it's unrecognized. Did you mean '" + LevenshteinDistance.getClosestAction(action) + "'? Type '/prism params' for help."));
                }
            }
        }
        // If none were valid, we end here.
        if (query.getActionTypes().size() == 0) {
            throw new IllegalArgumentException("Action parameter value not recognized. Try /pr ? for help");
        }
    }
}
Also used : ArrayList(java.util.ArrayList) MatchRule(me.botsko.prism.api.actions.MatchRule) ActionTypeImpl(me.botsko.prism.actionlibs.ActionTypeImpl)

Example 3 with MatchRule

use of me.botsko.prism.api.actions.MatchRule in project Prism-Bukkit by prism.

the class EntityParameter method process.

@Override
public void process(QueryParameters query, String alias, String input, CommandSender sender) {
    MatchRule match = MatchRule.INCLUDE;
    if (input.startsWith("!")) {
        match = MatchRule.EXCLUDE;
    }
    final String[] entityNames = input.split(",");
    if (entityNames.length > 0) {
        for (final String entityName : entityNames) {
            query.addEntity(entityName.replace("!", ""), match);
        }
    }
}
Also used : MatchRule(me.botsko.prism.api.actions.MatchRule)

Example 4 with MatchRule

use of me.botsko.prism.api.actions.MatchRule in project Prism-Bukkit by prism.

the class PlayerParameter method process.

/**
 * {@inheritDoc}
 */
@Override
public void process(QueryParameters query, String alias, String input, CommandSender sender) {
    MatchRule match = MatchRule.INCLUDE;
    if (input.startsWith("!")) {
        match = MatchRule.EXCLUDE;
        input = input.replace("!", "");
    } else if (input.startsWith("~")) {
        match = MatchRule.PARTIAL;
        input = input.replace("~", "");
    }
    final String[] playerNames = input.split(",");
    if (playerNames.length > 0) {
        for (String playerName : playerNames) {
            query.addPlayerName(playerName, match);
        }
    }
}
Also used : MatchRule(me.botsko.prism.api.actions.MatchRule)

Example 5 with MatchRule

use of me.botsko.prism.api.actions.MatchRule in project Prism-Bukkit by prism.

the class SqlSelectQueryBuilder method playerCondition.

private void playerCondition() {
    final Map<String, MatchRule> playerNames = parameters.getPlayerNames();
    if (playerNames.size() > 0) {
        // Match the first rule, this needs to change, we can't include and
        // exclude at the same time
        MatchRule playerMatch = MatchRule.INCLUDE;
        for (final MatchRule match : playerNames.values()) {
            playerMatch = match;
            break;
        }
        final String matchQuery = (playerMatch.equals(MatchRule.INCLUDE) ? "IN" : "NOT IN");
        // we're doing it here. This is going to be rewritten soon anyway.
        for (Entry<String, MatchRule> entry : playerNames.entrySet()) {
            entry.setValue(MatchRule.INCLUDE);
        }
        // Add conditions
        addCondition(tableNameData + ".player_id " + matchQuery + " ( SELECT p.player_id FROM " + prefix + "players p WHERE " + buildMultipleConditions(playerNames, "p.player", null) + ")");
    }
}
Also used : MatchRule(me.botsko.prism.api.actions.MatchRule)

Aggregations

MatchRule (me.botsko.prism.api.actions.MatchRule)5 ArrayList (java.util.ArrayList)1 ActionTypeImpl (me.botsko.prism.actionlibs.ActionTypeImpl)1 PrismParameterHandler (me.botsko.prism.parameters.PrismParameterHandler)1 Player (org.bukkit.entity.Player)1