Search in sources :

Example 6 with CommandException

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

the class JobCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    if (src instanceof Player) {
        Player player = ((Player) src).getPlayer().get();
        String jobName = totalEconomy.getTEJobManager().getPlayerJob(player);
        player.sendMessage(Text.of(TextColors.GRAY, "Current Job: ", TextColors.GOLD, totalEconomy.getTEJobManager().titleize(jobName)));
        player.sendMessage(Text.of(TextColors.GRAY, totalEconomy.getTEJobManager().titleize(jobName), " Level: ", TextColors.GOLD, totalEconomy.getTEJobManager().getJobLevel(jobName, player)));
        player.sendMessage(Text.of(TextColors.GRAY, totalEconomy.getTEJobManager().titleize(jobName), " Exp: ", TextColors.GOLD, totalEconomy.getTEJobManager().getJobExp(jobName, player), "/", totalEconomy.getTEJobManager().getExpToLevel(player), " exp\n"));
        player.sendMessage(Text.of(TextColors.GRAY, "Available Jobs: ", TextColors.GOLD, totalEconomy.getTEJobManager().getJobList()));
        return CommandResult.success();
    } else {
        throw new CommandException(Text.of("You can't have a job!"));
    }
}
Also used : Player(org.spongepowered.api.entity.living.player.Player) CommandException(org.spongepowered.api.command.CommandException)

Example 7 with CommandException

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

the class AdminPayCommand method execute.

@Override
public CommandResult execute(CommandSource src, CommandContext args) throws CommandException {
    String strAmount = (String) args.getOne("amount").get();
    User recipient = args.<User>getOne("player").get();
    Pattern amountPattern = Pattern.compile("^[+-]?(\\d*\\.)?\\d+$");
    Matcher m = amountPattern.matcher(strAmount);
    if (m.matches()) {
        BigDecimal amount = new BigDecimal((String) args.getOne("amount").get()).setScale(2, BigDecimal.ROUND_DOWN);
        TEAccount recipientAccount = (TEAccount) accountManager.getOrCreateAccount(recipient.getUniqueId()).get();
        Text amountText = Text.of(defaultCurrency.format(amount).toPlain().replace("-", ""));
        TransactionResult transactionResult = recipientAccount.deposit(totalEconomy.getDefaultCurrency(), amount, Cause.of(NamedCause.of("TotalEconomy", totalEconomy.getPluginContainer())));
        if (transactionResult.getResult() == ResultType.SUCCESS) {
            if (!strAmount.contains("-")) {
                src.sendMessage(Text.of(TextColors.GRAY, "You have sent ", TextColors.GOLD, amountText, TextColors.GRAY, " to ", TextColors.GOLD, recipient.getName(), TextColors.GRAY, "."));
                if (recipient.isOnline()) {
                    recipient.getPlayer().get().sendMessage(Text.of(TextColors.GRAY, "You have received ", TextColors.GOLD, amountText, TextColors.GRAY, " from ", TextColors.GOLD, src.getName(), TextColors.GRAY, "."));
                }
            } else {
                src.sendMessage(Text.of(TextColors.GRAY, "You have removed ", TextColors.GOLD, amountText, TextColors.GRAY, " from ", TextColors.GOLD, recipient.getName(), "'s", TextColors.GRAY, " account."));
                if (recipient.isOnline()) {
                    recipient.getPlayer().get().sendMessage(Text.of(TextColors.GOLD, amountText, TextColors.GRAY, " has been removed from your account by ", TextColors.GOLD, src.getName(), TextColors.GRAY, "."));
                }
            }
            return CommandResult.success();
        }
    } else {
        throw new CommandException(Text.of("Invalid amount! Must be a number!"));
    }
    return CommandResult.empty();
}
Also used : Pattern(java.util.regex.Pattern) TransactionResult(org.spongepowered.api.service.economy.transaction.TransactionResult) User(org.spongepowered.api.entity.living.player.User) Matcher(java.util.regex.Matcher) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) TEAccount(com.erigitic.config.TEAccount) BigDecimal(java.math.BigDecimal)

Example 8 with CommandException

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

the class MarketListCommand 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();
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    Optional<String> optFilter = args.getOne("name");
    String filter = optFilter.isPresent() ? optFilter.get() : "";
    if (!filter.matches(MarketService.VALID_ALIAS_REGEX)) {
        src.sendMessage(Text.of(TextColors.RED, "Invalid filter supplied."));
        return CommandResult.empty();
    }
    List<ItemDescriptor> prices = filter.isEmpty() ? service.getPrices() : service.getPrices(filter + "%");
    if (prices.isEmpty()) {
        src.sendMessage(Text.of(TextColors.YELLOW, "No items matched."));
        return CommandResult.success();
    }
    List<Text> result = prices.stream().filter(a -> filter.isEmpty() || a.getName().startsWith(filter)).sorted(Comparator.comparing(ItemDescriptor::getCurrentValue)).map(a -> createLine(a, service)).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Item List")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) CommandSource(org.spongepowered.api.command.CommandSource) GenericArguments.optional(org.spongepowered.api.command.args.GenericArguments.optional) Sponge(org.spongepowered.api.Sponge) MarketImplUtil.format(com.skelril.skree.content.market.MarketImplUtil.format) GenericArguments.remainingJoinedStrings(org.spongepowered.api.command.args.GenericArguments.remainingJoinedStrings) MarketService(com.skelril.skree.service.MarketService) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ItemDescriptor(com.skelril.skree.service.internal.market.ItemDescriptor) Collectors(java.util.stream.Collectors) CommandSpec(org.spongepowered.api.command.spec.CommandSpec) CommandException(org.spongepowered.api.command.CommandException) BigDecimal(java.math.BigDecimal) List(java.util.List) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) CommandExecutor(org.spongepowered.api.command.spec.CommandExecutor) Optional(java.util.Optional) Comparator(java.util.Comparator) TextColors(org.spongepowered.api.text.format.TextColors) ItemDescriptor(com.skelril.skree.service.internal.market.ItemDescriptor) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService) MarketService(com.skelril.skree.service.MarketService)

Example 9 with CommandException

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

the class RegionAddMemberCommand 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> newMembers = args.<User>getAll("player").stream().map(Identifiable::getUniqueId).filter(a -> !ref.getMembers().contains(a)).collect(Collectors.toList());
    ref.addMember(newMembers);
    player.sendMessage(Text.of(TextColors.YELLOW, "Added ", newMembers.size(), " players to 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 10 with CommandException

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

the class RegionListMarkersCommand 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();
    PaginationService pagination = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    List<RegionPoint> points = ref.getPoints();
    List<Text> result = ref.getFullPoints().stream().map(a -> Text.of((points.contains(a) ? TextColors.GREEN : TextColors.RED), a.getFloorX(), ", ", a.getFloorY(), ", ", a.getFloorZ())).collect(Collectors.toList());
    pagination.builder().contents(result).title(Text.of(TextColors.GOLD, "Region Markers")).padding(Text.of(" ")).sendTo(src);
    return CommandResult.success();
}
Also used : RegionPoint(com.skelril.skree.service.internal.region.RegionPoint) CommandResult(org.spongepowered.api.command.CommandResult) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) 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) Player(org.spongepowered.api.entity.living.player.Player) TextColors(org.spongepowered.api.text.format.TextColors) Player(org.spongepowered.api.entity.living.player.Player) Region(com.skelril.skree.service.internal.region.Region) RegionService(com.skelril.skree.service.RegionService) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService) RegionPoint(com.skelril.skree.service.internal.region.RegionPoint)

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