Search in sources :

Example 1 with CommandException

use of org.spongepowered.api.command.CommandException in project Skree by Skelril.

the class MarketVerifyCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    Optional<MarketService> optService = Sponge.getServiceManager().provide(MarketService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The market service is not currently running."));
        return CommandResult.empty();
    }
    MarketService service = optService.get();
    Task.builder().async().execute(() -> {
        PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
        List<Clause<String, BigDecimal>> profitMargins = new ArrayList<>();
        for (IRecipe recipe : CraftingManager.getInstance().getRecipeList()) {
            ItemStack output = recipe.getRecipeOutput();
            if (output == null) {
                continue;
            }
            Optional<BigDecimal> optResultPrice = service.getPrice(tf(output));
            if (!optResultPrice.isPresent()) {
                continue;
            }
            String name = service.getAlias(tf(output)).orElse(output.getItem().getRegistryName().toString());
            Collection<ItemStack> items = new ArrayList<>();
            if (recipe instanceof ShapedRecipes) {
                items.addAll(Lists.newArrayList(((ShapedRecipes) recipe).recipeItems));
            } else if (recipe instanceof ShapelessRecipes) {
                items.addAll(((ShapelessRecipes) recipe).recipeItems);
            } else {
                src.sendMessage(Text.of(TextColors.RED, "Unsupported recipe for " + name));
                continue;
            }
            items.removeAll(Collections.singleton(null));
            BigDecimal creationCost = BigDecimal.ZERO;
            try {
                for (ItemStack stack : items) {
                    creationCost = creationCost.add(service.getPrice(tf(stack)).orElse(BigDecimal.ZERO));
                }
            } catch (Exception ex) {
                src.sendMessage(Text.of(TextColors.RED, "Couldn't complete checks for " + name));
                continue;
            }
            if (creationCost.equals(BigDecimal.ZERO)) {
                src.sendMessage(Text.of(TextColors.RED, "No ingredients found on market for " + name));
                continue;
            }
            BigDecimal sellPrice = optResultPrice.get();
            sellPrice = sellPrice.multiply(service.getSellFactor(sellPrice));
            profitMargins.add(new Clause<>(name, sellPrice.subtract(creationCost)));
        }
        List<Text> result = profitMargins.stream().sorted((a, b) -> b.getValue().subtract(a.getValue()).intValue()).map(a -> {
            boolean profitable = a.getValue().compareTo(BigDecimal.ZERO) >= 0;
            return Text.of(profitable ? TextColors.RED : TextColors.GREEN, a.getKey().toUpperCase(), " has a profit margin of ", profitable ? "+" : "", MarketImplUtil.format(a.getValue()));
        }).collect(Collectors.toList());
        pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Profit Margin Report")).padding(Text.of(" ")).sendTo(src);
    }).submit(SkreePlugin.inst());
    src.sendMessage(Text.of(TextColors.YELLOW, "Verification in progress..."));
    return CommandResult.success();
}
Also used : java.util(java.util) ItemStack(net.minecraft.item.ItemStack) BigDecimal(java.math.BigDecimal) Lists(com.google.common.collect.Lists) SkreePlugin(com.skelril.skree.SkreePlugin) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Task(org.spongepowered.api.scheduler.Task) ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) TextColors(org.spongepowered.api.text.format.TextColors) ForgeTransformer.tf(com.skelril.nitro.transformer.ForgeTransformer.tf) MarketImplUtil(com.skelril.skree.content.market.MarketImplUtil) CommandResult(org.spongepowered.api.command.CommandResult) IRecipe(net.minecraft.item.crafting.IRecipe) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) MarketService(com.skelril.skree.service.MarketService) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ShapelessRecipes(net.minecraft.item.crafting.ShapelessRecipes) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) CraftingManager(net.minecraft.item.crafting.CraftingManager) Clause(com.skelril.nitro.Clause) ShapedRecipes(net.minecraft.item.crafting.ShapedRecipes) IRecipe(net.minecraft.item.crafting.IRecipe) BigDecimal(java.math.BigDecimal) CommandException(org.spongepowered.api.command.CommandException) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Clause(com.skelril.nitro.Clause) ItemStack(net.minecraft.item.ItemStack) ShapelessRecipes(net.minecraft.item.crafting.ShapelessRecipes) MarketService(com.skelril.skree.service.MarketService)

Example 2 with CommandException

use of org.spongepowered.api.command.CommandException in project Skree by Skelril.

the class RegionRemMemberCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (!(src instanceof Player)) {
        src.sendMessage(Text.of("You must be a player to use this command (for now ;) )!"));
        return CommandResult.empty();
    }
    Optional<RegionService> optService = Sponge.getServiceManager().provide(RegionService.class);
    if (!optService.isPresent()) {
        src.sendMessage(Text.of(TextColors.DARK_RED, "The region service is not currently running."));
        return CommandResult.empty();
    }
    RegionService service = optService.get();
    Player player = (Player) src;
    Optional<Region> optRef = service.getSelectedRegion(player);
    if (!optRef.isPresent()) {
        player.sendMessage(Text.of(TextColors.RED, "You do not currently have a region selected."));
        return CommandResult.empty();
    }
    Region ref = optRef.get();
    if (!ref.getMembers().contains(player.getUniqueId())) {
        player.sendMessage(Text.of(TextColors.RED, "You must be a member of the region to modify it!"));
        return CommandResult.empty();
    }
    List<UUID> oldMembers = args.<User>getAll("player").stream().map(Identifiable::getUniqueId).filter(a -> ref.getMembers().contains(a)).collect(Collectors.toList());
    ref.remMember(oldMembers);
    player.sendMessage(Text.of(TextColors.YELLOW, "Removed ", oldMembers.size(), " players from the region."));
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) GenericArguments.allOf(org.spongepowered.api.command.args.GenericArguments.allOf) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) RegionService(com.skelril.skree.service.RegionService) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Region(com.skelril.skree.service.internal.region.Region) Optional(java.util.Optional) GenericArguments.user(org.spongepowered.api.command.args.GenericArguments.user) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Identifiable(org.spongepowered.api.util.Identifiable) Player(org.spongepowered.api.entity.living.player.Player) User(org.spongepowered.api.entity.living.player.User) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) UUID(java.util.UUID) Identifiable(org.spongepowered.api.util.Identifiable)

Example 3 with CommandException

use of org.spongepowered.api.command.CommandException in project Skree by Skelril.

the class WorldListCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    WorldService service = Sponge.getServiceManager().provideUnchecked(WorldService.class);
    List<WorldEffectWrapper> worldEffectWrapperList = service.getEffectWrappers().stream().sorted((a, b) -> a.getName().compareToIgnoreCase(b.getName())).collect(Collectors.toList());
    for (WorldEffectWrapper wrapper : worldEffectWrapperList) {
        String worldType = wrapper.getName();
        if (!src.hasPermission("skree.world." + worldType.toLowerCase() + ".teleport")) {
            continue;
        }
        src.sendMessage(Text.of(TextColors.GOLD, "Available ", worldType, " worlds (click to teleport):"));
        for (World world : wrapper.getWorlds()) {
            String worldName = world.getName();
            String prettyName = worldName.replaceAll("_", " ");
            src.sendMessage(Text.of(TextColors.YELLOW, TextActions.runCommand("/world " + worldName), TextActions.showText(Text.of("Teleport to " + prettyName)), " - ", prettyName));
        }
    }
    return CommandResult.success();
}
Also used : WorldService(com.skelril.skree.service.WorldService) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) WorldEffectWrapper(com.skelril.skree.service.internal.world.WorldEffectWrapper) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) World(org.spongepowered.api.world.World) TextColors(org.spongepowered.api.text.format.TextColors) WorldEffectWrapper(com.skelril.skree.service.internal.world.WorldEffectWrapper) World(org.spongepowered.api.world.World) WorldService(com.skelril.skree.service.WorldService)

Example 4 with CommandException

use of org.spongepowered.api.command.CommandException in project Skree by Skelril.

the class WildernessMetaCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    WorldService service = Sponge.getServiceManager().provideUnchecked(WorldService.class);
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    WildernessWorldWrapper wrapper = service.getEffectWrapper(WildernessWorldWrapper.class).get();
    List<Text> result = wrapper.getMetaInformation().stream().sorted(Comparator.comparing(a -> a.getKey().getName())).map(this::createLine).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Meta Info List")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : WorldService(com.skelril.skree.service.WorldService) CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) DecimalFormat(java.text.DecimalFormat) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Map(java.util.Map) Player(org.spongepowered.api.entity.living.player.Player) Comparator(java.util.Comparator) TextColors(org.spongepowered.api.text.format.TextColors) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService) WorldService(com.skelril.skree.service.WorldService)

Example 5 with CommandException

use of org.spongepowered.api.command.CommandException in project TotalEconomy by Erigitic.

the class PayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String strAmount = (String) args.getOne("amount").get();
    Player recipient = (Player) args.getOne("player").get();
    if (src instanceof Player) {
        Player sender = (Player) src;
        if (sender.getUniqueId().equals(recipient.getUniqueId())) {
            throw new CommandException(Text.of("You cannot pay yourself!"));
        }
        Pattern amountPattern = Pattern.compile("^[+]?(\\d*\\.)?\\d+$");
        Matcher m = amountPattern.matcher(strAmount);
        if (m.matches()) {
            BigDecimal amount = new BigDecimal(strAmount).setScale(2, BigDecimal.ROUND_DOWN);
            TEAccount playerAccount = (TEAccount) accountManager.getOrCreateAccount(sender.getUniqueId()).get();
            TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
            TransferResult transferResult = playerAccount.transfer(recipientAccount, totalEconomy.getDefaultCurrency(), amount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
            if (transferResult.getResult() == ResultType.SUCCESS) {
                sender.sendMessage(Text.of(TextColors.GRAY, "You have sent ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " to ", TextColors.GOLD, recipient.getName(), TextColors.GRAY, "."));
                recipient.sendMessage(Text.of(TextColors.GRAY, "You have received ", TextColors.GOLD, defaultCurrency.format(amount), TextColors.GRAY, " from ", TextColors.GOLD, sender.getName(), TextColors.GRAY, "."));
                return CommandResult.success();
            } else if (transferResult.getResult() == ResultType.ACCOUNT_NO_FUNDS) {
                throw new CommandException(Text.of("Insufficient funds!"));
            }
        } else {
            throw new CommandException(Text.of("Invalid amount! Must be a positive number!"));
        }
    }
    return CommandResult.empty();
}
Also used : Pattern(java.util.regex.Pattern) Player(org.spongepowered.api.entity.living.player.Player) Matcher(java.util.regex.Matcher) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) TransferResult(org.spongepowered.api.service.economy.transaction.TransferResult) BigDecimal(java.math.BigDecimal)

Aggregations

CommandException (org.spongepowered.api.command.CommandException)11 Text (org.spongepowered.api.text.Text)9 Collectors (java.util.stream.Collectors)8 Sponge (org.spongepowered.api.Sponge)8 CommandResult (org.spongepowered.api.command.CommandResult)8 CommandSource (org.spongepowered.api.command.CommandSource)8 CommandContext (org.spongepowered.api.command.args.CommandContext)8 CommandExecutor (org.spongepowered.api.command.spec.CommandExecutor)8 CommandSpec (org.spongepowered.api.command.spec.CommandSpec)8 TextColors (org.spongepowered.api.text.format.TextColors)8 List (java.util.List)7 Player (org.spongepowered.api.entity.living.player.Player)7 Optional (java.util.Optional)5 PaginationService (org.spongepowered.api.service.pagination.PaginationService)5 RegionService (com.skelril.skree.service.RegionService)4 Region (com.skelril.skree.service.internal.region.Region)4 BigDecimal (java.math.BigDecimal)4 TEAccount (com.erigitic.config.TEAccount)2 MarketService (com.skelril.skree.service.MarketService)2 WorldService (com.skelril.skree.service.WorldService)2