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 be5cd0273..388f608a6 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 @@ -6,10 +6,12 @@ import java.util.List; import java.util.function.Predicate; import javax.annotation.Nonnull; +import javax.annotation.Nullable; 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; @@ -28,11 +30,22 @@ public class AbstractRecipe { this.result = result; } - public AbstractRecipe(@Nonnull ShapelessRecipe recipe) { + @Nullable + public static AbstractRecipe wrapRecipe(@Nullable Recipe recipe) { + if (recipe instanceof ShapedRecipe) { + return new AbstractRecipe((ShapedRecipe) recipe); + } else if (recipe instanceof ShapelessRecipe) { + return new AbstractRecipe((ShapelessRecipe) recipe); + } else { + return null; + } + } + + private AbstractRecipe(@Nonnull ShapelessRecipe recipe) { this(new ArrayList<>(recipe.getChoiceList()), recipe.getResult()); } - public AbstractRecipe(@Nonnull ShapedRecipe recipe) { + private AbstractRecipe(@Nonnull ShapedRecipe recipe) { this(getChoices(recipe), recipe.getResult()); } diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java new file mode 100644 index 000000000..e8dce2137 --- /dev/null +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/implementation/items/electric/machines/auto_crafters/VanillaAutoCrafter.java @@ -0,0 +1,87 @@ +package io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters; + +import java.util.function.Predicate; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import javax.annotation.ParametersAreNonnullByDefault; + +import org.bukkit.Bukkit; +import org.bukkit.NamespacedKey; +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 org.bukkit.inventory.Recipe; + +import io.github.thebusybiscuit.cscorelib2.data.PersistentDataAPI; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.utils.PatternUtils; +import io.papermc.lib.PaperLib; +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; + +public class VanillaAutoCrafter extends AbstractAutoCrafter { + + private final NamespacedKey recipeStorageKey; + + @ParametersAreNonnullByDefault + public VanillaAutoCrafter(Category category, SlimefunItemStack item, RecipeType recipeType, ItemStack[] recipe) { + super(category, item, recipeType, recipe); + + recipeStorageKey = new NamespacedKey(SlimefunPlugin.instance(), "recipe_key"); + } + + @Override + @Nullable + public AbstractRecipe getSelectedRecipe(@Nonnull Block b) { + return AbstractRecipe.wrapRecipe(getRecipe(b)); + } + + @Nullable + private Recipe getRecipe(@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); + + if (value != null) { + String[] values = PatternUtils.COLON.split(value); + + /* + * Normally this constructor should not be used. + * But it is completely fine for this purpose since we only use + * it for lookups. + */ + @SuppressWarnings("deprecation") + NamespacedKey key = new NamespacedKey(values[0], values[1]); + + return Bukkit.getRecipe(key); + } + } + + return null; + } + + @Override + protected boolean matches(@Nonnull ItemStack item, @Nonnull Predicate predicate) { + SlimefunItem sfItem = SlimefunItem.getByItem(item); + + // Slimefunitems should be ignored (unless allowed) + if (sfItem == null || sfItem.isUseableInWorkbench()) { + return super.matches(item, predicate); + } else { + return false; + } + } + + @Override + protected void onRightClick(@Nonnull Player p, @Nonnull Block b) { + // TODO: Implement Recipe Chooser + } + +}