From 7c05dd7457e8b58300834d0cdf8740b5af14a05c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 17 Feb 2021 13:20:34 +0000 Subject: [PATCH] Reworked recipes --- .../eco/util/plugin/AbstractEcoPlugin.java | 10 +-- .../willfp/eco/util/recipe/RecipeParts.java | 64 +++++++++++++++ .../{RecipeManager.java => Recipes.java} | 40 ++++------ .../util/recipe/lookup/RecipePartUtils.java | 51 ------------ .../recipe/{ => recipes}/EcoShapedRecipe.java | 79 +++---------------- .../recipe/{ => recipes}/RecipeListener.java | 7 +- .../util/recipe/recipes/RecipePosition.java | 65 +++++++++++++++ 7 files changed, 164 insertions(+), 152 deletions(-) create mode 100644 eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeParts.java rename eco-util/src/main/java/com/willfp/eco/util/recipe/{RecipeManager.java => Recipes.java} (54%) delete mode 100644 eco-util/src/main/java/com/willfp/eco/util/recipe/lookup/RecipePartUtils.java rename eco-util/src/main/java/com/willfp/eco/util/recipe/{ => recipes}/EcoShapedRecipe.java (80%) rename eco-util/src/main/java/com/willfp/eco/util/recipe/{ => recipes}/RecipeListener.java (92%) create mode 100644 eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipePosition.java diff --git a/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java b/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java index 7ce7254f..e4c48537 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java +++ b/eco-util/src/main/java/com/willfp/eco/util/plugin/AbstractEcoPlugin.java @@ -24,8 +24,7 @@ import com.willfp.eco.util.integrations.placeholder.PlaceholderManager; import com.willfp.eco.util.integrations.placeholder.plugins.PlaceholderIntegrationPAPI; import com.willfp.eco.util.optional.Prerequisite; import com.willfp.eco.util.protocollib.AbstractPacketAdapter; -import com.willfp.eco.util.recipe.RecipeListener; -import com.willfp.eco.util.recipe.RecipeManager; +import com.willfp.eco.util.recipe.recipes.RecipeListener; import com.willfp.eco.util.updater.UpdateChecker; import lombok.Getter; import org.apache.maven.artifact.versioning.DefaultArtifactVersion; @@ -144,12 +143,6 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { @Getter private final RunnableFactory runnableFactory; - /** - * Recipe handler for crafting recipes. - */ - @Getter - private final RecipeManager recipeManager; - /** * The loader for all plugin extensions. * @@ -208,7 +201,6 @@ public abstract class AbstractEcoPlugin extends JavaPlugin { this.runnableFactory = new RunnableFactory(this); this.extensionLoader = new EcoExtensionLoader(this); this.configHandler = new ConfigHandler(this); - this.recipeManager = new RecipeManager(this); this.langYml = new Lang(this); this.configYml = new Config(this); diff --git a/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeParts.java b/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeParts.java new file mode 100644 index 00000000..6eca5856 --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeParts.java @@ -0,0 +1,64 @@ +package com.willfp.eco.util.recipe; + +import com.willfp.eco.util.recipe.parts.EmptyRecipePart; +import com.willfp.eco.util.recipe.parts.RecipePart; +import com.willfp.eco.util.recipe.parts.SimpleRecipePart; +import lombok.experimental.UtilityClass; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.inventory.ItemStack; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.util.HashMap; +import java.util.Map; + +@UtilityClass +@SuppressWarnings("deprecation") +public final class RecipeParts { + /** + * All recipe parts. + */ + private static final Map PARTS = new HashMap<>(); + + /** + * Register a new recipe part. + * + * @param key The key of the recipe part. + * @param part The recipe part. + */ + public void registerRecipePart(@NotNull final NamespacedKey key, + @NotNull final RecipePart part) { + PARTS.put(key, part); + } + + /** + * Lookup recipe part from string. + * + * @param key The string to test. + * @return The found recipe part, or null if not found. + */ + @Nullable + public RecipePart lookup(@NotNull final String key) { + String[] split = key.toLowerCase().split(":"); + if (split.length == 1) { + Material material = Material.getMaterial(key.toUpperCase()); + if (material == null || material == Material.AIR) { + return new EmptyRecipePart(); + } + return new SimpleRecipePart(material); + } + + return PARTS.get(new NamespacedKey(split[0], split[1])); + } + + /** + * Get if itemStack is a recipe part (used to check for custom items). + * + * @param itemStack The itemStack to check. + * @return If is recipe. + */ + public boolean isRecipePart(@NotNull final ItemStack itemStack) { + return PARTS.values().stream().anyMatch(recipePart -> recipePart.matches(itemStack)); + } +} diff --git a/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeManager.java b/eco-util/src/main/java/com/willfp/eco/util/recipe/Recipes.java similarity index 54% rename from eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeManager.java rename to eco-util/src/main/java/com/willfp/eco/util/recipe/Recipes.java index db60f9ac..869e9973 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeManager.java +++ b/eco-util/src/main/java/com/willfp/eco/util/recipe/Recipes.java @@ -2,8 +2,8 @@ package com.willfp.eco.util.recipe; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; -import com.willfp.eco.util.internal.PluginDependent; -import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.eco.util.recipe.recipes.EcoShapedRecipe; +import lombok.experimental.UtilityClass; import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; @@ -12,40 +12,34 @@ import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +@UtilityClass @SuppressWarnings("deprecation") -public class RecipeManager extends PluginDependent { +public class Recipes { /** * Registry of all recipes. */ - private final BiMap registry = HashBiMap.create(); + private static final BiMap RECIPES = HashBiMap.create(); + /** - * Pass an {@link AbstractEcoPlugin} in order to interface with it. + * Register a recipe. * - * @param plugin The plugin to manage. + * @param recipe The recipe. */ - public RecipeManager(@NotNull final AbstractEcoPlugin plugin) { - super(plugin); - } + public void register(@NotNull final EcoShapedRecipe recipe) { + RECIPES.forcePut(recipe.getKey(), recipe); - void register(@NotNull final EcoShapedRecipe recipe) { - String key = recipe.getKey(); - registry.forcePut(key, recipe); + Bukkit.getServer().removeRecipe(recipe.getKey()); + Bukkit.getServer().removeRecipe(recipe.getDisplayedKey()); - NamespacedKey baseKey = this.getPlugin().getNamespacedKeyFactory().create(key); - Bukkit.getServer().removeRecipe(baseKey); - - NamespacedKey displayedKey = this.getPlugin().getNamespacedKeyFactory().create(key + "_displayed"); - Bukkit.getServer().removeRecipe(displayedKey); - - ShapedRecipe shapedRecipe = new ShapedRecipe(baseKey, recipe.getOutput()); + ShapedRecipe shapedRecipe = new ShapedRecipe(recipe.getKey(), recipe.getOutput()); shapedRecipe.shape("012", "345", "678"); for (int i = 0; i < 9; i++) { char character = String.valueOf(i).toCharArray()[0]; shapedRecipe.setIngredient(character, recipe.getMaterialAtIndex(i)); } - ShapedRecipe displayedRecipe = new ShapedRecipe(displayedKey, recipe.getOutput()); + ShapedRecipe displayedRecipe = new ShapedRecipe(recipe.getDisplayedKey(), recipe.getOutput()); displayedRecipe.shape("012", "345", "678"); for (int i = 0; i < 9; i++) { char character = String.valueOf(i).toCharArray()[0]; @@ -64,7 +58,7 @@ public class RecipeManager extends PluginDependent { */ @Nullable public EcoShapedRecipe getMatch(@NotNull final ItemStack[] matrix) { - return registry.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null); + return RECIPES.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null); } /** @@ -74,7 +68,7 @@ public class RecipeManager extends PluginDependent { * @return The shaped recipe, or null if not found. */ @Nullable - public EcoShapedRecipe getShapedRecipe(@NotNull final String key) { - return registry.get(key); + public EcoShapedRecipe getShapedRecipe(@NotNull final NamespacedKey key) { + return RECIPES.get(key); } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/recipe/lookup/RecipePartUtils.java b/eco-util/src/main/java/com/willfp/eco/util/recipe/lookup/RecipePartUtils.java deleted file mode 100644 index cf6dab87..00000000 --- a/eco-util/src/main/java/com/willfp/eco/util/recipe/lookup/RecipePartUtils.java +++ /dev/null @@ -1,51 +0,0 @@ -package com.willfp.eco.util.recipe.lookup; - -import com.willfp.eco.util.recipe.parts.EmptyRecipePart; -import com.willfp.eco.util.recipe.parts.RecipePart; -import com.willfp.eco.util.recipe.parts.SimpleRecipePart; -import lombok.experimental.UtilityClass; -import org.bukkit.Material; -import org.jetbrains.annotations.NotNull; - -import java.util.HashMap; -import java.util.Map; -import java.util.function.Function; - -@UtilityClass -public final class RecipePartUtils { - /** - * All recipes. - */ - private static final Map> TESTS = new HashMap<>(); - - /** - * Register a new lookup. - * - * @param key The key of the lookup. - * @param lookupFunction The lookup to register, where the output is the recipe part generated. - */ - public void registerLookup(@NotNull final String key, - @NotNull final Function lookupFunction) { - TESTS.put(key, lookupFunction); - } - - /** - * Lookup recipe part from string. - * - * @param key The string to test. - * @return The generated recipe part, or null if invalid. - */ - public RecipePart lookup(@NotNull final String key) { - Function lookup = TESTS.get(key); - - if (lookup == null) { - Material material = Material.getMaterial(key.toUpperCase()); - if (material == null || material == Material.AIR) { - return new EmptyRecipePart(); - } - return new SimpleRecipePart(material); - } - - return lookup.apply(key); - } -} diff --git a/eco-util/src/main/java/com/willfp/eco/util/recipe/EcoShapedRecipe.java b/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/EcoShapedRecipe.java similarity index 80% rename from eco-util/src/main/java/com/willfp/eco/util/recipe/EcoShapedRecipe.java rename to eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/EcoShapedRecipe.java index a1824fb8..51765975 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/recipe/EcoShapedRecipe.java +++ b/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/EcoShapedRecipe.java @@ -1,12 +1,14 @@ -package com.willfp.eco.util.recipe; +package com.willfp.eco.util.recipe.recipes; import com.willfp.eco.util.interfaces.Registerable; import com.willfp.eco.util.internal.PluginDependent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.eco.util.recipe.Recipes; import com.willfp.eco.util.recipe.parts.EmptyRecipePart; import com.willfp.eco.util.recipe.parts.RecipePart; import lombok.Getter; import org.bukkit.Material; +import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; @@ -23,7 +25,13 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab * The key of the recipe. */ @Getter - private final String key; + private final NamespacedKey key; + + /** + * The key of the displayed recipe. + */ + @Getter + private final NamespacedKey displayedKey; /** * The recipe's output. @@ -38,7 +46,8 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab super(plugin); this.parts = parts; - this.key = key; + this.key = plugin.getNamespacedKeyFactory().create(key); + this.displayedKey = plugin.getNamespacedKeyFactory().create(key + "_displayed"); this.output = output; } @@ -84,7 +93,7 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab */ @Override public void register() { - this.getPlugin().getRecipeManager().register(this); + Recipes.register(this); } @Override @@ -193,66 +202,4 @@ public final class EcoShapedRecipe extends PluginDependent implements Registerab return new EcoShapedRecipe(plugin, key.toLowerCase(), recipeParts, output); } } - - public enum RecipePosition { - /** - * Top left of matrix. - */ - TOP_LEFT(0), - - /** - * Top middle of matrix. - */ - TOP_MIDDLE(1), - - /** - * Top right of matrix. - */ - TOP_RIGHT(2), - - /** - * Middle left of matrix. - */ - MIDDLE_LEFT(3), - - /** - * Middle of matrix. - */ - MIDDLE(4), - - /** - * Middle right of matrix. - */ - MIDDLE_RIGHT(5), - - /** - * Bottom left of matrix. - */ - BOTTOM_LEFT(6), - - /** - * Bottom middle of matrix. - */ - BOTTOM_MIDDLE(7), - - /** - * Bottom right of matrix. - */ - BOTTOM_RIGHT(8); - - /** - * The index within a crafting table matrix. - */ - @Getter - private final int index; - - /** - * Recipe position with crafting table index. - * - * @param index The index. - */ - RecipePosition(final int index) { - this.index = index; - } - } } diff --git a/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeListener.java b/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipeListener.java similarity index 92% rename from eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeListener.java rename to eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipeListener.java index 7b4c54fe..c5c8813a 100644 --- a/eco-util/src/main/java/com/willfp/eco/util/recipe/RecipeListener.java +++ b/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipeListener.java @@ -1,7 +1,8 @@ -package com.willfp.eco.util.recipe; +package com.willfp.eco.util.recipe.recipes; import com.willfp.eco.util.internal.PluginDependent; import com.willfp.eco.util.plugin.AbstractEcoPlugin; +import com.willfp.eco.util.recipe.Recipes; import org.bukkit.Material; import org.bukkit.event.EventHandler; import org.bukkit.event.Listener; @@ -39,7 +40,7 @@ public class RecipeListener extends PluginDependent implements Listener { } ItemStack[] matrix = event.getInventory().getMatrix(); - EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix); + EcoShapedRecipe matched = Recipes.getMatch(matrix); if (matched == null) { event.getInventory().setResult(new ItemStack(Material.AIR)); @@ -71,7 +72,7 @@ public class RecipeListener extends PluginDependent implements Listener { } ItemStack[] matrix = event.getInventory().getMatrix(); - EcoShapedRecipe matched = this.getPlugin().getRecipeManager().getMatch(matrix); + EcoShapedRecipe matched = Recipes.getMatch(matrix); if (matched == null) { event.getInventory().setResult(new ItemStack(Material.AIR)); diff --git a/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipePosition.java b/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipePosition.java new file mode 100644 index 00000000..fcafb06d --- /dev/null +++ b/eco-util/src/main/java/com/willfp/eco/util/recipe/recipes/RecipePosition.java @@ -0,0 +1,65 @@ +package com.willfp.eco.util.recipe.recipes; + +import lombok.Getter; + +public enum RecipePosition { + /** + * Top left of matrix. + */ + TOP_LEFT(0), + + /** + * Top middle of matrix. + */ + TOP_MIDDLE(1), + + /** + * Top right of matrix. + */ + TOP_RIGHT(2), + + /** + * Middle left of matrix. + */ + MIDDLE_LEFT(3), + + /** + * Middle of matrix. + */ + MIDDLE(4), + + /** + * Middle right of matrix. + */ + MIDDLE_RIGHT(5), + + /** + * Bottom left of matrix. + */ + BOTTOM_LEFT(6), + + /** + * Bottom middle of matrix. + */ + BOTTOM_MIDDLE(7), + + /** + * Bottom right of matrix. + */ + BOTTOM_RIGHT(8); + + /** + * The index within a crafting table matrix. + */ + @Getter + private final int index; + + /** + * Recipe position with crafting table index. + * + * @param index The index. + */ + RecipePosition(final int index) { + this.index = index; + } +}