Search in sources :

Example 1 with EconHandler

use of com.plotsquared.core.util.EconHandler in project PlotSquared by IntellectualSites.

the class MainCommand method onCommand.

public static boolean onCommand(final PlotPlayer<?> player, String... args) {
    final EconHandler econHandler = PlotSquared.platform().econHandler();
    if (args.length >= 1 && args[0].contains(":")) {
        String[] split2 = args[0].split(":");
        if (split2.length == 2) {
            // Ref: c:v, this will push value to the last spot in the array
            // ex. /p h:2 SomeUsername
            // > /p h SomeUsername 2
            String[] tmp = new String[args.length + 1];
            tmp[0] = split2[0];
            tmp[args.length] = split2[1];
            if (args.length >= 2) {
                System.arraycopy(args, 1, tmp, 1, args.length - 1);
            }
            args = tmp;
        }
    }
    try {
        getInstance().execute(player, args, new RunnableVal3<>() {

            @Override
            public void run(final Command cmd, final Runnable success, final Runnable failure) {
                if (cmd.hasConfirmation(player)) {
                    CmdConfirm.addPending(player, cmd.getUsage(), () -> {
                        PlotArea area = player.getApplicablePlotArea();
                        if (area != null && econHandler.isEnabled(area)) {
                            PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
                            double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
                            if (econHandler.getMoney(player) < price) {
                                if (failure != null) {
                                    failure.run();
                                }
                                return;
                            }
                        }
                        if (success != null) {
                            success.run();
                        }
                    });
                    return;
                }
                PlotArea area = player.getApplicablePlotArea();
                if (area != null && econHandler.isEnabled(area)) {
                    PlotExpression priceEval = area.getPrices().get(cmd.getFullId());
                    double price = priceEval != null ? priceEval.evaluate(0d) : 0d;
                    if (price != 0d && econHandler.getMoney(player) < price) {
                        if (failure != null) {
                            failure.run();
                        }
                        return;
                    }
                }
                if (success != null) {
                    success.run();
                }
            }
        }, new RunnableVal2<>() {

            @Override
            public void run(Command cmd, CommandResult result) {
            // Post command stuff!?
            }
        }).thenAccept(result -> {
        // TODO: Something with the command result
        });
    } catch (CommandException e) {
        e.perform(player);
    }
    // Always true
    return true;
}
Also used : EconHandler(com.plotsquared.core.util.EconHandler) PlotExpression(com.plotsquared.core.util.PlotExpression) SinglePlotArea(com.plotsquared.core.plot.world.SinglePlotArea) PlotArea(com.plotsquared.core.plot.PlotArea) RunnableVal2(com.plotsquared.core.util.task.RunnableVal2) RunnableVal3(com.plotsquared.core.util.task.RunnableVal3)

Aggregations

PlotArea (com.plotsquared.core.plot.PlotArea)1 SinglePlotArea (com.plotsquared.core.plot.world.SinglePlotArea)1 EconHandler (com.plotsquared.core.util.EconHandler)1 PlotExpression (com.plotsquared.core.util.PlotExpression)1 RunnableVal2 (com.plotsquared.core.util.task.RunnableVal2)1 RunnableVal3 (com.plotsquared.core.util.task.RunnableVal3)1