Search in sources :

Example 1 with Util

use of io.github.nucleuspowered.nucleus.Util 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();
}
Also used : CatalogType(org.spongepowered.api.CatalogType) CatalogType(org.spongepowered.api.CatalogType) Util(io.github.nucleuspowered.nucleus.Util) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Aggregations

Util (io.github.nucleuspowered.nucleus.Util)1 ItemDataNode (io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)1 CatalogType (org.spongepowered.api.CatalogType)1