Skip to content

Commit

Permalink
Use String key for RecipeCatalyst instead of IRecipeHandler.class (#197)
Browse files Browse the repository at this point in the history
* Use String key for RecipeCatalyst instead of IRecipeHandler.class

* deprecate old methods

* delete commented out code
  • Loading branch information
miozune authored Jan 22, 2022
1 parent caebccc commit c73edaa
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 25 deletions.
38 changes: 36 additions & 2 deletions src/main/java/codechicken/nei/api/API.java
Original file line number Diff line number Diff line change
Expand Up @@ -300,7 +300,7 @@ public static void registerStackStringifyHandler(IStackStringifyHandler handler)
* @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<? extends IRecipeHandler> handler) {
public static void addRecipeCatalyst(ItemStack stack, IRecipeHandler handler) {
addRecipeCatalyst(Collections.singletonList(stack), handler);
}

Expand All @@ -310,8 +310,42 @@ public static void addRecipeCatalyst(ItemStack stack, Class<? extends IRecipeHan
* @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<ItemStack> 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<ItemStack> stacks, String handlerID) {
RecipeCatalysts.addRecipeCatalyst(stacks, handlerID);
}

@Deprecated
public static void addRecipeCatalyst(ItemStack stack, Class<? extends IRecipeHandler> handler) {
}

@Deprecated
public static void addRecipeCatalyst(List<ItemStack> stacks, Class<? extends IRecipeHandler> handler) {
RecipeCatalysts.addRecipeCatalyst(stacks, handler);
}

}
2 changes: 1 addition & 1 deletion src/main/java/codechicken/nei/recipe/GuiRecipe.java
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ private void refreshSlots() {
if (result != null)
slotcontainer.addSlot(result, p.x, p.y);

List<PositionedStack> catalysts = RecipeCatalysts.getRecipeCatalysts(handler.getClass());
List<PositionedStack> catalysts = RecipeCatalysts.getRecipeCatalysts(handler);
for (PositionedStack catalyst : catalysts) {
int xOffset = -GuiRecipeCatalyst.ingredientSize + 1;
int yOffset = BG_TOP_Y + GuiRecipeCatalyst.fullBorder;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/codechicken/nei/recipe/GuiRecipeCatalyst.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public GuiRecipeCatalyst(GuiRecipe guiRecipe) {

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();
Expand All @@ -49,7 +49,7 @@ public void draw() {
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();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/codechicken/nei/recipe/IRecipeHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ default String getHandlerId() {
* @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)
Expand Down
50 changes: 37 additions & 13 deletions src/main/java/codechicken/nei/recipe/RecipeCatalysts.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,49 @@
import java.util.Map;

public class RecipeCatalysts {
private static final Map<Class<? extends IRecipeHandler>, List<ItemStack>> recipeCatalystMap = new HashMap<>();
private static Map<Class<? extends IRecipeHandler>, List<PositionedStack>> positionedRecipeCatalystMap = new HashMap<>();
private static final Map<String, List<ItemStack>> recipeCatalystMap = new HashMap<>();
private static Map<String, List<PositionedStack>> positionedRecipeCatalystMap = new HashMap<>();
private static int heightCache;

public static void addRecipeCatalyst(List<ItemStack> stacks, Class<? extends IRecipeHandler> handler) {
if (recipeCatalystMap.containsKey(handler)) {
recipeCatalystMap.get(handler).addAll(stacks);
public static void addRecipeCatalyst(List<ItemStack> 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<Class<? extends IRecipeHandler>, List<PositionedStack>> getPositionedRecipeCatalystMap() {
public static Map<String, List<PositionedStack>> getPositionedRecipeCatalystMap() {
return positionedRecipeCatalystMap;
}

public static List<PositionedStack> getRecipeCatalysts(Class<? extends IRecipeHandler> handler) {
public static List<PositionedStack> getRecipeCatalysts(IRecipeHandler handler) {
return getRecipeCatalysts(handler.getOverlayIdentifier());
}

public static List<PositionedStack> 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<? extends IRecipeHandler> 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<Class<? extends IRecipeHandler>, List<PositionedStack>> newMap = new HashMap<>();
for (Map.Entry<Class<? extends IRecipeHandler>, List<ItemStack>> entry : recipeCatalystMap.entrySet()) {
Map<String, List<PositionedStack>> newMap = new HashMap<>();
for (Map.Entry<String, List<ItemStack>> entry : recipeCatalystMap.entrySet()) {
List<ItemStack> catalysts = entry.getValue();
List<PositionedStack> newStacks = new ArrayList<>();
int rowCount = getRowCount(availableHeight, catalysts.size());
Expand Down Expand Up @@ -75,4 +85,18 @@ public static int getRowCount(int availableHeight, int catalystsSize) {
return NEIServerUtils.divideCeil(catalystsSize, columnCount);
}

@Deprecated
public static void addRecipeCatalyst(List<ItemStack> stacks, Class<? extends IRecipeHandler> handler) {
}

@Deprecated
public static List<PositionedStack> getRecipeCatalysts(Class<? extends IRecipeHandler> handler) {
return Collections.emptyList();
}

@Deprecated
public static boolean containsCatalyst(Class<? extends IRecipeHandler> handler, ItemStack candidate) {
return false;
}

}
10 changes: 4 additions & 6 deletions src/main/java/codechicken/nei/recipe/RecipeInfo.java
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,10 @@ public static void load()
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());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,7 @@ public void loadUsageRecipes(ItemStack ingredient) {}
*
* @return The overlay identifier of this recipe type.
*/
@Override
public String getOverlayIdentifier() {
return null;
}
Expand Down Expand Up @@ -559,7 +560,7 @@ public IUsageHandler getUsageAndCatalystHandler(String inputId, Object... ingred
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);
Expand Down

0 comments on commit c73edaa

Please sign in to comment.