use of org.spongepowered.api.item.ItemType in project Nucleus by NucleusPowered.
the class DeletePowertoolCommand method executeCommand.
@Override
public CommandResult executeCommand(Player src, CommandContext args) throws Exception {
Optional<ItemStack> itemStack = src.getItemInHand(HandTypes.MAIN_HAND);
if (!itemStack.isPresent()) {
throw ReturnMessageException.fromKey("command.powertool.noitem");
}
PowertoolUserDataModule user = Nucleus.getNucleus().getUserDataManager().getUnchecked(src).get(PowertoolUserDataModule.class);
ItemType item = itemStack.get().getType();
Optional<List<String>> cmds = user.getPowertoolForItem(item);
if (cmds.isPresent() && !cmds.get().isEmpty()) {
user.clearPowertool(item);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.powertool.removed", Text.of(item)));
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.powertool.nocmds", Text.of(item)));
}
return CommandResult.success();
}
use of org.spongepowered.api.item.ItemType in project Nucleus by NucleusPowered.
the class ListPowertoolCommand method from.
private Text from(final PowertoolUserDataModule inu, Player src, String powertool, List<String> commands) {
Optional<ItemType> oit = Sponge.getRegistry().getType(ItemType.class, powertool);
MessageProvider mp = plugin.getMessageProvider();
// Create the click actions.
ClickAction viewAction = TextActions.executeCallback(pl -> Util.getPaginationBuilder(src).title(mp.getTextMessageWithFormat("command.powertool.ind.header", powertool)).padding(Text.of(TextColors.GREEN, "-")).contents(commands.stream().map(x -> Text.of(TextColors.YELLOW, x)).collect(Collectors.toList())).sendTo(src));
ClickAction deleteAction = TextActions.executeCallback(pl -> {
inu.clearPowertool(powertool);
pl.sendMessage(mp.getTextMessageWithFormat("command.powertool.removed", powertool));
});
TextColor tc = oit.map(itemType -> TextColors.YELLOW).orElse(TextColors.GRAY);
// id - [View] - [Delete]
return Text.builder().append(Text.of(tc, powertool)).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.view")).color(TextColors.YELLOW).onClick(viewAction).build()).append(Text.of(" - ")).append(Text.builder(mp.getMessageWithFormat("standard.delete")).color(TextColors.DARK_RED).onClick(deleteAction).build()).build();
}
use of org.spongepowered.api.item.ItemType in project Nucleus by NucleusPowered.
the class BuyCommand method executeCommand.
@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
CatalogType ct = args.<CatalogType>getOne(itemKey).get();
int amount = args.<Integer>getOne(amountKey).get();
ItemDataNode node = itemDataService.getDataForItem(ct.getId());
if (node.getServerBuyPrice() < 0) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.notforsale"));
return CommandResult.empty();
}
if (amount > max) {
amount = max;
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.maximum", String.valueOf(amount)));
}
final ItemStack created;
if (ct instanceof ItemType) {
created = ItemStack.of((ItemType) ct, amount);
} else {
created = ItemStack.builder().fromBlockState((BlockState) ct).quantity(amount).build();
}
// Get the cost.
final double perUnitCost = node.getServerBuyPrice();
final int unitCount = amount;
final double overallCost = perUnitCost * unitCount;
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itembuy.summary", Text.of(String.valueOf(amount)), Text.of(created), Text.of(econHelper.getCurrencySymbol(overallCost))));
if (args.hasAny("y")) {
new BuyCallback(src, overallCost, created, unitCount, perUnitCost).accept(src);
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itembuy.clickhere").toBuilder().onClick(TextActions.executeCallback(new BuyCallback(src, overallCost, created, unitCount, perUnitCost))).build());
}
return CommandResult.success();
}
use of org.spongepowered.api.item.ItemType 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();
}
use of org.spongepowered.api.item.ItemType 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();
}
Aggregations