Search in sources :

Example 1 with ItemDataNode

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();
}
Also used : ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 2 with ItemDataNode

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();
}
Also used : BlockState(org.spongepowered.api.block.BlockState) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 3 with ItemDataNode

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

Example 4 with ItemDataNode

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

Example 5 with ItemDataNode

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();
}
Also used : RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Keys(org.spongepowered.api.data.key.Keys) DataScanner(io.github.nucleuspowered.nucleus.internal.DataScanner) PermissionInformation(io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) EconHelper(io.github.nucleuspowered.nucleus.internal.EconHelper) HashMap(java.util.HashMap) ServerShopModule(io.github.nucleuspowered.nucleus.modules.servershop.ServerShopModule) GenericArguments(org.spongepowered.api.command.args.GenericArguments) Key(org.spongepowered.api.data.key.Key) ArrayList(java.util.ArrayList) ItemStack(org.spongepowered.api.item.inventory.ItemStack) ItemDataService(io.github.nucleuspowered.nucleus.dataservices.ItemDataService) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) Map(java.util.Map) SuggestedLevel(io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) TextColors(org.spongepowered.api.text.format.TextColors) CommandResult(org.spongepowered.api.command.CommandResult) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) Iterator(java.util.Iterator) BaseValue(org.spongepowered.api.data.value.BaseValue) CatalogType(org.spongepowered.api.CatalogType) Sponge(org.spongepowered.api.Sponge) PaginationService(org.spongepowered.api.service.pagination.PaginationService) CommandElement(org.spongepowered.api.command.args.CommandElement) ItemAliasArgument(io.github.nucleuspowered.nucleus.argumentparsers.ItemAliasArgument) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode) BlockState(org.spongepowered.api.block.BlockState) CommandException(org.spongepowered.api.command.CommandException) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) HandTypes(org.spongepowered.api.data.type.HandTypes) Optional(java.util.Optional) EssentialsEquivalent(io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent) Player(org.spongepowered.api.entity.living.player.Player) ItemType(org.spongepowered.api.item.ItemType) ItemType(org.spongepowered.api.item.ItemType) ArrayList(java.util.ArrayList) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) CommandResult(org.spongepowered.api.command.CommandResult) BaseValue(org.spongepowered.api.data.value.BaseValue) Text(org.spongepowered.api.text.Text) CommandException(org.spongepowered.api.command.CommandException) CommandException(org.spongepowered.api.command.CommandException) CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) PaginationService(org.spongepowered.api.service.pagination.PaginationService) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Key(org.spongepowered.api.data.key.Key) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Aggregations

ItemDataNode (io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)9 CatalogType (org.spongepowered.api.CatalogType)7 BlockState (org.spongepowered.api.block.BlockState)4 ItemStack (org.spongepowered.api.item.inventory.ItemStack)4 ItemType (org.spongepowered.api.item.ItemType)3 ReturnMessageException (io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException)2 Optional (java.util.Optional)2 Nucleus (io.github.nucleuspowered.nucleus.Nucleus)1 Util (io.github.nucleuspowered.nucleus.Util)1 ItemAliasArgument (io.github.nucleuspowered.nucleus.argumentparsers.ItemAliasArgument)1 ItemDataService (io.github.nucleuspowered.nucleus.dataservices.ItemDataService)1 DataScanner (io.github.nucleuspowered.nucleus.internal.DataScanner)1 EconHelper (io.github.nucleuspowered.nucleus.internal.EconHelper)1 Permissions (io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions)1 RegisterCommand (io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand)1 AbstractCommand (io.github.nucleuspowered.nucleus.internal.command.AbstractCommand)1 EssentialsEquivalent (io.github.nucleuspowered.nucleus.internal.docgen.annotations.EssentialsEquivalent)1 MessageProvider (io.github.nucleuspowered.nucleus.internal.messages.MessageProvider)1 PermissionInformation (io.github.nucleuspowered.nucleus.internal.permissions.PermissionInformation)1 SuggestedLevel (io.github.nucleuspowered.nucleus.internal.permissions.SuggestedLevel)1