Search in sources :

Example 21 with CommandResult

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

the class EnchantCommand method executeCommand.

@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
    // Check for item in hand
    if (!src.getItemInHand(HandTypes.MAIN_HAND).isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.noitem"));
        return CommandResult.empty();
    }
    // Get the arguments
    ItemStack itemInHand = src.getItemInHand(HandTypes.MAIN_HAND).get();
    EnchantmentType enchantment = args.<EnchantmentType>getOne(enchantmentKey).get();
    int level = args.<Integer>getOne(levelKey).get();
    boolean allowUnsafe = args.hasAny("u");
    boolean allowOverwrite = args.hasAny("o");
    // Can we apply the enchantment?
    if (!allowUnsafe) {
        if (!enchantment.canBeAppliedToStack(itemInHand)) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.enchant", itemInHand.getTranslation().get()));
            return CommandResult.empty();
        }
        if (level > enchantment.getMaximumLevel()) {
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.nounsafe.level", itemInHand.getTranslation().get()));
            return CommandResult.empty();
        }
    }
    // We know this should exist.
    EnchantmentData ed = itemInHand.getOrCreate(EnchantmentData.class).get();
    // Get all the enchantments.
    List<Enchantment> currentEnchants = ed.getListValue().get();
    List<Enchantment> enchantmentsToRemove = currentEnchants.stream().filter(x -> !x.getType().isCompatibleWith(enchantment) || x.getType().equals(enchantment)).collect(Collectors.toList());
    if (!allowOverwrite && !enchantmentsToRemove.isEmpty()) {
        // Build the list of the enchantment names, and send it.
        final StringBuilder sb = new StringBuilder();
        enchantmentsToRemove.forEach(x -> {
            if (sb.length() > 0) {
                sb.append(", ");
            }
            sb.append(Util.getTranslatableIfPresent(x.getType()));
        });
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.overwrite", sb.toString()));
        return CommandResult.empty();
    }
    // Remove all enchants that cannot co-exist.
    currentEnchants.removeIf(enchantmentsToRemove::contains);
    // Create the enchantment
    currentEnchants.add(Enchantment.of(enchantment, level));
    ed.setElements(currentEnchants);
    // Offer it to the item.
    DataTransactionResult dtr = itemInHand.offer(ed);
    if (dtr.isSuccessful()) {
        // If successful, we need to put the item in the player's hand for it to actually take effect.
        src.setItemInHand(HandTypes.MAIN_HAND, itemInHand);
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.success", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
        return CommandResult.success();
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.enchant.error", Util.getTranslatableIfPresent(enchantment), String.valueOf(level)));
    return CommandResult.empty();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) GenericArguments(org.spongepowered.api.command.args.GenericArguments) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) Enchantment(org.spongepowered.api.item.enchantment.Enchantment) ItemStack(org.spongepowered.api.item.inventory.ItemStack) 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) ImprovedCatalogTypeArgument(io.github.nucleuspowered.nucleus.argumentparsers.ImprovedCatalogTypeArgument) BoundedIntegerArgument(io.github.nucleuspowered.nucleus.argumentparsers.BoundedIntegerArgument) CommandResult(org.spongepowered.api.command.CommandResult) Maps(com.google.common.collect.Maps) CommandElement(org.spongepowered.api.command.args.CommandElement) Collectors(java.util.stream.Collectors) EnchantmentData(org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) HandTypes(org.spongepowered.api.data.type.HandTypes) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) EnchantmentType(org.spongepowered.api.item.enchantment.EnchantmentType) DataTransactionResult(org.spongepowered.api.data.DataTransactionResult) ItemStack(org.spongepowered.api.item.inventory.ItemStack) EnchantmentData(org.spongepowered.api.data.manipulator.mutable.item.EnchantmentData) Enchantment(org.spongepowered.api.item.enchantment.Enchantment)

Example 22 with CommandResult

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

the class SkullCommand method executeCommand.

@Override
public CommandResult executeCommand(Player pl, CommandContext args) throws Exception {
    User user = this.getUserFromArgs(User.class, pl, player, args);
    int amount = args.<Integer>getOne(amountKey).orElse(1);
    if (this.isUseMinecraftCommand) {
        CommandResult result = Sponge.getCommandManager().process(Sponge.getServer().getConsole(), String.format("minecraft:give %s skull %d 3 {SkullOwner:%s}", pl.getName(), amount, user.getName()));
        if (result.getSuccessCount().orElse(0) > 0) {
            pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.plural", String.valueOf(amount), user.getName()));
            return result;
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.error", user.getName()));
    }
    int fullStacks = amount / 64;
    int partialStack = amount % 64;
    // Create the Skull
    ItemStack skullStack = ItemStack.builder().itemType(ItemTypes.SKULL).quantity(64).build();
    // Set it to subject skull type and set the owner to the specified subject
    if (skullStack.offer(Keys.SKULL_TYPE, SkullTypes.PLAYER).isSuccessful() && skullStack.offer(Keys.REPRESENTED_PLAYER, user.getProfile()).isSuccessful()) {
        skullStack.toContainer().set(DataQuery.of("SkullOwner"), user.getName());
        List<ItemStack> itemStackList = Lists.newArrayList();
        // If there were stacks, create as many as needed.
        if (fullStacks > 0) {
            itemStackList.add(skullStack);
            for (int i = 2; i <= fullStacks; i++) {
                itemStackList.add(skullStack.copy());
            }
        }
        // Same with the partial stacks.
        if (partialStack > 0) {
            ItemStack is = skullStack.copy();
            is.setQuantity(partialStack);
            itemStackList.add(is);
        }
        int accepted = 0;
        int failed = 0;
        Inventory inventoryToOfferTo = pl.getInventory().query(Hotbar.class, GridInventory.class);
        for (ItemStack itemStack : itemStackList) {
            int stackSize = itemStack.getQuantity();
            InventoryTransactionResult itr = inventoryToOfferTo.offer(itemStack);
            int currentFail = itr.getRejectedItems().stream().mapToInt(ItemStackSnapshot::getQuantity).sum();
            failed += currentFail;
            accepted += stackSize - currentFail;
        }
        // What was accepted?
        if (accepted > 0) {
            if (failed > 0) {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.semifull", String.valueOf(failed)));
            }
            if (accepted == 1) {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.single", user.getName()));
            } else {
                pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.success.plural", String.valueOf(accepted), user.getName()));
            }
            return CommandResult.success();
        }
        pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.full", user.getName()));
        return CommandResult.empty();
    } else {
        pl.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.skull.error", user.getName()));
        return CommandResult.empty();
    }
}
Also used : User(org.spongepowered.api.entity.living.player.User) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) GridInventory(org.spongepowered.api.item.inventory.type.GridInventory) InventoryTransactionResult(org.spongepowered.api.item.inventory.transaction.InventoryTransactionResult) CommandResult(org.spongepowered.api.command.CommandResult)

Example 23 with CommandResult

use of org.spongepowered.api.command.CommandResult 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 24 with CommandResult

use of org.spongepowered.api.command.CommandResult 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 25 with CommandResult

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

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