Search in sources :

Example 6 with ItemDataNode

use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode in project Nucleus by NucleusPowered.

the class ClearItemAliasesCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    CatalogType al = args.<CatalogType>getOne(item).get();
    String id = al.getId().toLowerCase();
    ItemDataNode node = itemDataService.getDataForItem(id);
    node.clearAliases();
    itemDataService.setDataForItem(id, node);
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.removeitemalias.cleared", id));
    return CommandResult.success();
}
Also used : CatalogType(org.spongepowered.api.CatalogType) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 7 with ItemDataNode

use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode in project Nucleus by NucleusPowered.

the class SetItemAliasCommand method executeCommand.

@Override
public CommandResult executeCommand(CommandSource src, CommandContext args) throws Exception {
    // Do we have an item or blockstate?
    String a = args.<String>getOne(alias).get().toLowerCase();
    if (itemDataService.getIdFromAlias(a).isPresent()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.inuse", a));
        return CommandResult.empty();
    }
    if (!ItemDataNode.ALIAS_PATTERN.matcher(a).matches()) {
        src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.notvalid", a));
        return CommandResult.empty();
    }
    CatalogType type = getCatalogTypeFromHandOrArgs(src, item, args);
    // Set the alias.
    String id = type.getId().toLowerCase();
    ItemDataNode idn = itemDataService.getDataForItem(id);
    idn.addAlias(a);
    itemDataService.setDataForItem(id, idn);
    // Tell the user
    src.sendMessage(plugin.getMessageProvider().getTextMessageWithFormat("command.nucleus.setitemalias.success", a, id));
    return CommandResult.success();
}
Also used : CatalogType(org.spongepowered.api.CatalogType) ItemDataNode(io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode)

Example 8 with ItemDataNode

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

Example 9 with ItemDataNode

use of io.github.nucleuspowered.nucleus.configurate.datatypes.ItemDataNode 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();
}
Also used : Optional(java.util.Optional) ItemType(org.spongepowered.api.item.ItemType) ReturnMessageException(io.github.nucleuspowered.nucleus.internal.command.ReturnMessageException) CatalogType(org.spongepowered.api.CatalogType) BlockState(org.spongepowered.api.block.BlockState) Slot(org.spongepowered.api.item.inventory.Slot) ItemStack(org.spongepowered.api.item.inventory.ItemStack) Inventory(org.spongepowered.api.item.inventory.Inventory) 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