Search in sources :

Example 56 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class ItemInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    Optional<CatalogType> catalogTypeOptional = args.getOne(key);
    ItemStack it;
    if (catalogTypeOptional.isPresent()) {
        CatalogType ct = catalogTypeOptional.get();
        if (ct instanceof ItemType) {
            it = ((ItemType) ct).getTemplate().createStack();
        } else {
            BlockState bs = ((BlockState) ct);
            it = bs.getType().getItem().orElseThrow(() -> new CommandException(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.invalidblockstate"))).getTemplate().createStack();
            it.offer(Keys.ITEM_BLOCKSTATE, bs);
        }
    } else if (player.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        it = player.getItemInHand(HandTypes.MAIN_HAND).get();
    } else {
        player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.none"));
        return CommandResult.empty();
    }
    final List<Text> lt = new ArrayList<>();
    String id = it.getType().getId().toLowerCase();
    lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.id", it.getType().getId(), it.getTranslation().get()));
    Optional<BlockState> obs = it.get(Keys.ITEM_BLOCKSTATE);
    if (obs.isPresent()) {
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", obs.get().getId()));
        id = obs.get().getId().toLowerCase();
    }
    if (args.hasAny("e") || args.hasAny("extended")) {
        // For each key, see if the item supports it. If so, get and
        // print the value.
        DataScanner.getInstance().getKeysForHolder(it).entrySet().stream().filter(x -> x.getValue() != null).filter(x -> {
            // Work around a Sponge bug.
            try {
                return it.supports(x.getValue());
            } catch (Exception e) {
                return false;
            }
        }).forEach(x -> {
            Key<? extends BaseValue<Object>> k = (Key<? extends BaseValue<Object>>) x.getValue();
            if (it.get(k).isPresent()) {
                DataScanner.getText(player, "command.iteminfo.key", x.getKey(), it.get(k).get()).ifPresent(lt::add);
            }
        });
    }
    ItemDataNode itemDataNode = itemDataService.getDataForItem(id);
    // /buy and /sell prices
    if (econHelper.economyServiceExists() && plugin.getModuleContainer().isModuleLoaded(ServerShopModule.ID)) {
        boolean space = false;
        double buyPrice = itemDataNode.getServerBuyPrice();
        if (buyPrice >= 0) {
            lt.add(Text.EMPTY);
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.buyprice", econHelper.getCurrencySymbol(buyPrice)));
            space = true;
        }
        double sellPrice = itemDataNode.getServerSellPrice();
        if (sellPrice >= 0) {
            if (!space) {
                lt.add(Text.EMPTY);
            }
            lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.sellprice", econHelper.getCurrencySymbol(sellPrice)));
        }
    }
    List<String> aliases = itemDataNode.getAliases();
    if (!aliases.isEmpty()) {
        lt.add(Text.EMPTY);
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.list.aliases"));
        Text.Builder tb = Text.builder();
        Iterator<String> iterator = aliases.iterator();
        tb.append(Text.of(TextColors.YELLOW, iterator.next()));
        while (iterator.hasNext()) {
            tb.append(comma, Text.of(TextColors.YELLOW, iterator.next()));
        }
        lt.add(tb.build());
    }
    Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.list.header")).sendTo(player);
    return CommandResult.success();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Keys(org.spongepowered.api.data.key.Keys) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) EconHelper(io.github.nucleuspowered.nucleus.internal.EconHelper) HashMap(java.util.HashMap) ServerShopModule(io.github.nucleuspowered.nucleus.modules.servershop.ServerShopModule) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemDataService(io.github.nucleuspowered.nucleus.dataservices.ItemDataService) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) Iterator(java.util.Iterator) BaseValue(org.spongepowered.api.data.value.BaseValue) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) ItemAliasArgument(io.github.nucleuspowered.nucleus.argumentparsers.ItemAliasArgument) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode) BlockState(org.spongepowered.api.block.BlockState) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ItemType(org.spongepowered.api.item.ItemType) ItemType(org.spongepowered.api.item.ItemType) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) BaseValue(org.spongepowered.api.data.value.BaseValue) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Key(org.spongepowered.api.data.key.Key) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 57 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class BlockInfoCommand method executeCommand.

@Override
public CommandResult executeCommand(Player player, CommandContext args) throws Exception {
    BlockRay<World> bl = BlockRay.from(player).distanceLimit(10).stopFilter(BlockRay.continueAfterFilter(BlockRay.onlyAirFilter(), 1)).build();
    Optional<BlockRayHit<World>> ob = bl.end();
    // If the last block is not air...
    if (ob.isPresent() && ob.get().getLocation().getBlockType() != BlockTypes.AIR) {
        BlockRayHit<World> brh = ob.get();
        // get the information.
        BlockState b = brh.getLocation().getBlock();
        BlockType it = b.getType();
        List<Text> lt = new ArrayList<>();
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.id", it.getId(), it.getTranslation().get()));
        lt.add(plugin.getMessageProvider().getTextMessageWithFormat("command.iteminfo.extendedid", b.getId()));
        if (args.hasAny("e") || args.hasAny("extended")) {
            Collection<Property<?, ?>> cp = b.getApplicableProperties();
            if (!cp.isEmpty()) {
                cp.forEach(x -> {
                    if (x.getValue() != null) {
                        DataScanner.getText(player, "command.blockinfo.property.item", x.getKey().toString(), x.getValue()).ifPresent(lt::add);
                    }
                });
            }
            Collection<BlockTrait<?>> cb = b.getTraits();
            if (!cb.isEmpty()) {
                cb.forEach(x -> b.getTraitValue(x).ifPresent(v -> DataScanner.getText(player, "command.blockinfo.traits.item", x.getName(), v).ifPresent(lt::add)));
            }
        }
        Sponge.getServiceManager().provideUnchecked(PaginationService.class).builder().contents(lt).padding(Text.of(TextColors.GREEN, "-")).title(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.list.header", String.valueOf(brh.getBlockX()), String.valueOf(brh.getBlockY()), String.valueOf(brh.getBlockZ()))).sendTo(player);
        return CommandResult.success();
    }
    player.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.blockinfo.none"));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) GenericArguments(org.spongepowered.api.command.args.GenericArguments) BlockRay(org.spongepowered.api.util.blockray.BlockRay) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) ArrayList(java.util.ArrayList) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit) CommandResult(org.spongepowered.api.command.CommandResult) BlockTypes(org.spongepowered.api.block.BlockTypes) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) BlockState(org.spongepowered.api.block.BlockState) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) World(org.spongepowered.api.world.World) BlockType(org.spongepowered.api.block.BlockType) Property(org.spongepowered.api.data.Property) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) BlockTrait(org.spongepowered.api.block.trait.BlockTrait) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) Text(org.spongepowered.api.text.Text) World(org.spongepowered.api.world.World) BlockState(org.spongepowered.api.block.BlockState) BlockType(org.spongepowered.api.block.BlockType) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Property(org.spongepowered.api.data.Property) BlockRayHit(org.spongepowered.api.util.blockray.BlockRayHit)

Example 58 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class GetFromIpCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    String ip = args.<String>getOne(ipKey).get();
    if (Arrays.stream(ip.split("\\.")).anyMatch(x -> Integer.parseInt(x) > 255)) {
        throw ReturnMessageException.fromKey("command.getfromip.notvalid");
    }
    UserStorageService uss = Sponge.getServiceManager().provideUnchecked(UserStorageService.class);
    List<User> users = plugin.getUserCacheService().getForIp(ip).stream().map(uss::get).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    if (users.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.getfromip.nousers"));
        return CommandResult.success();
    }
    NameUtil name = plugin.getNameUtil();
    Util.getPaginationBuilder(src).title(plugin.getMessageProvider().getTextMessageWithFormat("command.getfromip.title", ip)).contents(users.stream().map(y -> {
        Text n = name.getName(y);
        return n.toBuilder().onClick(TextActions.runCommand("/nucleus:seen " + y.getName())).onHover(TextActions.showText(plugin.getMessageProvider().getTextMessageWithTextFormat("command.getfromip.hover", n))).build();
    }).collect(Collectors.toList())).sendTo(src);
    return CommandResult.success();
}
Also used : UserStorageService(org.spongepowered.api.service.user.UserStorageService) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Arrays(java.util.Arrays) NameUtil(io.github.nucleuspowered.nucleus.NameUtil) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) UserStorageService(org.spongepowered.api.service.user.UserStorageService) RegexArgument(io.github.nucleuspowered.nucleus.argumentparsers.RegexArgument) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) User(org.spongepowered.api.entity.living.player.User) Optional(java.util.Optional) Text(org.spongepowered.api.text.Text) NameUtil(io.github.nucleuspowered.nucleus.NameUtil)

Example 59 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class ListPlayerCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    boolean showVanished = this.permissions.testSuffix(src, "seevanished");
    Collection<Player> players = Sponge.getServer().getOnlinePlayers();
    long playerCount = players.size();
    long hiddenCount = players.stream().filter(x -> x.get(Keys.VANISH).orElse(false)).count();
    Text header;
    if (showVanished && hiddenCount > 0) {
        header = plugin.getMessageProvider().getTextMessageWithFormat("command.list.playercount.hidden", String.valueOf(playerCount), String.valueOf(Sponge.getServer().getMaxPlayers()), String.valueOf(hiddenCount));
    } else {
        header = plugin.getMessageProvider().getTextMessageWithFormat("command.list.playercount.base", String.valueOf(playerCount - hiddenCount), String.valueOf(Sponge.getServer().getMaxPlayers()));
    }
    src.sendMessage(header);
    Optional<PermissionService> optPermissionService = Sponge.getServiceManager().provide(PermissionService.class);
    if (this.listConfig.isGroupByPermissionGroup() && optPermissionService.isPresent()) {
        listByPermissionGroup(optPermissionService.get(), players, src, showVanished);
    } else {
        // If we have players, send them on.
        getPlayerList(players, showVanished).ifPresent(src::sendMessage);
    }
    return CommandResult.success();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Keys(org.spongepowered.api.data.key.Keys) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) HashMap(java.util.HashMap) Contextual(org.spongepowered.api.service.context.Contextual) PermissionService(org.spongepowered.api.service.permission.PermissionService) Function(java.util.function.Function) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) Lists(com.google.common.collect.Lists) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) Subject(org.spongepowered.api.service.permission.Subject) TextColors(org.spongepowered.api.text.format.TextColors) Nullable(javax.annotation.Nullable) CommandResult(org.spongepowered.api.command.CommandResult) PlayerInfoConfigAdapter(io.github.nucleuspowered.nucleus.modules.playerinfo.config.PlayerInfoConfigAdapter) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) CommandSource(org.spongepowered.api.command.CommandSource) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Maps(com.google.common.collect.Maps) Collectors(java.util.stream.Collectors) Reloadable(io.github.nucleuspowered.nucleus.internal.interfaces.Reloadable) ExecutionException(java.util.concurrent.ExecutionException) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) AFKHandler(io.github.nucleuspowered.nucleus.modules.afk.handlers.AFKHandler) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ListConfig(io.github.nucleuspowered.nucleus.modules.playerinfo.config.ListConfig) PermissionService(org.spongepowered.api.service.permission.PermissionService) Player(org.spongepowered.api.entity.living.player.Player) Text(org.spongepowered.api.text.Text)

Example 60 with CommandResult

use of org.spongepowered.api.command.CommandResult in project Nucleus by NucleusPowered.

the class MailReadBase method executeCommand.

public CommandResult executeCommand(CommandSource src, final User target, Collection<NucleusMailService.MailFilter> lmf) {
    List<MailData> lmd;
    if (!lmf.isEmpty()) {
        lmd = handler.getMailInternal(target, lmf.toArray(new NucleusMailService.MailFilter[lmf.size()]));
    } else {
        lmd = handler.getMailInternal(target);
    }
    if (lmd.isEmpty()) {
        if (src instanceof Player && target.getUniqueId().equals(((Player) src).getUniqueId())) {
            src.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat(!lmf.isEmpty() ? "command.mail.none.filter" : "command.mail.none.normal.self"));
        } else {
            src.sendMessage(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat(!lmf.isEmpty() ? "command.mail.none.filter" : "command.mail.none.normal.other", target.getName()));
        }
        return CommandResult.success();
    }
    List<Text> mails = lmd.stream().sorted(Comparator.comparing(MailMessage::getDate)).map(x -> createMessage(x, target)).collect(Collectors.toList());
    // Paginate the mail.
    PaginationService ps = Sponge.getServiceManager().provideUnchecked(PaginationService.class);
    PaginationList.Builder b = ps.builder().padding(Text.of(TextColors.GREEN, "-")).title(getHeader(src, target, !lmf.isEmpty())).contents(mails);
    if (!(src instanceof Player)) {
        b.linesPerPage(-1);
    } else {
        b.header(Nucleus.getNucleus().getMessageProvider().getTextMessageWithFormat("mail.header"));
    }
    b.sendTo(src);
    return CommandResult.success();
}
Also used : CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) User(org.spongepowered.api.entity.living.player.User) CommandSource(org.spongepowered.api.command.CommandSource) TextStyles(org.spongepowered.api.text.format.TextStyles) Collection(java.util.Collection) Sponge(org.spongepowered.api.Sponge) NucleusMailService(io.github.nucleuspowered.nucleus.api.service.NucleusMailService) PaginationService(org.spongepowered.api.service.pagination.PaginationService) Collectors(java.util.stream.Collectors) ZoneId(java.time.ZoneId) PaginationList(org.spongepowered.api.service.pagination.PaginationList) InternalServiceManagerTrait(io.github.nucleuspowered.nucleus.internal.traits.InternalServiceManagerTrait) List(java.util.List) MailMessage(io.github.nucleuspowered.nucleus.api.nucleusdata.MailMessage) Text(org.spongepowered.api.text.Text) MailData(io.github.nucleuspowered.nucleus.modules.mail.data.MailData) MailHandler(io.github.nucleuspowered.nucleus.modules.mail.handlers.MailHandler) DateTimeFormatter(java.time.format.DateTimeFormatter) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Comparator(java.util.Comparator) TextColors(org.spongepowered.api.text.format.TextColors) NucleusMailService(io.github.nucleuspowered.nucleus.api.service.NucleusMailService) Player(org.spongepowered.api.entity.living.player.Player) PaginationList(org.spongepowered.api.service.pagination.PaginationList) MailData(io.github.nucleuspowered.nucleus.modules.mail.data.MailData) Text(org.spongepowered.api.text.Text) PaginationService(org.spongepowered.api.service.pagination.PaginationService)

Aggregations

CommandResult (org.spongepowered.api.command.CommandResult)62 Text (org.spongepowered.api.text.Text)46 CommandContext (org.spongepowered.api.command.args.CommandContext)40 List (java.util.List)39 CommandSource (org.spongepowered.api.command.CommandSource)37 Player (org.spongepowered.api.entity.living.player.Player)36 Sponge (org.spongepowered.api.Sponge)34 Optional (java.util.Optional)33 Collectors (java.util.stream.Collectors)33 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)26 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)26 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)26 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)26 TextColors (org.spongepowered.api.text.format.TextColors)25 CommandElement (org.spongepowered.api.command.args.CommandElement)21 Util (io.github.nucleuspowered.nucleus.Util)20 GenericArguments (org.spongepowered.api.command.args.GenericArguments)20 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)18 CommandException (org.spongepowered.api.command.CommandException)18