Search in sources :

Example 1 with ItemClickData

use of gg.projecteden.nexus.features.menus.api.ItemClickData in project Nexus by ProjectEdenGG.

the class ExchangeConfigProvider method addItemSelector.

public void addItemSelector(Player player, InventoryContents contents, int row, AtomicReference<ItemStack> itemStack, boolean allowEditItem) {
    ItemStack placeholder = new ItemBuilder(Material.BLACK_STAINED_GLASS).name("&ePlace your item here").lore("&7or click to search for an item").build();
    if (!allowEditItem)
        contents.set(row, 4, ClickableItem.empty(itemStack.get()));
    else {
        Consumer<ItemClickData> action = e -> {
            ((InventoryClickEvent) e.getEvent()).setCancelled(true);
            if (!isNullOrAir(player.getItemOnCursor())) {
                try {
                    ItemStack item = player.getItemOnCursor();
                    if (new ItemBuilder(item).isNot(ItemSetting.TRADEABLE))
                        throw new InvalidInputException("You can not trade that item in shops");
                    itemStack.set(item);
                    PlayerUtils.giveItem(player, itemStack.get().clone());
                    player.setItemOnCursor(null);
                    open(player);
                } catch (Exception ex) {
                    PlayerUtils.send(player, Shops.PREFIX + "&c" + ex.getMessage());
                    open(player);
                }
            } else if (contents.get(row, 4).isPresent() && contents.get(row, 4).get().getItem().equals(placeholder)) {
                Nexus.getSignMenuFactory().lines("", ARROWS, "Enter a", "search term").prefix(Shops.PREFIX).onError(() -> open(player)).response(lines -> {
                    if (lines[0].length() > 0) {
                        Function<Material, Boolean> filter = material -> material.name().toLowerCase().contains(lines[0].toLowerCase());
                        new ItemSearchProvider(this, filter, onChoose -> {
                            itemStack.set(new ItemStack(onChoose.getItem().getType()));
                            open(player);
                        }).open(player);
                    } else
                        open(player);
                }).open(player);
            } else {
                itemStack.set(null);
                open(player);
            }
        };
        if (itemStack.get() != null)
            contents.set(row, 4, ClickableItem.of(itemStack.get(), action));
        else
            contents.set(row, 4, ClickableItem.of(placeholder, action));
    }
    if (contents.get(row, 4).isPresent() && contents.get(row, 4).get().getItem() != null && contents.get(row, 4).get().getItem().equals(placeholder)) {
        contents.set(row, 2, ClickableItem.empty(less8));
        contents.set(row, 3, ClickableItem.empty(less1));
        contents.set(row, 5, ClickableItem.empty(more1));
        contents.set(row, 6, ClickableItem.empty(more8));
    } else {
        contents.set(row, 2, ClickableItem.of(less8, e2 -> contents.get(row, 4).ifPresent(i -> {
            ItemStack item = i.getItem();
            item.setAmount(Math.max(1, Math.min(item.getType().getMaxStackSize(), item.getAmount() == 64 ? 56 : item.getAmount() - 8)));
            open(player);
        })));
        contents.set(row, 3, ClickableItem.of(less1, e2 -> contents.get(row, 4).ifPresent(i -> {
            ItemStack item = i.getItem();
            item.setAmount(Math.max(1, Math.min(item.getType().getMaxStackSize(), item.getAmount() - 1)));
            open(player);
        })));
        contents.set(row, 5, ClickableItem.of(more1, e2 -> contents.get(row, 4).ifPresent(i -> {
            ItemStack item = i.getItem();
            item.setAmount(Math.min(64, Math.min(item.getType().getMaxStackSize(), item.getAmount() + 1)));
            open(player);
        })));
        contents.set(row, 6, ClickableItem.of(more8, e2 -> contents.get(row, 4).ifPresent(i -> {
            ItemStack item = i.getItem();
            item.setAmount(Math.min(64, Math.min(item.getType().getMaxStackSize(), item.getAmount() == 1 ? 8 : item.getAmount() + 8)));
            open(player);
        })));
    }
}
Also used : Shop(gg.projecteden.nexus.models.shop.Shop) ShopProvider(gg.projecteden.nexus.features.shops.providers.common.ShopProvider) Player(org.bukkit.entity.Player) ARROWS(gg.projecteden.nexus.features.menus.SignMenuFactory.ARROWS) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ClickableItem(gg.projecteden.nexus.features.menus.api.ClickableItem) InvalidInputException(gg.projecteden.nexus.framework.exceptions.postconfigured.InvalidInputException) PlayerUtils(gg.projecteden.nexus.utils.PlayerUtils) BigDecimal(java.math.BigDecimal) ItemClickData(gg.projecteden.nexus.features.menus.api.ItemClickData) Material(org.bukkit.Material) Product(gg.projecteden.nexus.models.shop.Shop.Product) RoundingMode(java.math.RoundingMode) Nexus(gg.projecteden.nexus.Nexus) ItemBuilder(gg.projecteden.nexus.utils.ItemBuilder) Shops(gg.projecteden.nexus.features.shops.Shops) StringUtils.pretty(gg.projecteden.nexus.utils.StringUtils.pretty) StringUtils.camelCase(gg.projecteden.nexus.utils.StringUtils.camelCase) InventoryClickEvent(org.bukkit.event.inventory.InventoryClickEvent) InventoryContents(gg.projecteden.nexus.features.menus.api.content.InventoryContents) Utils(gg.projecteden.nexus.utils.Utils) ItemStack(org.bukkit.inventory.ItemStack) Consumer(java.util.function.Consumer) ExchangeType(gg.projecteden.nexus.models.shop.Shop.ExchangeType) Title(gg.projecteden.nexus.features.menus.api.annotations.Title) ItemSetting(gg.projecteden.nexus.utils.ItemBuilder.ItemSetting) Nullables.isNullOrAir(gg.projecteden.nexus.utils.Nullables.isNullOrAir) ShopUtils.prettyMoney(gg.projecteden.nexus.features.shops.ShopUtils.prettyMoney) InvalidInputException(gg.projecteden.nexus.framework.exceptions.postconfigured.InvalidInputException) Function(java.util.function.Function) ItemBuilder(gg.projecteden.nexus.utils.ItemBuilder) ItemClickData(gg.projecteden.nexus.features.menus.api.ItemClickData) ItemStack(org.bukkit.inventory.ItemStack) InvalidInputException(gg.projecteden.nexus.framework.exceptions.postconfigured.InvalidInputException)

Aggregations

Nexus (gg.projecteden.nexus.Nexus)1 ARROWS (gg.projecteden.nexus.features.menus.SignMenuFactory.ARROWS)1 ClickableItem (gg.projecteden.nexus.features.menus.api.ClickableItem)1 ItemClickData (gg.projecteden.nexus.features.menus.api.ItemClickData)1 Title (gg.projecteden.nexus.features.menus.api.annotations.Title)1 InventoryContents (gg.projecteden.nexus.features.menus.api.content.InventoryContents)1 ShopUtils.prettyMoney (gg.projecteden.nexus.features.shops.ShopUtils.prettyMoney)1 Shops (gg.projecteden.nexus.features.shops.Shops)1 ShopProvider (gg.projecteden.nexus.features.shops.providers.common.ShopProvider)1 InvalidInputException (gg.projecteden.nexus.framework.exceptions.postconfigured.InvalidInputException)1 Shop (gg.projecteden.nexus.models.shop.Shop)1 ExchangeType (gg.projecteden.nexus.models.shop.Shop.ExchangeType)1 Product (gg.projecteden.nexus.models.shop.Shop.Product)1 ItemBuilder (gg.projecteden.nexus.utils.ItemBuilder)1 ItemSetting (gg.projecteden.nexus.utils.ItemBuilder.ItemSetting)1 Nullables.isNullOrAir (gg.projecteden.nexus.utils.Nullables.isNullOrAir)1 PlayerUtils (gg.projecteden.nexus.utils.PlayerUtils)1 StringUtils.camelCase (gg.projecteden.nexus.utils.StringUtils.camelCase)1 StringUtils.pretty (gg.projecteden.nexus.utils.StringUtils.pretty)1 Utils (gg.projecteden.nexus.utils.Utils)1