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