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();
}
Aggregations