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 accbcb5d4..42a0ec35b 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 @@ -1,8 +1,6 @@ package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; -import java.util.ArrayList; import java.util.Collection; -import java.util.List; import java.util.function.Predicate; import javax.annotation.Nonnull; @@ -12,13 +10,10 @@ import javax.annotation.ParametersAreNonnullByDefault; import org.apache.commons.lang.Validate; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.Recipe; -import org.bukkit.inventory.RecipeChoice; import org.bukkit.inventory.ShapedRecipe; import org.bukkit.inventory.ShapelessRecipe; -import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask; -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; @@ -37,88 +32,6 @@ public abstract class AbstractRecipe { this.result = result; } - @Nullable - public static AbstractRecipe of(@Nullable Recipe recipe) { - if (recipe instanceof ShapedRecipe) { - return new AbstractRecipe((ShapedRecipe) recipe) { - - @Override - public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) { - // TODO Auto-generated method stub - - } - - }; - } else if (recipe instanceof ShapelessRecipe) { - return new AbstractRecipe((ShapelessRecipe) recipe) { - - @Override - public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) { - // TODO Auto-generated method stub - - } - - }; - } else { - return null; - } - } - - @Nullable - public static AbstractRecipe of(@Nonnull SlimefunItem item) { - return new AbstractRecipe(item) { - - @Override - public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) { - // TODO Auto-generated method stub - - } - }; - } - - private AbstractRecipe(@Nonnull SlimefunItem item) { - Validate.notNull(item, "The SlimefunItem should not be null!"); - Validate.isTrue(item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE), "The item must be crafted in an enhanced crafting table!"); - - result = item.getRecipeOutput(); - inputs = new ArrayList<>(); - - for (int i = 0; i < 9; i++) { - ItemStack ingredient = item.getRecipe()[i]; - inputs.add(stack -> SlimefunUtils.isItemSimilar(stack, ingredient, true)); - } - } - - private AbstractRecipe(@Nonnull ShapelessRecipe recipe) { - this(new ArrayList<>(recipe.getChoiceList()), recipe.getResult()); - } - - private AbstractRecipe(@Nonnull ShapedRecipe recipe) { - this(getChoices(recipe), recipe.getResult()); - } - - @Nonnull - private static Collection> getChoices(@Nonnull ShapedRecipe recipe) { - List> choices = new ArrayList<>(); - - for (String row : recipe.getShape()) { - for (char c : row.toCharArray()) { - RecipeChoice choice = recipe.getChoiceMap().get(c); - - if (choice != null) { - choices.add(choice); - } - } - } - - return choices; - } - - @Nonnull - private static RecipeChoice[] getShape(@Nonnull Recipe recipe) { - return SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe); - } - @Nonnull public Collection> getInputs() { return inputs; @@ -131,4 +44,24 @@ public abstract class AbstractRecipe { public abstract void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task); + @Nullable + public static AbstractRecipe of(@Nullable Recipe recipe) { + if (recipe instanceof ShapedRecipe) { + return new VanillaRecipe((ShapedRecipe) recipe); + } else if (recipe instanceof ShapelessRecipe) { + return new VanillaRecipe((ShapelessRecipe) recipe); + } else { + return null; + } + } + + @Nullable + public static AbstractRecipe of(@Nullable SlimefunItem item) { + if (item != null && item.getRecipeType().equals(RecipeType.ENHANCED_CRAFTING_TABLE)) { + return new EnhancedRecipe(item); + } else { + return null; + } + } + } 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/EnhancedRecipe.java new file mode 100644 index 000000000..ebf0320b9 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/EnhancedRecipe.java @@ -0,0 +1,51 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; + +import java.util.ArrayList; + +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.RecipeChoiceTask; +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.Objects.SlimefunItem.SlimefunItem; + +/** + * This {@link AbstractRecipe} implementation stands for a {@link SlimefunItem} which + * is crafted using an {@link EnhancedCraftingTable}. + * + * @author TheBusyBiscuit + * + * @see EnhancedCraftingTable + * @see EnhancedAutoCrafter + * + */ +class EnhancedRecipe extends AbstractRecipe { + + private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 }; + private final SlimefunItem item; + + EnhancedRecipe(@Nonnull SlimefunItem item) { + super(new ArrayList<>(), item.getRecipeOutput()); + this.item = item; + + for (int i = 0; i < 9; i++) { + ItemStack ingredient = item.getRecipe()[i]; + getInputs().add(stack -> SlimefunUtils.isItemSimilar(stack, ingredient, true)); + } + } + + @Override + public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) { + menu.addItem(24, getResult().clone(), ChestMenuUtils.getEmptyClickHandler()); + ItemStack[] recipe = item.getRecipe(); + + for (int i = 0; i < 9; i++) { + menu.addItem(slots[i], recipe[i], ChestMenuUtils.getEmptyClickHandler()); + } + } + +} diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaRecipe.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaRecipe.java new file mode 100644 index 000000000..1afa449c7 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaRecipe.java @@ -0,0 +1,99 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.function.Predicate; + +import javax.annotation.Nonnull; + +import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.Recipe; +import org.bukkit.inventory.RecipeChoice; +import org.bukkit.inventory.RecipeChoice.MaterialChoice; +import org.bukkit.inventory.ShapedRecipe; +import org.bukkit.inventory.ShapelessRecipe; + +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.implementation.tasks.RecipeChoiceTask; +import io.github.thebusybiscuit.slimefun4.utils.ChestMenuUtils; +import me.mrCookieSlime.CSCoreLibPlugin.general.Inventory.ChestMenu; + +/** + * The {@link VanillaRecipe} implements an {@link AbstractRecipe} and represents a + * {@link ShapedRecipe} or {@link ShapelessRecipe}. + * + * @author TheBusyBiscuit + * + * @see VanillaAutoCrafter + * + */ +class VanillaRecipe extends AbstractRecipe { + + private final int[] slots = { 11, 12, 13, 20, 21, 22, 29, 30, 31 }; + private final Recipe recipe; + + VanillaRecipe(@Nonnull ShapelessRecipe recipe) { + super(new ArrayList<>(recipe.getChoiceList()), recipe.getResult()); + + this.recipe = recipe; + } + + VanillaRecipe(@Nonnull ShapedRecipe recipe) { + super(getChoices(recipe), recipe.getResult()); + + this.recipe = recipe; + } + + @Nonnull + private static Collection> getChoices(@Nonnull ShapedRecipe recipe) { + List> choices = new ArrayList<>(); + + for (String row : recipe.getShape()) { + for (char c : row.toCharArray()) { + RecipeChoice choice = recipe.getChoiceMap().get(c); + + if (choice != null) { + choices.add(choice); + } + } + } + + return choices; + } + + @Nonnull + private static RecipeChoice[] getShape(@Nonnull Recipe recipe) { + return SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe); + } + + @Override + public void show(@Nonnull ChestMenu menu, @Nonnull RecipeChoiceTask task) { + menu.addItem(24, getResult().clone(), ChestMenuUtils.getEmptyClickHandler()); + RecipeChoice[] choices = SlimefunPlugin.getMinecraftRecipeService().getRecipeShape(recipe); + ItemStack[] items = new ItemStack[9]; + + if (choices.length == 1 && choices[0] instanceof MaterialChoice) { + items[4] = new ItemStack(((MaterialChoice) choices[0]).getChoices().get(0)); + + if (((MaterialChoice) choices[0]).getChoices().size() > 1) { + task.add(slots[4], (MaterialChoice) choices[0]); + } + } else { + for (int i = 0; i < choices.length; i++) { + if (choices[i] instanceof MaterialChoice) { + items[i] = new ItemStack(((MaterialChoice) choices[i]).getChoices().get(0)); + + if (((MaterialChoice) choices[i]).getChoices().size() > 1) { + task.add(slots[i], (MaterialChoice) choices[i]); + } + } + } + } + + for (int i = 0; i < 9; i++) { + menu.addItem(slots[i], items[i], ChestMenuUtils.getEmptyClickHandler()); + } + } + +} diff --git a/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java b/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java index 084a6bbe8..8ba0a28df 100644 --- a/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java +++ b/src/main/java/me/mrCookieSlime/Slimefun/Lists/RecipeType.java @@ -9,6 +9,8 @@ import java.util.Locale; import java.util.Set; import java.util.function.BiConsumer; +import javax.annotation.ParametersAreNonnullByDefault; + import org.bukkit.ChatColor; import org.bukkit.Keyed; import org.bukkit.Material; @@ -138,16 +140,32 @@ public class RecipeType implements Keyed { } @Override - public NamespacedKey getKey() { + public final NamespacedKey getKey() { return key; } + @Override + public final boolean equals(Object obj) { + if (obj instanceof RecipeType) { + return ((RecipeType) obj).getKey().equals(this.getKey()); + } else { + return false; + } + } + + @Override + public final int hashCode() { + return getKey().hashCode(); + } + + @ParametersAreNonnullByDefault private static void registerBarterDrop(ItemStack[] recipe, ItemStack output) { if (SlimefunPlugin.getMinecraftVersion().isAtLeast(MinecraftVersion.MINECRAFT_1_16)) { SlimefunPlugin.getRegistry().getBarteringDrops().add(output); } } + @ParametersAreNonnullByDefault private static void registerMobDrop(ItemStack[] recipe, ItemStack output) { String mob = ChatColor.stripColor(recipe[4].getItemMeta().getDisplayName()).toUpperCase(Locale.ROOT).replace(' ', '_'); EntityType entity = EntityType.valueOf(mob);