Search in sources :

Example 6 with CustomItemStack

use of io.github.bakedlibs.dough.items.CustomItemStack in project Slimefun4 by Slimefun.

the class AbstractAutoCrafter method showRecipe.

/**
 * This shows the given {@link AbstractRecipe} to the {@link Player} in a preview window.
 *
 * @param p
 *            The {@link Player}
 * @param b
 *            The {@link Block} of the {@link AbstractAutoCrafter}
 * @param recipe
 *            The {@link AbstractRecipe} to show them
 */
@ParametersAreNonnullByDefault
protected void showRecipe(Player p, Block b, AbstractRecipe recipe) {
    Validate.notNull(p, "The Player should not be null");
    Validate.notNull(b, "The Block should not be null");
    Validate.notNull(recipe, "The Recipe should not be null");
    ChestMenu menu = new ChestMenu(getItemName());
    menu.setPlayerInventoryClickable(false);
    menu.setEmptySlotsClickable(false);
    ChestMenuUtils.drawBackground(menu, background);
    ChestMenuUtils.drawBackground(menu, 45, 46, 47, 48, 50, 51, 52, 53);
    if (recipe.isEnabled()) {
        menu.addItem(49, new CustomItemStack(Material.BARRIER, Slimefun.getLocalization().getMessages(p, "messages.auto-crafting.tooltips.enabled")));
        menu.addMenuClickHandler(49, (pl, item, slot, action) -> {
            if (action.isRightClicked()) {
                deleteRecipe(pl, b);
            } else {
                setRecipeEnabled(pl, b, false);
            }
            return false;
        });
    } else {
        menu.addItem(49, new CustomItemStack(HeadTexture.EXCLAMATION_MARK.getAsItemStack(), Slimefun.getLocalization().getMessages(p, "messages.auto-crafting.tooltips.disabled")));
        menu.addMenuClickHandler(49, (pl, item, slot, action) -> {
            if (action.isRightClicked()) {
                deleteRecipe(pl, b);
            } else {
                setRecipeEnabled(pl, b, true);
            }
            return false;
        });
    }
    // This makes the slots cycle through different ingredients
    AsyncRecipeChoiceTask task = new AsyncRecipeChoiceTask();
    recipe.show(menu, task);
    menu.open(p);
    p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
    // Only schedule the task if necessary
    if (!task.isEmpty()) {
        task.start(menu.toInventory());
    }
}
Also used : CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ChestMenu(me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu) AsyncRecipeChoiceTask(io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Example 7 with CustomItemStack

use of io.github.bakedlibs.dough.items.CustomItemStack in project Slimefun4 by Slimefun.

the class SlimefunLocalization method getRecipeTypeItem.

@Nonnull
public ItemStack getRecipeTypeItem(@Nonnull Player p, @Nonnull RecipeType recipeType) {
    Validate.notNull(p, "Player cannot be null!");
    Validate.notNull(recipeType, "Recipe type cannot be null!");
    ItemStack item = recipeType.toItem();
    if (item == null) {
        // Fixes #3088
        return new ItemStack(Material.AIR);
    }
    Language language = getLanguage(p);
    NamespacedKey key = recipeType.getKey();
    return new CustomItemStack(item, meta -> {
        String displayName = getStringOrNull(language, LanguageFile.RECIPES, key.getNamespace() + "." + key.getKey() + ".name");
        // Set the display name if possible, else keep the default item name.
        if (displayName != null) {
            meta.setDisplayName(ChatColor.AQUA + displayName);
        }
        List<String> lore = getStringListOrNull(language, LanguageFile.RECIPES, key.getNamespace() + "." + key.getKey() + ".lore");
        // Set the lore if possible, else keep the default lore.
        if (lore != null) {
            lore.replaceAll(line -> ChatColor.GRAY + line);
            meta.setLore(lore);
        }
        meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
        meta.addItemFlags(ItemFlag.HIDE_ENCHANTS);
    });
}
Also used : NamespacedKey(org.bukkit.NamespacedKey) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) Nonnull(javax.annotation.Nonnull)

Example 8 with CustomItemStack

use of io.github.bakedlibs.dough.items.CustomItemStack in project Slimefun4 by Slimefun.

the class SlimefunAutoCrafter method updateRecipe.

@Override
protected void updateRecipe(@Nonnull Block b, @Nonnull Player p) {
    ItemStack itemInHand = p.getInventory().getItemInMainHand();
    SlimefunItem item = SlimefunItem.getByItem(itemInHand);
    if (item != null && item.getRecipeType().equals(targetRecipeType)) {
        // Fixes #1161
        if (item.canUse(p, true)) {
            AbstractRecipe recipe = AbstractRecipe.of(item, targetRecipeType);
            if (recipe != null) {
                ChestMenu menu = new ChestMenu(getItemName());
                menu.setPlayerInventoryClickable(false);
                menu.setEmptySlotsClickable(false);
                ChestMenuUtils.drawBackground(menu, background);
                ChestMenuUtils.drawBackground(menu, 45, 46, 47, 48, 50, 51, 52, 53);
                menu.addItem(49, new CustomItemStack(Material.CRAFTING_TABLE, ChatColor.GREEN + Slimefun.getLocalization().getMessage(p, "messages.auto-crafting.select")));
                menu.addMenuClickHandler(49, (pl, stack, slot, action) -> {
                    setSelectedRecipe(b, recipe);
                    p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
                    Slimefun.getLocalization().sendMessage(p, "messages.auto-crafting.recipe-set");
                    showRecipe(p, b, recipe);
                    return false;
                });
                AsyncRecipeChoiceTask task = new AsyncRecipeChoiceTask();
                recipe.show(menu, task);
                menu.open(p);
                p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
                if (!task.isEmpty()) {
                    task.start(menu.toInventory());
                }
            } else {
                Slimefun.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
            }
        }
    } else {
        Slimefun.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
    }
}
Also used : CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) ItemStack(org.bukkit.inventory.ItemStack) SlimefunItemStack(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ChestMenu(me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu) AsyncRecipeChoiceTask(io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask)

Example 9 with CustomItemStack

use of io.github.bakedlibs.dough.items.CustomItemStack in project Slimefun4 by Slimefun.

the class AbstractItemNetwork method collectExtractionRequest.

private void collectExtractionRequest(Map<Location, Inventory> inventories, ItemRequest request, BlockMenu terminal, Iterator<ItemRequest> iterator, Set<Location> providers) {
    int slot = request.getSlot();
    ItemStack prevStack = terminal.getItemInSlot(slot);
    if (!(prevStack == null || (prevStack.getAmount() + request.getItem().getAmount() <= prevStack.getMaxStackSize() && SlimefunUtils.isItemSimilar(prevStack, request.getItem(), true, false)))) {
        iterator.remove();
        return;
    }
    ItemStack stack = null;
    ItemStack item = request.getItem();
    for (Location l : providers) {
        Optional<Block> target = getAttachedBlock(l);
        if (target.isPresent()) {
            ItemStack is = CargoUtils.withdraw(this, inventories, l.getBlock(), target.get(), item);
            if (is != null) {
                if (stack == null) {
                    stack = is;
                } else {
                    stack = new CustomItemStack(stack, stack.getAmount() + is.getAmount());
                }
                if (is.getAmount() == item.getAmount()) {
                    break;
                } else {
                    item = new CustomItemStack(item, item.getAmount() - is.getAmount());
                }
            }
        }
    }
    if (stack != null) {
        ItemStack prev = terminal.getItemInSlot(slot);
        if (prev == null) {
            terminal.replaceExistingItem(slot, stack);
        } else {
            terminal.replaceExistingItem(slot, new CustomItemStack(stack, stack.getAmount() + prev.getAmount()));
        }
    }
    iterator.remove();
}
Also used : CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) Block(org.bukkit.block.Block) ItemStack(org.bukkit.inventory.ItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) Location(org.bukkit.Location)

Example 10 with CustomItemStack

use of io.github.bakedlibs.dough.items.CustomItemStack in project Slimefun4 by Slimefun.

the class AbstractItemNetwork method updateTerminal.

@ParametersAreNonnullByDefault
private void updateTerminal(Location l, BlockMenu terminal, int slot, int index, List<ItemStackAndInteger> items) {
    if (items.size() > index) {
        ItemStackAndInteger item = items.get(index);
        ItemStack stack = item.getItem().clone();
        stack.setAmount(1);
        ItemMeta im = stack.getItemMeta();
        List<String> lore = new ArrayList<>();
        lore.add("");
        lore.add(ChatColors.color("&7Stored Items: &f" + NumberUtils.getCompactDouble(item.getInt())));
        if (stack.getMaxStackSize() > 1) {
            int amount = item.getInt() > stack.getMaxStackSize() ? stack.getMaxStackSize() : item.getInt();
            lore.add(ChatColors.color("&7<Left Click: Request 1 | Right Click: Request " + amount + ">"));
        } else {
            lore.add(ChatColors.color("&7<Left Click: Request 1>"));
        }
        lore.add("");
        if (im.hasLore()) {
            lore.addAll(im.getLore());
        }
        im.setLore(lore);
        stack.setItemMeta(im);
        terminal.replaceExistingItem(slot, stack);
        terminal.addMenuClickHandler(slot, (p, sl, is, action) -> {
            int amount = item.getInt() > item.getItem().getMaxStackSize() ? item.getItem().getMaxStackSize() : item.getInt();
            ItemStack requestedItem = new CustomItemStack(item.getItem(), action.isRightClicked() ? amount : 1);
            itemRequests.add(new ItemRequest(l, 44, requestedItem, ItemTransportFlow.WITHDRAW));
            return false;
        });
    } else {
        terminal.replaceExistingItem(slot, terminalPlaceholderItem);
        terminal.addMenuClickHandler(slot, ChestMenuUtils.getEmptyClickHandler());
    }
}
Also used : ArrayList(java.util.ArrayList) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ItemMeta(org.bukkit.inventory.meta.ItemMeta) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault)

Aggregations

CustomItemStack (io.github.bakedlibs.dough.items.CustomItemStack)172 Test (org.junit.jupiter.api.Test)100 DisplayName (org.junit.jupiter.api.DisplayName)90 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)83 ItemStack (org.bukkit.inventory.ItemStack)70 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)43 NamespacedKey (org.bukkit.NamespacedKey)31 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)26 Player (org.bukkit.entity.Player)25 ChestMenu (me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu)22 ItemGroup (io.github.thebusybiscuit.slimefun4.api.items.ItemGroup)20 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)18 Slimefun (io.github.thebusybiscuit.slimefun4.implementation.Slimefun)12 PlayerProfile (io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile)11 FlexItemGroup (io.github.thebusybiscuit.slimefun4.api.items.groups.FlexItemGroup)10 ArrayList (java.util.ArrayList)10 Nonnull (javax.annotation.Nonnull)10 Material (org.bukkit.Material)10 LockedItemGroup (io.github.thebusybiscuit.slimefun4.api.items.groups.LockedItemGroup)9 Location (org.bukkit.Location)9