From c73edaa72e6a77e377ed2f7597363501cbe790ad Mon Sep 17 00:00:00 2001 From: miozune Date: Sun, 23 Jan 2022 07:37:54 +0900 Subject: [PATCH] Use String key for RecipeCatalyst instead of IRecipeHandler.class (#197) * Use String key for RecipeCatalyst instead of IRecipeHandler.class * deprecate old methods * delete commented out code --- src/main/java/codechicken/nei/api/API.java | 38 +++++++++++++- .../codechicken/nei/recipe/GuiRecipe.java | 2 +- .../nei/recipe/GuiRecipeCatalyst.java | 4 +- .../nei/recipe/IRecipeHandler.java | 9 ++++ .../nei/recipe/RecipeCatalysts.java | 50 ++++++++++++++----- .../codechicken/nei/recipe/RecipeInfo.java | 10 ++-- .../nei/recipe/TemplateRecipeHandler.java | 3 +- 7 files changed, 91 insertions(+), 25 deletions(-) diff --git a/src/main/java/codechicken/nei/api/API.java b/src/main/java/codechicken/nei/api/API.java index 694ccac4..b19fa016 100644 --- a/src/main/java/codechicken/nei/api/API.java +++ b/src/main/java/codechicken/nei/api/API.java @@ -300,7 +300,7 @@ public class API * @param stack the ingredient that can craft recipes (like a furnace or crafting table) * @param handler the recipe category handled by the ingredient */ - public static void addRecipeCatalyst(ItemStack stack, Class handler) { + public static void addRecipeCatalyst(ItemStack stack, IRecipeHandler handler) { addRecipeCatalyst(Collections.singletonList(stack), handler); } @@ -310,8 +310,42 @@ public class API * @param stacks the ingredients that can craft recipes (like a furnace or crafting table) * @param handler the recipe category handled by the ingredient */ + public static void addRecipeCatalyst(List stacks, IRecipeHandler handler) { + String handlerID = handler.getOverlayIdentifier(); + if (handlerID != null) { + addRecipeCatalyst(stacks, handler.getOverlayIdentifier()); + } + else { + NEIClientConfig.logger.warn(String.format("failed to add recipe catalyst, implement IRecipeHandler#getOverlayIdentifier for your handler %s", handler.getClass().getName())); + } + } + + /** + * Adds an association between an ingredient and what it can craft. (i.e. Furnace ItemStack -> Smelting and Fuel Recipes) + * Allows players to see what ingredient they need to craft in order to make recipes from a recipe category. + * @param stack the ingredient that can craft recipes (like a furnace or crafting table) + * @param handlerID recipe category identifier (see {@link IRecipeHandler#getOverlayIdentifier()}) + */ + public static void addRecipeCatalyst(ItemStack stack, String handlerID) { + addRecipeCatalyst(Collections.singletonList(stack), handlerID); + } + + /** + * Adds an association between an ingredient and what it can craft. (i.e. Furnace ItemStack -> Smelting and Fuel Recipes) + * Allows players to see what ingredient they need to craft in order to make recipes from a recipe category. + * @param stacks the ingredients that can craft recipes (like a furnace or crafting table) + * @param handlerID recipe category identifier (see {@link IRecipeHandler#getOverlayIdentifier()}) + */ + public static void addRecipeCatalyst(List stacks, String handlerID) { + RecipeCatalysts.addRecipeCatalyst(stacks, handlerID); + } + + @Deprecated + public static void addRecipeCatalyst(ItemStack stack, Class handler) { + } + + @Deprecated public static void addRecipeCatalyst(List stacks, Class handler) { - RecipeCatalysts.addRecipeCatalyst(stacks, handler); } } diff --git a/src/main/java/codechicken/nei/recipe/GuiRecipe.java b/src/main/java/codechicken/nei/recipe/GuiRecipe.java index d419da30..5520f9c5 100644 --- a/src/main/java/codechicken/nei/recipe/GuiRecipe.java +++ b/src/main/java/codechicken/nei/recipe/GuiRecipe.java @@ -507,7 +507,7 @@ public abstract class GuiRecipe extends GuiContainer implements IGuiContainerOve if (result != null) slotcontainer.addSlot(result, p.x, p.y); - List catalysts = RecipeCatalysts.getRecipeCatalysts(handler.getClass()); + List catalysts = RecipeCatalysts.getRecipeCatalysts(handler); for (PositionedStack catalyst : catalysts) { int xOffset = -GuiRecipeCatalyst.ingredientSize + 1; int yOffset = BG_TOP_Y + GuiRecipeCatalyst.fullBorder; diff --git a/src/main/java/codechicken/nei/recipe/GuiRecipeCatalyst.java b/src/main/java/codechicken/nei/recipe/GuiRecipeCatalyst.java index f2ae4358..25a7d0e7 100644 --- a/src/main/java/codechicken/nei/recipe/GuiRecipeCatalyst.java +++ b/src/main/java/codechicken/nei/recipe/GuiRecipeCatalyst.java @@ -24,7 +24,7 @@ public class GuiRecipeCatalyst extends INEIGuiAdapter { public void draw() { if (guiRecipe == null) return; - int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler().getClass()).size(); + int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler()).size(); if (catalystsSize == 0) return; int availableHeight = RecipeCatalysts.getHeight(); @@ -49,7 +49,7 @@ public class GuiRecipeCatalyst extends INEIGuiAdapter { public boolean hideItemPanelSlot(GuiContainer gui, int x, int y, int w, int h) { if (!(gui instanceof GuiRecipe)) return false; guiRecipe = (GuiRecipe) gui; - int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler().getClass()).size(); + int catalystsSize = RecipeCatalysts.getRecipeCatalysts(guiRecipe.getHandler()).size(); if (catalystsSize == 0) return false; int availableHeight = RecipeCatalysts.getHeight(); diff --git a/src/main/java/codechicken/nei/recipe/IRecipeHandler.java b/src/main/java/codechicken/nei/recipe/IRecipeHandler.java index 36d57653..f35dd36e 100644 --- a/src/main/java/codechicken/nei/recipe/IRecipeHandler.java +++ b/src/main/java/codechicken/nei/recipe/IRecipeHandler.java @@ -89,6 +89,15 @@ public interface IRecipeHandler * @return An instance of {@link IOverlayHandler} to be used for rendering the overlay of this specific recipe. */ public IOverlayHandler getOverlayHandler(GuiContainer gui, int recipe); + /** + * Simply works with the {@link codechicken.nei.api.DefaultOverlayRenderer} + * If the current container has been registered with this identifier, the question mark appears and an overlay guide can be drawn. + * + * @return The overlay identifier of this recipe type. + */ + public default String getOverlayIdentifier() { + return null; + } /** * * @return The number of recipes that can fit on a page in the viewer (1 or 2) diff --git a/src/main/java/codechicken/nei/recipe/RecipeCatalysts.java b/src/main/java/codechicken/nei/recipe/RecipeCatalysts.java index 48ed3b66..3d343887 100644 --- a/src/main/java/codechicken/nei/recipe/RecipeCatalysts.java +++ b/src/main/java/codechicken/nei/recipe/RecipeCatalysts.java @@ -12,39 +12,49 @@ import java.util.List; import java.util.Map; public class RecipeCatalysts { - private static final Map, List> recipeCatalystMap = new HashMap<>(); - private static Map, List> positionedRecipeCatalystMap = new HashMap<>(); + private static final Map> recipeCatalystMap = new HashMap<>(); + private static Map> positionedRecipeCatalystMap = new HashMap<>(); private static int heightCache; - public static void addRecipeCatalyst(List stacks, Class handler) { - if (recipeCatalystMap.containsKey(handler)) { - recipeCatalystMap.get(handler).addAll(stacks); + public static void addRecipeCatalyst(List stacks, String handlerID) { + if (handlerID == null) return; + if (recipeCatalystMap.containsKey(handlerID)) { + recipeCatalystMap.get(handlerID).addAll(stacks); } else { - recipeCatalystMap.put(handler, new ArrayList<>(stacks)); + // use ArrayList initializer to prevent UOE + recipeCatalystMap.put(handlerID, new ArrayList<>(stacks)); } } - public static Map, List> getPositionedRecipeCatalystMap() { + public static Map> getPositionedRecipeCatalystMap() { return positionedRecipeCatalystMap; } - public static List getRecipeCatalysts(Class handler) { + public static List getRecipeCatalysts(IRecipeHandler handler) { + return getRecipeCatalysts(handler.getOverlayIdentifier()); + } + + public static List getRecipeCatalysts(String handlerID) { if (!NEIClientConfig.areJEIStyleTabsVisible() || !NEIClientConfig.areJEIStyleRecipeCatalystsVisible()) { return Collections.emptyList(); } - return positionedRecipeCatalystMap.getOrDefault(handler, Collections.emptyList()); + return positionedRecipeCatalystMap.getOrDefault(handlerID, Collections.emptyList()); } - public static boolean containsCatalyst(Class handler, ItemStack candidate) { - return recipeCatalystMap.getOrDefault(handler, Collections.emptyList()).stream() + public static boolean containsCatalyst(IRecipeHandler handler, ItemStack candidate) { + return containsCatalyst(handler.getOverlayIdentifier(), candidate); + } + + public static boolean containsCatalyst(String handlerID, ItemStack candidate) { + return recipeCatalystMap.getOrDefault(handlerID, Collections.emptyList()).stream() .anyMatch(s -> NEIServerUtils.areStacksSameType(s, candidate)); } public static void updatePosition(int availableHeight) { if (availableHeight == heightCache) return; - Map, List> newMap = new HashMap<>(); - for (Map.Entry, List> entry : recipeCatalystMap.entrySet()) { + Map> newMap = new HashMap<>(); + for (Map.Entry> entry : recipeCatalystMap.entrySet()) { List catalysts = entry.getValue(); List newStacks = new ArrayList<>(); int rowCount = getRowCount(availableHeight, catalysts.size()); @@ -75,4 +85,18 @@ public class RecipeCatalysts { return NEIServerUtils.divideCeil(catalystsSize, columnCount); } + @Deprecated + public static void addRecipeCatalyst(List stacks, Class handler) { + } + + @Deprecated + public static List getRecipeCatalysts(Class handler) { + return Collections.emptyList(); + } + + @Deprecated + public static boolean containsCatalyst(Class handler, ItemStack candidate) { + return false; + } + } diff --git a/src/main/java/codechicken/nei/recipe/RecipeInfo.java b/src/main/java/codechicken/nei/recipe/RecipeInfo.java index fe6a224d..85a13aef 100644 --- a/src/main/java/codechicken/nei/recipe/RecipeInfo.java +++ b/src/main/java/codechicken/nei/recipe/RecipeInfo.java @@ -123,12 +123,10 @@ public class RecipeInfo API.registerRecipeHandler(new ProfilerRecipeHandler(true)); API.registerUsageHandler(new ProfilerRecipeHandler(false)); - API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), ShapedRecipeHandler.class); - API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), ShapelessRecipeHandler.class); - API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), FireworkRecipeHandler.class); - API.addRecipeCatalyst(new ItemStack(Blocks.furnace), FurnaceRecipeHandler.class); - API.addRecipeCatalyst(new ItemStack(Items.brewing_stand), BrewingRecipeHandler.class); - API.addRecipeCatalyst(new ItemStack(Blocks.furnace), FuelRecipeHandler.class); + API.addRecipeCatalyst(new ItemStack(Blocks.crafting_table), "crafting"); + API.addRecipeCatalyst(new ItemStack(Blocks.furnace), new FurnaceRecipeHandler()); + API.addRecipeCatalyst(new ItemStack(Items.brewing_stand), new BrewingRecipeHandler()); + API.addRecipeCatalyst(new ItemStack(Blocks.furnace), new FuelRecipeHandler()); } } diff --git a/src/main/java/codechicken/nei/recipe/TemplateRecipeHandler.java b/src/main/java/codechicken/nei/recipe/TemplateRecipeHandler.java index e8ead7c4..82fb9312 100644 --- a/src/main/java/codechicken/nei/recipe/TemplateRecipeHandler.java +++ b/src/main/java/codechicken/nei/recipe/TemplateRecipeHandler.java @@ -442,6 +442,7 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH * * @return The overlay identifier of this recipe type. */ + @Override public String getOverlayIdentifier() { return null; } @@ -559,7 +560,7 @@ public abstract class TemplateRecipeHandler implements ICraftingHandler, IUsageH TemplateRecipeHandler handler = newInstance(); if (inputId.equals("item")) { ItemStack candidate = (ItemStack) ingredients[0]; - if (RecipeCatalysts.containsCatalyst(handler.getClass(), candidate)) { + if (RecipeCatalysts.containsCatalyst(handler, candidate)) { for (RecipeTransferRect rect : transferRects) { if (specifyTransferRect() == null || Objects.equals(rect.outputId, specifyTransferRect())) { handler.loadCraftingRecipes(rect.outputId, rect.results);