diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractRecipe.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractRecipe.java index 10a53d26c..1cb454871 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractRecipe.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/AbstractRecipe.java @@ -29,7 +29,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; * * @see AbstractAutoCrafter * @see VanillaRecipe - * @see EnhancedRecipe + * @see SlimefunItemRecipe * */ public abstract class AbstractRecipe { @@ -120,18 +120,20 @@ public abstract class AbstractRecipe { /** * This static accessor is for {@link SlimefunItem} recipes. - * Note that the {@link SlimefunItem} must be crafted using an {@link EnhancedCraftingTable}, + * Note that the {@link SlimefunItem} must be crafted using the specified {@link RecipeType}, * otherwise null will be returned. * * @param item * The {@link SlimefunItem} the recipe belongs to + * @param recipeType + * The {@link RecipeType} * * @return The wrapped {@link AbstractRecipe} or null */ @Nullable - public static AbstractRecipe of(@Nullable SlimefunItem item) { - if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) { - return new EnhancedRecipe(item); + public static AbstractRecipe of(@Nullable SlimefunItem item, @Nonnull RecipeType recipeType) { + if (item != null && item.getRecipeType().equals(recipeType)) { + return new SlimefunItemRecipe(item); } else { return null; } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java index 701c5f4f1..c9ca4c72e 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedAutoCrafter.java @@ -1,29 +1,12 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; import javax.annotation.ParametersAreNonnullByDefault; -import org.bukkit.ChatColor; -import org.bukkit.Material; -import org.bukkit.Sound; -import org.bukkit.block.Block; -import org.bukkit.block.BlockState; -import org.bukkit.block.Skull; -import org.bukkit.entity.Player; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; -import io.github.thebusybiscuit.cscorelib2.item.CustomItem; -import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable; -import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask; -import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; -import io.papermc.lib.PaperLib; -import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.Category; -import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; /** @@ -34,77 +17,14 @@ import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; * * @see AbstractAutoCrafter * @see VanillaAutoCrafter - * @see EnhancedRecipe + * @see SlimefunItemRecipe * */ -public class EnhancedAutoCrafter extends AbstractAutoCrafter { +public class EnhancedAutoCrafter extends SlimefunAutoCrafter { @ParametersAreNonnullByDefault public EnhancedAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { - super(category, item, recipeType, recipe); - } - - @Override - @Nullable - public AbstractRecipe getSelectedRecipe(@Nonnull Block b) { - BlockState state = PaperLib.getBlockState(b, false).getState(); - - if (state instanceof Skull) { - // Read the stored value from persistent data storage - String value = PersistentDataAPI.getString((Skull) state, recipeStorageKey); - SlimefunItem item = SlimefunItem.getByID(value); - - if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) { - return AbstractRecipe.of(item); - } - } - - return null; - } - - @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(RecipeType.ENHANCED_CRAFTING_TABLE)) { - // Fixes #1161 - if (item.canUse(p, true)) { - AbstractRecipe recipe = AbstractRecipe.of(item); - - 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 CustomItem(Material.CRAFTING_TABLE, ChatColor.GREEN + SlimefunPlugin.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); - SlimefunPlugin.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 { - SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes"); - } - } - } else { - SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes"); - } + super(category, item, recipeType, recipe, RecipeType.ENHANCED_CRAFTING_TABLE); } } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/SlimefunAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/SlimefunAutoCrafter.java new file mode 100644 index 000000000..b8c2b7fd5 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/SlimefunAutoCrafter.java @@ -0,0 +1,116 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.bukkit.ChatColor; +import org.bukkit.Material; +import org.bukkit.Sound; +import org.bukkit.block.Block; +import org.bukkit.block.BlockState; +import org.bukkit.block.Skull; +import org.bukkit.entity.Player; +import org.bukkit.inventory.ItemStack; + +import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask; +import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; +import io.papermc.lib.PaperLib; +import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; +import me.mrCookieSlime.Slimefun.Lists.RecipeType; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; +import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; + +/** + * This extension of the {@link AbstractAutoCrafter} allows you to implement any + * {@link RecipeType}. + * The concrete implementation for this can be seen in the {@link EnhancedAutoCrafter} but + * it theoretically works for any {@link RecipeType}. + * + * @author TheBusyBiscuit + * + * @see EnhancedAutoCrafter + * + */ +public class SlimefunAutoCrafter extends AbstractAutoCrafter { + + /** + * The targeted {@link RecipeType} that is being crafted here. + */ + private final RecipeType targetRecipeType; + + @ParametersAreNonnullByDefault + protected SlimefunAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe, RecipeType targetRecipeType) { + super(category, item, recipeType, recipe); + + this.targetRecipeType = targetRecipeType; + } + + @Override + @Nullable + public AbstractRecipe getSelectedRecipe(@Nonnull Block b) { + BlockState state = PaperLib.getBlockState(b, false).getState(); + + if (state instanceof Skull) { + // Read the stored value from persistent data storage + String value = PersistentDataAPI.getString((Skull) state, recipeStorageKey); + SlimefunItem item = SlimefunItem.getByID(value); + + if (item != null) { + return AbstractRecipe.of(item, targetRecipeType); + } + } + + return null; + } + + @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 CustomItem(Material.CRAFTING_TABLE, ChatColor.GREEN + SlimefunPlugin.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); + SlimefunPlugin.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 { + SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes"); + } + } + } else { + SlimefunPlugin.getLocalization().sendMessage(p, "messages.auto-crafting.no-recipes"); + } + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedRecipe.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/SlimefunItemRecipe.java similarity index 86% rename from src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedRecipe.java rename to src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/SlimefunItemRecipe.java index f8e4c1281..acadd73f0 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedRecipe.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/SlimefunItemRecipe.java @@ -9,29 +9,28 @@ import javax.annotation.Nonnull; import org.bukkit.inventory.ItemStack; -import io.github.thebusybiscuit.slimefun4.implementation.items.multiblocks.EnhancedCraftingTable; import io.github.thebusybiscuit.slimefun4.implementation.tasks.AsyncRecipeChoiceTask; import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; import io.github.thebusybiscuit.slimefun4.utils.SlimefunUtils; import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; +import me.mrCookieSlime.Slimefun.Lists.RecipeType; import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem; /** * This {@link AbstractRecipe} implementation stands for a {@link SlimefunItem} which - * is crafted using an {@link EnhancedCraftingTable}. + * is crafted using any {@link RecipeType}. * * @author TheBusyBiscuit * - * @see EnhancedCraftingTable - * @see EnhancedAutoCrafter + * @see SlimefunAutoCrafter * */ -class EnhancedRecipe extends AbstractRecipe { +class SlimefunItemRecipe extends AbstractRecipe { private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 }; private final SlimefunItem item; - EnhancedRecipe(@Nonnull SlimefunItem item) { + SlimefunItemRecipe(@Nonnull SlimefunItem item) { super(getInputs(item), item.getRecipeOutput()); this.item = item; }