Search in sources :

Example 1 with PrismParameterHandler

use of me.botsko.prism.parameters.PrismParameterHandler in project Prism-Bukkit by prism.

the class PreprocessArgs method parseParam.

/**
     * 
     * @param plugin
     * @param sender
     * @param parameters
     * @param registeredParams
     * @param foundArgsNames
     * @param foundArgsList
     * @param arg
     * @return
     */
private static ParseResult parseParam(Prism plugin, CommandSender sender, QueryParameters parameters, HashMap<String, PrismParameterHandler> registeredParams, Set<String> foundArgsNames, List<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)
                sender.sendMessage(Prism.messenger.playerError("Unrecognized parameter '" + arg + "'. Use /prism ? for help."));
            break;
        case NoPermission:
            if (sender != null)
                sender.sendMessage(Prism.messenger.playerError("No permission for parameter '" + arg + "', skipped."));
            break;
        default:
            break;
    }
    return result;
}
Also used : Player(org.bukkit.entity.Player) MatchRule(me.botsko.prism.actionlibs.MatchRule) PrismParameterHandler(me.botsko.prism.parameters.PrismParameterHandler)

Example 2 with PrismParameterHandler

use of me.botsko.prism.parameters.PrismParameterHandler in project Prism-Bukkit by prism.

the class PreprocessArgs method process.

/**
     *
     */
public static QueryParameters process(Prism plugin, CommandSender sender, String[] args, PrismProcessType processType, int startAt, boolean useDefaults, boolean optional) {
    // Check for player or sender
    Player player = null;
    if (sender != null && sender instanceof Player) {
        player = (Player) sender;
    }
    // Start query
    final QueryParameters parameters = new QueryParameters();
    parameters.setProcessType(processType);
    // Define pagination/process type
    if (parameters.getProcessType().equals(PrismProcessType.LOOKUP)) {
        parameters.setLimit(plugin.getConfig().getInt("prism.queries.lookup-max-results"));
        parameters.setPerPage(plugin.getConfig().getInt("prism.queries.default-results-per-page"));
    }
    // Load registered parameters
    final HashMap<String, PrismParameterHandler> registeredParams = Prism.getParameters();
    // Store names of matched params/handlers
    final Set<String> foundArgsNames = new HashSet<String>();
    final List<MatchedParam> foundArgsList = new ArrayList<MatchedParam>();
    // Iterate all command arguments
    if (args == null) {
        return parameters;
    }
    for (int i = startAt; i < args.length; i++) {
        final String arg = args[i];
        if (arg.isEmpty())
            continue;
        if (parseParam(plugin, sender, parameters, registeredParams, foundArgsNames, foundArgsList, arg) == ParseResult.NotFound)
            return null;
    }
    parameters.setFoundArgs(foundArgsNames);
    // Reject no matches
    if (foundArgsList.isEmpty() && !optional) {
        if (sender != null)
            sender.sendMessage(Prism.messenger.playerError("You're missing valid parameters. Use /prism ? for assistance."));
        return null;
    }
    /**
         * Call default method for handlers *not* used
         */
    if (useDefaults) {
        for (final Entry<String, PrismParameterHandler> entry : registeredParams.entrySet()) {
            if (!foundArgsNames.contains(entry.getKey().toLowerCase())) {
                entry.getValue().defaultTo(parameters, sender);
            }
        }
    }
    /**
         * Send arguments to parameter handlers
         */
    for (final MatchedParam matchedParam : foundArgsList) {
        try {
            final PrismParameterHandler handler = matchedParam.getHandler();
            handler.process(parameters, matchedParam.getArg(), sender);
        } catch (final IllegalArgumentException e) {
            if (sender != null)
                sender.sendMessage(Prism.messenger.playerError(e.getMessage()));
            return null;
        }
    }
    // Enforce specifying an action
    if (sender != null && sender.hasPermission("prism.parameters.action.required") && parameters.getActionTypes().isEmpty()) {
        sender.sendMessage(Prism.messenger.playerError("You're missing valid actions. Use /prism ? for assistance."));
        return null;
    }
    // Player location
    if (player != null && !plugin.getConfig().getBoolean("prism.queries.never-use-defaults") && parameters.getPlayerLocation() == null && (parameters.getMaxLocation() == null || parameters.getMinLocation() == null)) {
        parameters.setMinMaxVectorsFromPlayerLocation(player.getLocation());
    }
    return parameters;
}
Also used : Player(org.bukkit.entity.Player) QueryParameters(me.botsko.prism.actionlibs.QueryParameters) PrismParameterHandler(me.botsko.prism.parameters.PrismParameterHandler)

Aggregations

PrismParameterHandler (me.botsko.prism.parameters.PrismParameterHandler)2 Player (org.bukkit.entity.Player)2 MatchRule (me.botsko.prism.actionlibs.MatchRule)1 QueryParameters (me.botsko.prism.actionlibs.QueryParameters)1