Search in sources :

Example 1 with EconHelper

use of io.github.nucleuspowered.nucleus.internal.EconHelper in project Nucleus by NucleusPowered.

the class KitCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws ReturnMessageException {
    Kit kit = args.<Kit>getOne(KIT_PARAMETER).get();
    EconHelper econHelper = Nucleus.getNucleus().getEconHelper();
    double cost = econHelper.economyServiceExists() ? kit.getCost() : 0;
    if (permissions.testCostExempt(player)) {
        // If exempt - no cost.
        cost = 0;
    }
    // If we have a cost for the kit, check we have funds.
    if (cost > 0 && !econHelper.hasBalance(player, cost)) {
        throw ReturnMessageException.fromKey("command.kit.notenough", kit.getName(), econHelper.getCurrencySymbol(cost));
    }
    try {
        NucleusKitService.RedeemResult redeemResult = KIT_HANDLER.redeemKit(kit, player, true, this.mustGetAll);
        if (!redeemResult.rejected().isEmpty()) {
            // If we drop them, tell the user
            if (this.isDrop) {
                player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.itemsdropped"));
                redeemResult.rejected().forEach(x -> Util.dropItemOnFloorAtLocation(x, player.getLocation()));
            } else {
                player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.fullinventory"));
            }
        }
        if (kit.isDisplayMessageOnRedeem()) {
            player.sendMessage(this.plugin.getMessageProvider().getTextMessageWithFormat("command.kit.spawned", kit.getName()));
        }
        // Charge, if necessary
        if (cost > 0 && econHelper.economyServiceExists()) {
            econHelper.withdrawFromPlayer(player, cost);
        }
        return CommandResult.success();
    } catch (KitRedeemException ex) {
        switch(ex.getReason()) {
            case ALREADY_REDEEMED:
                throw ReturnMessageException.fromKey("command.kit.onetime.alreadyredeemed", kit.getName());
            case COOLDOWN_NOT_EXPIRED:
                KitRedeemException.Cooldown kre = (KitRedeemException.Cooldown) ex;
                throw ReturnMessageException.fromKey("command.kit.cooldown", Util.getTimeStringFromSeconds(kre.getTimeLeft().getSeconds()), kit.getName());
            case PRE_EVENT_CANCELLED:
                KitRedeemException.PreCancelled krepe = (KitRedeemException.PreCancelled) ex;
                throw new ReturnMessageException(krepe.getCancelMessage().orElseGet(() -> (Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("command.kit.cancelledpre", kit.getName()))));
            case NO_SPACE:
                throw ReturnMessageException.fromKey("command.kit.fullinventorynosave", kit.getName());
            case UNKNOWN:
            default:
                throw ReturnMessageException.fromKey("command.kit.fail", kit.getName());
        }
    }
}
Also used : NucleusKitService(io.github.nucleuspowered.nucleus.api.service.NucleusKitService) NoCooldown(io.github.nucleuspowered.nucleus.internal.annotations.command.NoCooldown) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) EconHelper(io.github.nucleuspowered.nucleus.internal.EconHelper) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) KitRedeemException(io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)

Aggregations

KitRedeemException (io.github.nucleuspowered.nucleus.api.exceptions.KitRedeemException)1 Kit (io.github.nucleuspowered.nucleus.api.nucleusdata.Kit)1 NucleusKitService (io.github.nucleuspowered.nucleus.api.service.NucleusKitService)1 EconHelper (io.github.nucleuspowered.nucleus.internal.EconHelper)1 NoCooldown (io.github.nucleuspowered.nucleus.internal.annotations.command.NoCooldown)1 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)1