Search in sources :

Example 1 with AsyncRecipeChoiceTask

use of io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask 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 2 with AsyncRecipeChoiceTask

use of io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask 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 3 with AsyncRecipeChoiceTask

use of io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask in project Slimefun4 by Slimefun.

the class VanillaAutoCrafter method updateRecipe.

@Override
protected void updateRecipe(@Nonnull Block b, @Nonnull Player p) {
    ItemStack item = p.getInventory().getItemInMainHand();
    List<Recipe> recipes = getRecipesFor(item);
    if (recipes.isEmpty()) {
        Slimefun.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes");
    } else {
        ChestMenu menu = new ChestMenu(getItemName());
        menu.setPlayerInventoryClickable(false);
        menu.setEmptySlotsClickable(false);
        ChestMenuUtils.drawBackground(menu, background);
        ChestMenuUtils.drawBackground(menu, 45, 47, 48, 50, 51, 53);
        AsyncRecipeChoiceTask task = new AsyncRecipeChoiceTask();
        offerRecipe(p, b, recipes, 0, menu, task);
        menu.open(p);
        task.start(menu.toInventory());
        p.playSound(p.getLocation(), Sound.UI_BUTTON_CLICK, 1, 1);
    }
}
Also used : ShapelessRecipe(org.bukkit.inventory.ShapelessRecipe) ShapedRecipe(org.bukkit.inventory.ShapedRecipe) Recipe(org.bukkit.inventory.Recipe) 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 4 with AsyncRecipeChoiceTask

use of io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask in project Slimefun4 by Slimefun.

the class SurvivalSlimefunGuide method displayItem.

private void displayItem(ChestMenu menu, PlayerProfile profile, Player p, Object item, ItemStack output, RecipeType recipeType, ItemStack[] recipe, AsyncRecipeChoiceTask task) {
    addBackButton(menu, 0, p, profile);
    MenuClickHandler clickHandler = (pl, slot, itemstack, action) -> {
        try {
            if (itemstack != null && itemstack.getType() != Material.BARRIER) {
                displayItem(profile, itemstack, 0, true);
            }
        } catch (Exception | LinkageError x) {
            printErrorMessage(pl, x);
        }
        return false;
    };
    boolean isSlimefunRecipe = item instanceof SlimefunItem;
    for (int i = 0; i < 9; i++) {
        ItemStack recipeItem = getDisplayItem(p, isSlimefunRecipe, recipe[i]);
        menu.addItem(recipeSlots[i], recipeItem, clickHandler);
        if (recipeItem != null && item instanceof MultiBlockMachine) {
            for (Tag<Material> tag : MultiBlock.getSupportedTags()) {
                if (tag.isTagged(recipeItem.getType())) {
                    task.add(recipeSlots[i], tag);
                    break;
                }
            }
        }
    }
    menu.addItem(10, recipeType.getItem(p), ChestMenuUtils.getEmptyClickHandler());
    menu.addItem(16, output, ChestMenuUtils.getEmptyClickHandler());
}
Also used : ItemFlag(org.bukkit.inventory.ItemFlag) MaterialChoice(org.bukkit.inventory.RecipeChoice.MaterialChoice) Arrays(java.util.Arrays) SlimefunGuideSettings(io.github.thebusybiscuit.slimefun4.core.guide.options.SlimefunGuideSettings) Player(org.bukkit.entity.Player) ItemUtils(io.github.bakedlibs.dough.items.ItemUtils) ParametersAreNonnullByDefault(javax.annotation.ParametersAreNonnullByDefault) Inventory(org.bukkit.inventory.Inventory) PlayerProfile(io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile) RecipeType(io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType) Locale(java.util.Locale) SlimefunGuideMode(io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideMode) MinecraftRecipe(io.github.bakedlibs.dough.recipes.MinecraftRecipe) Recipe(org.bukkit.inventory.Recipe) Material(org.bukkit.Material) ChatInput(io.github.bakedlibs.dough.chat.ChatInput) SlimefunGuideImplementation(io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuideImplementation) ItemGroup(io.github.thebusybiscuit.slimefun4.api.items.ItemGroup) ChestMenuUtils(io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils) Sound(org.bukkit.Sound) SlimefunAddon(io.github.thebusybiscuit.slimefun4.api.SlimefunAddon) Research(io.github.thebusybiscuit.slimefun4.api.researches.Research) ChatUtils(io.github.thebusybiscuit.slimefun4.utils.ChatUtils) ItemStack(org.bukkit.inventory.ItemStack) List(java.util.List) Slimefun(io.github.thebusybiscuit.slimefun4.implementation.Slimefun) Optional(java.util.Optional) ChestMenu(me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu) Validate(org.apache.commons.lang.Validate) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) LockedItemGroup(io.github.thebusybiscuit.slimefun4.api.items.groups.LockedItemGroup) SlimefunGuideItem(io.github.thebusybiscuit.slimefun4.utils.itemstack.SlimefunGuideItem) Tag(org.bukkit.Tag) RecipeChoice(org.bukkit.inventory.RecipeChoice) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) AsyncRecipeChoiceTask(io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask) MenuClickHandler(me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.MenuClickHandler) RecipeDisplayItem(io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem) MultiBlock(io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlock) LinkedList(java.util.LinkedList) Nonnull(javax.annotation.Nonnull) FlexItemGroup(io.github.thebusybiscuit.slimefun4.api.items.groups.FlexItemGroup) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) MultiBlockMachine(io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine) SubItemGroup(io.github.thebusybiscuit.slimefun4.api.items.groups.SubItemGroup) ChatColor(org.bukkit.ChatColor) GuideHistory(io.github.thebusybiscuit.slimefun4.core.guide.GuideHistory) SlimefunGuide(io.github.thebusybiscuit.slimefun4.core.guide.SlimefunGuide) MenuClickHandler(me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu.MenuClickHandler) Material(org.bukkit.Material) SlimefunItem(io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem) ItemStack(org.bukkit.inventory.ItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) MultiBlockMachine(io.github.thebusybiscuit.slimefun4.core.multiblocks.MultiBlockMachine)

Example 5 with AsyncRecipeChoiceTask

use of io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask in project Slimefun4 by Slimefun.

the class SurvivalSlimefunGuide method showMinecraftRecipe.

private void showMinecraftRecipe(Recipe[] recipes, int index, ItemStack item, PlayerProfile profile, Player p, boolean addToHistory) {
    Recipe recipe = recipes[index];
    ItemStack[] recipeItems = new ItemStack[9];
    RecipeType recipeType = RecipeType.NULL;
    ItemStack result = null;
    Optional<MinecraftRecipe<? super Recipe>> optional = MinecraftRecipe.of(recipe);
    AsyncRecipeChoiceTask task = new AsyncRecipeChoiceTask();
    if (optional.isPresent()) {
        showRecipeChoices(recipe, recipeItems, task);
        recipeType = new RecipeType(optional.get());
        result = recipe.getResult();
    } else {
        recipeItems = new ItemStack[] { null, null, null, null, new CustomItemStack(Material.BARRIER, "&4We are somehow unable to show you this Recipe :/"), null, null, null, null };
    }
    ChestMenu menu = create(p);
    if (addToHistory) {
        profile.getGuideHistory().add(item, index);
    }
    displayItem(menu, profile, p, item, result, recipeType, recipeItems, task);
    if (recipes.length > 1) {
        for (int i = 27; i < 36; i++) {
            menu.addItem(i, ChestMenuUtils.getBackground(), ChestMenuUtils.getEmptyClickHandler());
        }
        menu.addItem(28, ChestMenuUtils.getPreviousButton(p, index + 1, recipes.length), (pl, slot, action, stack) -> {
            if (index > 0) {
                showMinecraftRecipe(recipes, index - 1, item, profile, p, true);
            }
            return false;
        });
        menu.addItem(34, ChestMenuUtils.getNextButton(p, index + 1, recipes.length), (pl, slot, action, stack) -> {
            if (index < recipes.length - 1) {
                showMinecraftRecipe(recipes, index + 1, item, profile, p, true);
            }
            return false;
        });
    }
    menu.open(p);
    if (!task.isEmpty()) {
        task.start(menu.toInventory());
    }
}
Also used : MinecraftRecipe(io.github.bakedlibs.dough.recipes.MinecraftRecipe) Recipe(org.bukkit.inventory.Recipe) RecipeType(io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType) MinecraftRecipe(io.github.bakedlibs.dough.recipes.MinecraftRecipe) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ItemStack(org.bukkit.inventory.ItemStack) CustomItemStack(io.github.bakedlibs.dough.items.CustomItemStack) ChestMenu(me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu) AsyncRecipeChoiceTask(io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask)

Aggregations

CustomItemStack (io.github.bakedlibs.dough.items.CustomItemStack)6 AsyncRecipeChoiceTask (io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask)6 ChestMenu (me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu)6 ItemStack (org.bukkit.inventory.ItemStack)5 RecipeType (io.github.thebusybiscuit.slimefun4.api.recipes.RecipeType)3 ParametersAreNonnullByDefault (javax.annotation.ParametersAreNonnullByDefault)3 Recipe (org.bukkit.inventory.Recipe)3 MinecraftRecipe (io.github.bakedlibs.dough.recipes.MinecraftRecipe)2 SlimefunItem (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItem)2 SlimefunItemStack (io.github.thebusybiscuit.slimefun4.api.items.SlimefunItemStack)2 RecipeDisplayItem (io.github.thebusybiscuit.slimefun4.core.attributes.RecipeDisplayItem)2 Player (org.bukkit.entity.Player)2 ChatInput (io.github.bakedlibs.dough.chat.ChatInput)1 ItemUtils (io.github.bakedlibs.dough.items.ItemUtils)1 SlimefunAddon (io.github.thebusybiscuit.slimefun4.api.SlimefunAddon)1 ItemGroup (io.github.thebusybiscuit.slimefun4.api.items.ItemGroup)1 FlexItemGroup (io.github.thebusybiscuit.slimefun4.api.items.groups.FlexItemGroup)1 LockedItemGroup (io.github.thebusybiscuit.slimefun4.api.items.groups.LockedItemGroup)1 SubItemGroup (io.github.thebusybiscuit.slimefun4.api.items.groups.SubItemGroup)1 PlayerProfile (io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile)1