From 88bc01e5df0cb83fb0604b9774e59ff2ab66cc0c Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 4 Apr 2021 14:45:17 +0100 Subject: [PATCH] Refactored shaped recipes --- .../willfp/eco/core/items/TestableItem.java | 4 +- .../com/willfp/eco/core/recipe/Recipes.java | 39 +++--------- .../core/recipe/recipes/CraftingRecipe.java | 21 +------ .../recipe/recipes/ShapedCraftingRecipe.java | 63 ++++++++++--------- .../willfp/eco/spigot/EcoSpigotPlugin.java | 4 +- ...istener.java => ShapedRecipeListener.java} | 23 ++++--- 6 files changed, 63 insertions(+), 91 deletions(-) rename eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/{RecipeListener.java => ShapedRecipeListener.java} (88%) diff --git a/eco-api/src/main/java/com/willfp/eco/core/items/TestableItem.java b/eco-api/src/main/java/com/willfp/eco/core/items/TestableItem.java index 94089770..ca24b355 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/items/TestableItem.java +++ b/eco-api/src/main/java/com/willfp/eco/core/items/TestableItem.java @@ -13,9 +13,9 @@ public interface TestableItem { boolean matches(@Nullable ItemStack itemStack); /** - * Get a displayed itemstack, for autocraft. + * Get an example item. * - * @return The item, displayed. + * @return The item. */ ItemStack getItem(); } diff --git a/eco-api/src/main/java/com/willfp/eco/core/recipe/Recipes.java b/eco-api/src/main/java/com/willfp/eco/core/recipe/Recipes.java index cab55aec..111261c1 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/recipe/Recipes.java +++ b/eco-api/src/main/java/com/willfp/eco/core/recipe/Recipes.java @@ -2,13 +2,10 @@ package com.willfp.eco.core.recipe; import com.google.common.collect.BiMap; import com.google.common.collect.HashBiMap; -import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe; +import com.willfp.eco.core.recipe.recipes.CraftingRecipe; import lombok.experimental.UtilityClass; -import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; -import org.bukkit.inventory.RecipeChoice; -import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -18,7 +15,7 @@ public class Recipes { /** * Registry of all recipes. */ - private static final BiMap RECIPES = HashBiMap.create(); + private static final BiMap RECIPES = HashBiMap.create(); /** @@ -26,28 +23,8 @@ public class Recipes { * * @param recipe The recipe. */ - public void register(@NotNull final ShapedCraftingRecipe recipe) { + public void register(@NotNull final CraftingRecipe recipe) { RECIPES.forcePut(recipe.getKey(), recipe); - - Bukkit.getServer().removeRecipe(recipe.getKey()); - Bukkit.getServer().removeRecipe(recipe.getDisplayedKey()); - - 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(recipe.getDisplayedKey(), recipe.getOutput()); - displayedRecipe.shape("012", "345", "678"); - for (int i = 0; i < 9; i++) { - char character = String.valueOf(i).toCharArray()[0]; - displayedRecipe.setIngredient(character, new RecipeChoice.ExactChoice(recipe.getDisplayedAtIndex(i))); - } - - Bukkit.getServer().addRecipe(shapedRecipe); - Bukkit.getServer().addRecipe(displayedRecipe); } /** @@ -57,19 +34,19 @@ public class Recipes { * @return The match, or null if not found. */ @Nullable - public ShapedCraftingRecipe getMatch(@NotNull final ItemStack[] matrix) { + public CraftingRecipe getMatch(@NotNull final ItemStack[] matrix) { return RECIPES.values().stream().filter(recipe -> recipe.test(matrix)).findFirst().orElse(null); } /** - * Get shaped recipe by key. + * Get recipe by key. * * @param key The key. - * @return The shaped recipe, or null if not found. + * @return The recipe, or null if not found. */ @Nullable - public ShapedCraftingRecipe getShapedRecipe(@NotNull final NamespacedKey key) { - ShapedCraftingRecipe recipe = RECIPES.get(key); + public CraftingRecipe getRecipe(@NotNull final NamespacedKey key) { + CraftingRecipe recipe = RECIPES.get(key); if (recipe != null) { return recipe; } diff --git a/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/CraftingRecipe.java b/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/CraftingRecipe.java index 7434381f..f1d42344 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/CraftingRecipe.java +++ b/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/CraftingRecipe.java @@ -1,28 +1,13 @@ package com.willfp.eco.core.recipe.recipes; import com.willfp.eco.core.items.TestableItem; -import org.bukkit.Material; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; import org.jetbrains.annotations.NotNull; +import java.util.List; + public interface CraftingRecipe { - /** - * Get item material at a specific index. - * - * @param index The index to check. - * @return The material. - */ - Material getMaterialAtIndex(int index); - - /** - * Get "real" item at specific index. - * - * @param index The index to check. - * @return The item. - */ - ItemStack getDisplayedAtIndex(int index); - /** * Test matrix against recipe. * @@ -41,7 +26,7 @@ public interface CraftingRecipe { * * @return The parts. */ - TestableItem[] getParts(); + List getParts(); /** * Get the recipe key. diff --git a/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/ShapedCraftingRecipe.java b/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/ShapedCraftingRecipe.java index 1b2652c4..599fd2a7 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/ShapedCraftingRecipe.java +++ b/eco-api/src/main/java/com/willfp/eco/core/recipe/recipes/ShapedCraftingRecipe.java @@ -1,24 +1,28 @@ package com.willfp.eco.core.recipe.recipes; -import com.willfp.eco.core.PluginDependent; import com.willfp.eco.core.EcoPlugin; +import com.willfp.eco.core.PluginDependent; import com.willfp.eco.core.items.TestableItem; import com.willfp.eco.core.recipe.Recipes; import com.willfp.eco.core.recipe.parts.EmptyTestableItem; import lombok.Getter; -import org.bukkit.Material; +import org.bukkit.Bukkit; import org.bukkit.NamespacedKey; import org.bukkit.inventory.ItemStack; +import org.bukkit.inventory.RecipeChoice; +import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; -import java.util.Arrays; +import java.util.ArrayList; +import java.util.List; +@SuppressWarnings("deprecation") public final class ShapedCraftingRecipe extends PluginDependent implements CraftingRecipe { /** * Recipe parts. */ @Getter - private final TestableItem[] parts; + private final List parts; /** * The key of the recipe. @@ -40,7 +44,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft private ShapedCraftingRecipe(@NotNull final EcoPlugin plugin, @NotNull final String key, - @NotNull final TestableItem[] parts, + @NotNull final List parts, @NotNull final ItemStack output) { super(plugin); @@ -50,21 +54,11 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft this.output = output; } - @Override - public Material getMaterialAtIndex(final int index) { - return parts[index].getItem().getType(); - } - - @Override - public ItemStack getDisplayedAtIndex(final int index) { - return parts[index].getItem(); - } - @Override public boolean test(@NotNull final ItemStack[] matrix) { boolean matches = true; for (int i = 0; i < 9; i++) { - if (!parts[i].matches(matrix[i])) { + if (!parts.get(i).matches(matrix[i])) { matches = false; } } @@ -75,15 +69,26 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft @Override public void register() { Recipes.register(this); - } - @Override - public String toString() { - return "EcoShapedRecipe{" - + "parts=" + Arrays.toString(parts) - + ", key='" + key + '\'' - + ", output=" + output - + '}'; + Bukkit.getServer().removeRecipe(this.getKey()); + Bukkit.getServer().removeRecipe(this.getDisplayedKey()); + + ShapedRecipe shapedRecipe = new ShapedRecipe(this.getKey(), this.getOutput()); + shapedRecipe.shape("012", "345", "678"); + for (int i = 0; i < 9; i++) { + char character = String.valueOf(i).toCharArray()[0]; + shapedRecipe.setIngredient(character, parts.get(i).getItem().getType()); + } + + ShapedRecipe displayedRecipe = new ShapedRecipe(this.getDisplayedKey(), this.getOutput()); + displayedRecipe.shape("012", "345", "678"); + for (int i = 0; i < 9; i++) { + char character = String.valueOf(i).toCharArray()[0]; + displayedRecipe.setIngredient(character, new RecipeChoice.ExactChoice(parts.get(i).getItem())); + } + + Bukkit.getServer().addRecipe(shapedRecipe); + Bukkit.getServer().addRecipe(displayedRecipe); } /** @@ -102,7 +107,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft /** * The recipe parts. */ - private final TestableItem[] recipeParts = new TestableItem[9]; + private final List recipeParts = new ArrayList<>(9); /** * The output of the recipe. @@ -140,7 +145,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft */ public Builder setRecipePart(@NotNull final RecipePosition position, @NotNull final TestableItem part) { - this.recipeParts[position.getIndex()] = part; + recipeParts.set(position.getIndex(), part); return this; } @@ -153,7 +158,7 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft */ public Builder setRecipePart(final int position, @NotNull final TestableItem part) { - this.recipeParts[position] = part; + recipeParts.set(position, part); return this; } @@ -175,8 +180,8 @@ public final class ShapedCraftingRecipe extends PluginDependent implements Craft */ public ShapedCraftingRecipe build() { for (int i = 0; i < 9; i++) { - if (recipeParts[i] == null) { - recipeParts[i] = new EmptyTestableItem(); + if (recipeParts.get(i) == null) { + recipeParts.set(i, new EmptyTestableItem()); } } diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java index a6cdabf9..7b6d86f5 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/EcoSpigotPlugin.java @@ -34,7 +34,7 @@ import com.willfp.eco.spigot.integrations.antigrief.AntigriefLands; import com.willfp.eco.spigot.integrations.antigrief.AntigriefTowny; import com.willfp.eco.spigot.integrations.antigrief.AntigriefWorldGuard; import com.willfp.eco.spigot.integrations.mcmmo.McmmoIntegrationImpl; -import com.willfp.eco.spigot.recipes.RecipeListener; +import com.willfp.eco.spigot.recipes.ShapedRecipeListener; import com.willfp.eco.util.BlockUtils; import com.willfp.eco.util.SkullUtils; import com.willfp.eco.util.TridentUtils; @@ -72,7 +72,7 @@ public class EcoSpigotPlugin extends EcoPlugin { this.getEventManager().registerListener(new ArmorListener()); this.getEventManager().registerListener(new DispenserArmorListener()); this.getEventManager().registerListener(new EntityDeathByEntityListeners(this)); - this.getEventManager().registerListener(new RecipeListener()); + this.getEventManager().registerListener(new ShapedRecipeListener()); } @Override diff --git a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/RecipeListener.java b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java similarity index 88% rename from eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/RecipeListener.java rename to eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java index c7e07031..3dcf6181 100644 --- a/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/RecipeListener.java +++ b/eco-core/core-plugin/src/main/java/com/willfp/eco/spigot/recipes/ShapedRecipeListener.java @@ -5,6 +5,7 @@ import com.willfp.eco.core.items.CustomItems; import com.willfp.eco.core.items.TestableItem; import com.willfp.eco.core.recipe.Recipes; import com.willfp.eco.core.recipe.parts.MaterialTestableItem; +import com.willfp.eco.core.recipe.recipes.CraftingRecipe; import com.willfp.eco.core.recipe.recipes.ShapedCraftingRecipe; import org.bukkit.Material; import org.bukkit.event.Event; @@ -17,7 +18,7 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.ShapedRecipe; import org.jetbrains.annotations.NotNull; -public class RecipeListener implements Listener { +public class ShapedRecipeListener implements Listener { /** * Called on item craft. * @@ -36,7 +37,7 @@ public class RecipeListener implements Listener { } ItemStack[] matrix = event.getInventory().getMatrix(); - ShapedCraftingRecipe matched = Recipes.getMatch(matrix); + CraftingRecipe matched = Recipes.getMatch(matrix); if (matched == null) { event.getInventory().setResult(new ItemStack(Material.AIR)); @@ -68,7 +69,7 @@ public class RecipeListener implements Listener { } ItemStack[] matrix = event.getInventory().getMatrix(); - ShapedCraftingRecipe matched = Recipes.getMatch(matrix); + CraftingRecipe matched = Recipes.getMatch(matrix); if (matched == null) { event.getInventory().setResult(new ItemStack(Material.AIR)); @@ -99,15 +100,17 @@ public class RecipeListener implements Listener { ShapedRecipe recipe = (ShapedRecipe) event.getRecipe(); - ShapedCraftingRecipe shapedCraftingRecipe = Recipes.getShapedRecipe(recipe.getKey()); + CraftingRecipe craftingRecipe = Recipes.getRecipe(recipe.getKey()); - if (shapedCraftingRecipe == null) { + if (!(craftingRecipe instanceof ShapedCraftingRecipe)) { return; } + ShapedCraftingRecipe shapedCraftingRecipe = (ShapedCraftingRecipe) craftingRecipe; + for (int i = 0; i < 9; i++) { ItemStack itemStack = event.getInventory().getMatrix()[i]; - TestableItem part = shapedCraftingRecipe.getParts()[i]; + TestableItem part = shapedCraftingRecipe.getParts().get(i); if (part instanceof MaterialTestableItem) { if (CustomItems.isCustomItem(itemStack)) { event.getInventory().setResult(new ItemStack(Material.AIR)); @@ -130,15 +133,17 @@ public class RecipeListener implements Listener { ShapedRecipe recipe = (ShapedRecipe) event.getRecipe(); - ShapedCraftingRecipe shapedCraftingRecipe = Recipes.getShapedRecipe(recipe.getKey()); + CraftingRecipe craftingRecipe = Recipes.getRecipe(recipe.getKey()); - if (shapedCraftingRecipe == null) { + if (!(craftingRecipe instanceof ShapedCraftingRecipe)) { return; } + ShapedCraftingRecipe shapedCraftingRecipe = (ShapedCraftingRecipe) craftingRecipe; + for (int i = 0; i < 9; i++) { ItemStack itemStack = event.getInventory().getMatrix()[i]; - TestableItem part = shapedCraftingRecipe.getParts()[i]; + TestableItem part = shapedCraftingRecipe.getParts().get(i); if (part instanceof MaterialTestableItem) { if (CustomItems.isCustomItem(itemStack)) { event.getInventory().setResult(new ItemStack(Material.AIR));