Search in sources :

Example 26 with CatalogType

use of org.spongepowered.api.CatalogType in project Nucleus by NucleusPowered.

the class SellAllCommand method executeCommand.

@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
    boolean accepted = args.hasAny("a");
    CatalogType ct = getCatalogTypeFromHandOrArgs(src, itemKey, args);
    String id = ct.getId();
    ItemStack query;
    if (ct instanceof BlockState) {
        query = ItemStack.builder().fromBlockState((BlockState) ct).quantity(1).build();
        // Yeah...
        query.setQuantity(-1);
    } else {
        // Having a quantity of -1 causes an IllegalArgumentException here...
        query = ItemStack.of((ItemType) ct, 1);
        // and doesn't care here...
        query.setQuantity(-1);
    }
    ItemDataNode node = itemDataService.getDataForItem(id);
    final double sellPrice = node.getServerSellPrice();
    if (sellPrice < 0) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.itemsell.notforselling"));
    }
    Iterable<Slot> slots = Util.getStandardInventory(src).query(query).slots();
    List<ItemStack> itemsToSell = StreamSupport.stream(Util.getStandardInventory(src).query(query).slots().spliterator(), false).map(Inventory::peek).filter(Optional::isPresent).map(Optional::get).collect(Collectors.toList());
    // Get the cost.
    final int amt = itemsToSell.stream().mapToInt(ItemStack::getQuantity).sum();
    if (amt <= 0) {
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsellall.none", Text.of(query)));
    }
    final double overallCost = sellPrice * amt;
    if (accepted) {
        if (econHelper.depositInPlayer(src, overallCost, false)) {
            slots.forEach(Inventory::clear);
            src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.summary", Text.of(amt), Text.of(query), Text.of(econHelper.getCurrencySymbol(overallCost))));
            return CommandResult.success();
        }
        throw new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.error", Text.of(query)));
    }
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsellall.summary", Text.of(amt), Text.of(query), Text.of(econHelper.getCurrencySymbol(overallCost)), Text.of(id)).toBuilder().onClick(TextActions.runCommand("/nucleus:itemsellall -a " + id)).build());
    return CommandResult.success();
}
Also used : Optional(java.util.Optional) ItemType(org.spongepowered.api.item.ItemType) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Aggregations

CatalogType (org.spongepowered.api.CatalogType)26 Map (java.util.Map)12 ArrayList (java.util.ArrayList)8 ItemDataNode (io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)7 DataView (org.spongepowered.api.data.DataView)6 Nullable (javax.annotation.Nullable)5 CatalogRegistryModule (org.spongepowered.api.registry.CatalogRegistryModule)5 ImmutableMap (com.google.common.collect.ImmutableMap)4 Collection (java.util.Collection)4 List (java.util.List)4 Optional (java.util.Optional)4 Sponge (org.spongepowered.api.Sponge)4 DataContainer (org.spongepowered.api.data.DataContainer)4 Text (org.spongepowered.api.text.Text)4 ImmutableList (com.google.common.collect.ImmutableList)3 IOException (java.io.IOException)3 Method (java.lang.reflect.Method)3 IdentityHashMap (java.util.IdentityHashMap)3 BlockState (org.spongepowered.api.block.BlockState)3 DataSerializable (org.spongepowered.api.data.DataSerializable)3