use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode in project Nucleus by NucleusPowered.
the class RemoveItemAliasCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
String al = args.<String>getOne(alias).get();
String id = itemDataService.getIdFromAlias(al).get();
ItemDataNode node = itemDataService.getDataForItem(id);
node.removeAlias(al);
itemDataService.setDataForItem(id, node);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.removeitemalias.removed", al, id));
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode in project Nucleus by NucleusPowered.
the class SellCommand method executeCommand.
@Override
public CommandResult executeCommand(final Player src, CommandContext args) throws Exception {
// Get the item in the hand.
ItemStack is = src.getItemInHand(HandTypes.MAIN_HAND).orElseThrow(() -> new ReturnMessageException(plugin.getMessageProvider().getTextMessageWithFormat("command.generalerror.handempty")));
String id;
Optional<BlockState> blockState = is.get(Keys.ITEM_BLOCKSTATE);
id = blockState.map(blockState1 -> blockState1.getId().toLowerCase()).orElseGet(() -> is.getType().getId());
ItemDataNode node = itemDataService.getDataForItem(id);
final double sellPrice = node.getServerSellPrice();
if (sellPrice < 0) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.itemsell.notforselling"));
return CommandResult.empty();
}
// Get the cost.
final int amt = is.getQuantity();
final double overallCost = sellPrice * amt;
if (econHelper.depositInPlayer(src, overallCost, false)) {
src.setItemInHand(HandTypes.MAIN_HAND, null);
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.summary", Text.of(amt), Text.of(is), Text.of(econHelper.getCurrencySymbol(overallCost))));
return CommandResult.success();
}
src.sendMessage(plugin.getMessageProvider().getTextMessageWithTextFormat("command.itemsell.error", Text.of(is)));
return CommandResult.empty();
}
use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode in project Nucleus by NucleusPowered.
the class SetWorthCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
String id = getCatalogTypeFromHandOrArgs(src, item, args).getId();
Type transactionType = args.<Type>getOne(type).get();
double newCost = args.<Double>getOne(cost).get();
if (newCost < 0) {
newCost = -1;
}
// Get the item from the system.
ItemDataNode node = itemDataService.getDataForItem(id);
// Get the current item worth.
double currentWorth = transactionType.getter.apply(node);
String worth;
String newWorth;
if (econHelper.economyServiceExists()) {
worth = econHelper.getCurrencySymbol(currentWorth);
newWorth = econHelper.getCurrencySymbol(newCost);
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.setworth.noeconservice"));
worth = String.valueOf(currentWorth);
newWorth = String.valueOf(newCost);
}
String name;
Optional<CatalogType> type = Util.getCatalogTypeForItemFromId(id);
name = type.map(Util::getTranslatableIfPresentOnCatalogType).orElse(id);
if (currentWorth == newCost) {
if (currentWorth < 0) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.setworth.alreadyunavailable", name, transactionType.getTranslation()));
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.setworth.samecost", transactionType.getTranslation(), name, worth));
}
return CommandResult.empty();
}
// Set the item worth.
transactionType.setter.accept(node, newCost);
itemDataService.setDataForItem(id, node);
// Tell the user.
if (currentWorth == -1) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.setworth.success.new", name, transactionType.getTranslation(), newWorth));
} else if (newCost == -1) {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.setworth.success.removed", name, transactionType.getTranslation(), worth));
} else {
src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.setworth.success.changed", name, transactionType.getTranslation(), newWorth, worth));
}
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode in project Nucleus by NucleusPowered.
the class WorthCommand method executeCommand.
@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
CatalogType type = getCatalogTypeFromHandOrArgs(src, item, args);
String id = type.getId();
// Get the item from the system.
ItemDataNode node = itemDataService.getDataForItem(id);
// Get the current item worth.
MessageProvider provider = plugin.getMessageProvider();
if (!econHelper.economyServiceExists()) {
src.sendMessage(provider.getTextMessageWithFormat("command.setworth.noeconservice"));
}
double buyPrice = node.getServerBuyPrice();
double sellPrice = node.getServerSellPrice();
StringBuilder stringBuilder = new StringBuilder();
if (buyPrice >= 0) {
stringBuilder.append(provider.getMessageWithFormat("command.worth.buy", econHelper.getCurrencySymbol(node.getServerBuyPrice())));
}
if (sellPrice >= 0) {
if (stringBuilder.length() > 0) {
stringBuilder.append(" - ");
}
stringBuilder.append(provider.getMessageWithFormat("command.worth.sell", econHelper.getCurrencySymbol(node.getServerSellPrice())));
}
if (stringBuilder.length() == 0) {
src.sendMessage(provider.getTextMessageWithFormat("command.worth.nothing", Util.getTranslatableIfPresentOnCatalogType(type)));
} else {
src.sendMessage(provider.getTextMessageWithFormat("command.worth.something", Util.getTranslatableIfPresentOnCatalogType(type), stringBuilder.toString()));
}
return CommandResult.success();
}
use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode 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