diff --git a/src/main/java/com/willfp/eco/util/recipes/lookup/RecipePartUtils.java b/src/main/java/com/willfp/eco/util/recipes/lookup/RecipePartUtils.java index 68c43267..51cf64e4 100644 --- a/src/main/java/com/willfp/eco/util/recipes/lookup/RecipePartUtils.java +++ b/src/main/java/com/willfp/eco/util/recipes/lookup/RecipePartUtils.java @@ -1,17 +1,22 @@ package com.willfp.eco.util.recipes.lookup; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.eco.util.recipes.parts.ComplexRecipePart; import com.willfp.eco.util.recipes.parts.EmptyRecipePart; import com.willfp.eco.util.recipes.parts.RecipePart; +import com.willfp.eco.util.recipes.parts.SimpleRecipePart; import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; -import org.bukkit.entity.Player; +import org.bukkit.Material; +import org.bukkit.inventory.ItemStack; import org.bukkit.plugin.ServicePriority; +import org.bukkit.scoreboard.ScoreboardManager; import org.jetbrains.annotations.NotNull; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.function.Function; +import java.util.function.Predicate; @UtilityClass public final class RecipePartUtils { @@ -42,10 +47,33 @@ public final class RecipePartUtils { * @return The generated recipe part, or null if invalid. */ public RecipePart lookup(@NotNull final String key) { + Object recipePartUncast = null; + try { - return (RecipePart) lookupMethod.invoke(instance, key); + recipePartUncast = lookupMethod.invoke(instance, key); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); + return new EmptyRecipePart(); + } + + if (recipePartUncast.getClass().toString().contains("EmptyRecipePart")) { + return new EmptyRecipePart(); + } + + try { + if (recipePartUncast.getClass().toString().contains("SimpleRecipePart")) { + Material material = (Material) recipePartUncast.getClass().getDeclaredMethod("getMaterial").invoke(recipePartUncast); + return new SimpleRecipePart(material); + } + + if (recipePartUncast.getClass().toString().contains("ComplexRecipePart")) { + Predicate predicate = (Predicate) recipePartUncast.getClass().getDeclaredMethod("getPredicate").invoke(recipePartUncast); + ItemStack displayed = (ItemStack) recipePartUncast.getClass().getDeclaredMethod("getDisplayed").invoke(recipePartUncast); + + return new ComplexRecipePart(predicate, displayed); + } + } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) { + e.printStackTrace(); } return new EmptyRecipePart(); diff --git a/src/main/java/com/willfp/eco/util/recipes/parts/ComplexRecipePart.java b/src/main/java/com/willfp/eco/util/recipes/parts/ComplexRecipePart.java index 18e31077..b800a2ea 100644 --- a/src/main/java/com/willfp/eco/util/recipes/parts/ComplexRecipePart.java +++ b/src/main/java/com/willfp/eco/util/recipes/parts/ComplexRecipePart.java @@ -1,5 +1,6 @@ package com.willfp.eco.util.recipes.parts; +import lombok.Getter; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -10,11 +11,13 @@ public class ComplexRecipePart implements RecipePart { /** * The test for itemstacks to pass. */ + @Getter private final Predicate predicate; /** * Displayed itemstack: what the user should see. */ + @Getter private final ItemStack displayed; /** diff --git a/src/main/java/com/willfp/eco/util/recipes/parts/SimpleRecipePart.java b/src/main/java/com/willfp/eco/util/recipes/parts/SimpleRecipePart.java index 1a37c011..7734a49d 100644 --- a/src/main/java/com/willfp/eco/util/recipes/parts/SimpleRecipePart.java +++ b/src/main/java/com/willfp/eco/util/recipes/parts/SimpleRecipePart.java @@ -1,5 +1,6 @@ package com.willfp.eco.util.recipes.parts; +import lombok.Getter; import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -9,6 +10,7 @@ public class SimpleRecipePart implements RecipePart { /** * The material. */ + @Getter private final Material material; /**