Search in sources :

Example 46 with ItemType

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();
}
Also used : ItemType(org.spongepowered.api.item.ItemType) List(java.util.List) ItemStack(org.spongepowered.api.item.inventory.ItemStack) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule)

Example 47 with ItemType

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();
}
Also used : NoModifiers(io.github.nucleuspowered.nucleus.internal.annotations.command.NoModifiers) CommandResult(org.spongepowered.api.command.CommandResult) ClickAction(org.spongepowered.api.text.action.ClickAction) TextActions(org.spongepowered.api.text.action.TextActions) Nucleus(io.github.nucleuspowered.nucleus.Nucleus) RegisterCommand(io.github.nucleuspowered.nucleus.internal.annotations.command.RegisterCommand) Sponge(org.spongepowered.api.Sponge) NonnullByDefault(org.spongepowered.api.util.annotation.NonnullByDefault) PowertoolUserDataModule(io.github.nucleuspowered.nucleus.modules.powertool.datamodules.PowertoolUserDataModule) Collectors(java.util.stream.Collectors) RunAsync(io.github.nucleuspowered.nucleus.internal.annotations.RunAsync) List(java.util.List) AbstractCommand(io.github.nucleuspowered.nucleus.internal.command.AbstractCommand) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) CommandContext(org.spongepowered.api.command.args.CommandContext) Text(org.spongepowered.api.text.Text) TextColor(org.spongepowered.api.text.format.TextColor) Map(java.util.Map) Optional(java.util.Optional) Util(io.github.nucleuspowered.nucleus.Util) Player(org.spongepowered.api.entity.living.player.Player) Permissions(io.github.nucleuspowered.nucleus.internal.annotations.command.Permissions) ItemType(org.spongepowered.api.item.ItemType) TextColors(org.spongepowered.api.text.format.TextColors) MessageProvider(io.github.nucleuspowered.nucleus.internal.messages.MessageProvider) ClickAction(org.spongepowered.api.text.action.ClickAction) ItemType(org.spongepowered.api.item.ItemType) TextColor(org.spongepowered.api.text.format.TextColor)

Example 48 with ItemType

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

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

Example 50 with ItemType

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

ItemType (org.spongepowered.api.item.ItemType)53 ItemStack (org.spongepowered.api.item.inventory.ItemStack)19 BlockState (org.spongepowered.api.block.BlockState)12 Listener (org.spongepowered.api.event.Listener)12 World (org.spongepowered.api.world.World)11 Optional (java.util.Optional)9 Text (org.spongepowered.api.text.Text)8 Region (br.net.fabiozumbi12.RedProtect.Sponge.Region)7 ArrayList (java.util.ArrayList)7 Sponge (org.spongepowered.api.Sponge)7 BlockType (org.spongepowered.api.block.BlockType)7 List (java.util.List)6 BlockSnapshot (org.spongepowered.api.block.BlockSnapshot)6 Player (org.spongepowered.api.entity.living.player.Player)6 Vector3d (com.flowpowered.math.vector.Vector3d)5 CommandResult (org.spongepowered.api.command.CommandResult)5 Collectors (java.util.stream.Collectors)4 IBlockState (net.minecraft.block.state.IBlockState)4 Inventory (org.spongepowered.api.item.inventory.Inventory)4 ItemStackSnapshot (org.spongepowered.api.item.inventory.ItemStackSnapshot)4