Search in sources :

Example 21 with CommandContext

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

the class KitEditCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
    if (KIT_HANDLER.isOpen(kitInfo.getName())) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.edit.current", kitInfo.getName()));
    }
    Inventory inventory = Util.getKitInventoryBuilder().property(InventoryTitle.PROPERTY_NAME, InventoryTitle.of(plugin.getMessageProvider().getTextMessageWithFormat("command.kit.edit.title", kitInfo.getName()))).build(plugin);
    kitInfo.getStacks().stream().filter(x -> !x.getType().equals(ItemTypes.NONE)).forEach(x -> inventory.offer(x.createStack()));
    Optional<Container> openedInventory = src.openInventory(inventory);
    if (openedInventory.isPresent()) {
        KIT_HANDLER.addKitInventoryToListener(Tuple.of(kitInfo, inventory), openedInventory.get());
        return CommandResult.success();
    }
    throw ReturnMessageException.fromKey("command.kit.edit.cantopen", kitInfo.getName());
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) ItemTypes(org.spongepowered.api.item.ItemTypes) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Since(io.github.nucleuspowered.nucleus.internal.annotations.Since) KitFallbackBase(io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase) GenericArguments(org.spongepowered.api.command.args.GenericArguments) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Util(io.github.nucleuspowered.nucleus.Util) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) KitHandler(io.github.nucleuspowered.nucleus.modules.kit.handlers.KitHandler) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Tuple(org.spongepowered.api.util.Tuple) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) CommandElement(org.spongepowered.api.command.args.CommandElement) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) Container(org.spongepowered.api.item.inventory.Container) Optional(java.util.Optional) Player(org.spongepowered.api.entity.living.player.Player) KitArgument(io.github.nucleuspowered.nucleus.argumentparsers.KitArgument) Container(org.spongepowered.api.item.inventory.Container) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 22 with CommandContext

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

the class CheckJailedCommand method executeCommand.

@Override
protected CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    // Using the cache, tell us who is jailed.
    MessageProvider provider = plugin.getMessageProvider();
    Optional<NamedLocation> jail = args.getOne(jailNameKey);
    List<UUID> usersInJail = jail.map(x -> plugin.getUserCacheService().getJailedIn(x.getName())).orElseGet(() -> plugin.getUserCacheService().getJailed());
    String jailName = jail.map(NamedLocation::getName).orElseGet(() -> provider.getMessageWithFormat("standard.alljails"));
    if (usersInJail.isEmpty()) {
        src.sendMessage(provider.getTextMessageWithFormat("command.checkjailed.none", jailName));
        return CommandResult.success();
    }
    // Get the users in this jail, or all jails
    Util.getPaginationBuilder(src).title(provider.getTextMessageWithFormat("command.checkjailed.header", jailName)).contents(usersInJail.stream().map(x -> {
        Text name = plugin.getNameUtil().getName(x).orElseGet(() -> Text.of("unknown: ", x.toString()));
        return name.toBuilder().onHover(TextActions.showText(provider.getTextMessageWithFormat("command.checkjailed.hover"))).onClick(TextActions.runCommand("/nucleus:checkjail " + x.toString())).build();
    }).collect(Collectors.toList())).sendTo(src);
    return CommandResult.success();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) CommandSource(org.spongepowered.api.command.CommandSource) NamedLocation(io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) UUID(java.util.UUID) CommandElement(org.spongepowered.api.command.args.CommandElement) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) JailArgument(io.github.nucleuspowered.nucleus.argumentparsers.JailArgument) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) JailHandler(io.github.nucleuspowered.nucleus.modules.jail.handlers.JailHandler) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) NamedLocation(io.github.nucleuspowered.nucleus.api.nucleusdata.NamedLocation) Text(org.spongepowered.api.text.Text) UUID(java.util.UUID)

Example 23 with CommandContext

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

the class KitEditCommandCommand method executeCommand.

@Override
protected CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    final Kit kitInfo = args.<Kit>getOne(KIT_PARAMETER).get();
    List<String> commands = kitInfo.getCommands();
    if (commands.size() > 9 * 6) {
        throw ReturnMessageException.fromKey("command.kit.command.edit.toomany", kitInfo.getName());
    }
    // Create an inventory with signed books.
    Random r = new Random();
    List<ItemStack> books = commands.stream().map(x -> {
        ItemStack stack = ItemStack.of(ItemTypes.WRITTEN_BOOK, 1);
        Text command = Text.of(x);
        stack.offer(Keys.DISPLAY_NAME, command);
        stack.offer(Keys.BOOK_PAGES, Lists.newArrayList(command));
        // So books don't stack.
        stack.offer(Keys.BOOK_AUTHOR, Text.of(kitInfo.getName(), "-", r.nextInt()));
        return stack;
    }).collect(Collectors.toList());
    // Create Inventory GUI.
    final InventoryTitle title = InventoryTitle.of(Text.of("Kit Commands: ", kitInfo.getName()));
    final Inventory inventory = Inventory.builder().of(InventoryArchetypes.DOUBLE_CHEST).property(InventoryTitle.PROPERTY_NAME, title).build(plugin);
    books.forEach(inventory::offer);
    src.openInventory(inventory).ifPresent(x -> KIT_HANDLER.addKitCommandInventoryToListener(Tuple.of(kitInfo, inventory), x));
    return CommandResult.success();
}
Also used : Inventory(org.spongepowered.api.item.inventory.Inventory) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Keys(org.spongepowered.api.data.key.Keys) ItemTypes(org.spongepowered.api.item.ItemTypes) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) Random(java.util.Random) KitFallbackBase(io.github.nucleuspowered.nucleus.modules.kit.commands.KitFallbackBase) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Lists(com.google.common.collect.Lists) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) InventoryArchetypes(org.spongepowered.api.item.inventory.InventoryArchetypes) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) Tuple(org.spongepowered.api.util.Tuple) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) List(java.util.List) Player(org.spongepowered.api.entity.living.player.Player) KitArgument(io.github.nucleuspowered.nucleus.argumentparsers.KitArgument) InventoryTitle(org.spongepowered.api.item.inventory.property.InventoryTitle) Random(java.util.Random) Kit(io.github.nucleuspowered.nucleus.api.nucleusdata.Kit) Text(org.spongepowered.api.text.Text) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory)

Example 24 with CommandContext

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

the class ListPowertoolCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    PowertoolUserDataModule inu = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
    boolean toggle = inu.isPowertoolToggled();
    Map<String, List<String>> powertools = inu.getPowertools();
    if (powertools.isEmpty()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.none"));
        return CommandResult.success();
    }
    // Generate powertools.
    List<Text> mesl = powertools.entrySet().stream().sorted((a, b) -> a.getKey().compareToIgnoreCase(b.getKey())).map(k -> from(inu, src, k.getKey(), k.getValue())).collect(Collectors.toList());
    // Paginate the tools.
    Util.getPaginationBuilder(src).title(plugin.getMessageProvider().getTextMessageWithFormat("command.powertool.list.header", toggle ? "&aenabled" : "&cdisabled")).padding(Text.of(TextColors.YELLOW, "-")).contents(mesl).sendTo(src);
    return CommandResult.success();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) ClickAction(org.spongepowered.api.text.action.ClickAction) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) List(java.util.List) Text(org.spongepowered.api.text.Text) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Example 25 with CommandContext

use of org.spongepowered.api.command.args.CommandContext 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)

Aggregations

CommandContext (org.spongepowered.api.command.args.CommandContext)55 CommandResult (org.spongepowered.api.command.CommandResult)46 Text (org.spongepowered.api.text.Text)45 CommandSource (org.spongepowered.api.command.CommandSource)40 List (java.util.List)36 Collectors (java.util.stream.Collectors)30 CommandElement (org.spongepowered.api.command.args.CommandElement)30 Player (org.spongepowered.api.entity.living.player.Player)29 Optional (java.util.Optional)28 Sponge (org.spongepowered.api.Sponge)28 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)27 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)27 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)27 NonnullByDefault (org.spongepowered.api.util.annotation.NonnullByDefault)27 GenericArguments (org.spongepowered.api.command.args.GenericArguments)26 TextColors (org.spongepowered.api.text.format.TextColors)24 Util (io.github.nucleuspowered.nucleus.Util)19 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)19 NoModifiers (io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers)18 CommandException (org.spongepowered.api.command.CommandException)18