From 973868ece23dd75080cfe6ef7b48f8284070ce4e Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 30 Jan 2019 00:12:47 +0800 Subject: Fixes Bugs Close #13 Close #15 Close #18 --- .../java/me/shedaniel/rei/client/RecipeHelper.java | 63 ++++++++++++---------- 1 file changed, 34 insertions(+), 29 deletions(-) (limited to 'src/main/java/me/shedaniel/rei/client/RecipeHelper.java') diff --git a/src/main/java/me/shedaniel/rei/client/RecipeHelper.java b/src/main/java/me/shedaniel/rei/client/RecipeHelper.java index 378eb2f59..0224933fa 100644 --- a/src/main/java/me/shedaniel/rei/client/RecipeHelper.java +++ b/src/main/java/me/shedaniel/rei/client/RecipeHelper.java @@ -15,20 +15,17 @@ import java.util.stream.Collectors; public class RecipeHelper { - private static Map> recipeCategoryListMap; - private static List categories; - private static RecipeManager recipeManager; - private static Map speedCraftAreaSupplierMap; - private static Map> speedCraftFunctionalMap; - - public RecipeHelper() { - this.recipeCategoryListMap = Maps.newHashMap(); - this.categories = Lists.newArrayList(); - this.speedCraftAreaSupplierMap = Maps.newHashMap(); - this.speedCraftFunctionalMap = Maps.newHashMap(); + private final Map> recipeCategoryListMap = Maps.newHashMap(); + private final List categories = Lists.newArrayList(); + private final Map speedCraftAreaSupplierMap = Maps.newHashMap(); + private final Map> speedCraftFunctionalMap = Maps.newHashMap(); + private RecipeManager recipeManager; + + public static RecipeHelper getInstance() { + return RoughlyEnoughItemsCore.getRecipeHelper(); } - public static List findCraftableByItems(List inventoryItems) { + public List findCraftableByItems(List inventoryItems) { List craftables = new ArrayList<>(); for(List value : recipeCategoryListMap.values()) for(IRecipeDisplay recipeDisplay : value) { @@ -57,25 +54,27 @@ public class RecipeHelper { return craftables.stream().distinct().collect(Collectors.toList()); } - public static void registerCategory(IRecipeCategory category) { + public void registerCategory(IRecipeCategory category) { categories.add(category); recipeCategoryListMap.put(category.getIdentifier(), Lists.newArrayList()); } - public static void registerRecipe(Identifier categoryIdentifier, IRecipeDisplay display) { + public void registerRecipe(Identifier categoryIdentifier, IRecipeDisplay display) { if (!recipeCategoryListMap.containsKey(categoryIdentifier)) return; recipeCategoryListMap.get(categoryIdentifier).add(display); } - public static Map> getRecipesFor(ItemStack stack) { + public Map> getRecipesFor(ItemStack stack) { Map> categoriesMap = new HashMap<>(); categories.forEach(f -> categoriesMap.put(f.getIdentifier(), new LinkedList<>())); - for(List value : recipeCategoryListMap.values()) - for(IRecipeDisplay recipeDisplay : value) + for(Map.Entry> entry : recipeCategoryListMap.entrySet()) { + IRecipeCategory category = getCategory(entry.getKey()); + for(IRecipeDisplay recipeDisplay : entry.getValue()) for(ItemStack outputStack : (List) recipeDisplay.getOutput()) - if (ItemStack.areEqualIgnoreTags(stack, outputStack)) + if (category.checkTags() ? ItemStack.areEqual(stack, outputStack) : ItemStack.areEqualIgnoreTags(stack, outputStack)) categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay); + } categoriesMap.keySet().removeIf(f -> categoriesMap.get(f).isEmpty()); Map> recipeCategoryListMap = Maps.newHashMap(); categories.forEach(category -> { @@ -85,19 +84,24 @@ public class RecipeHelper { return recipeCategoryListMap; } - public static RecipeManager getRecipeManager() { + private IRecipeCategory getCategory(Identifier identifier) { + return categories.stream().filter(category -> category.getIdentifier().equals(identifier)).findFirst().orElse(null); + } + + public RecipeManager getRecipeManager() { return recipeManager; } - public static Map> getUsagesFor(ItemStack stack) { + public Map> getUsagesFor(ItemStack stack) { Map> categoriesMap = new HashMap<>(); categories.forEach(f -> categoriesMap.put(f.getIdentifier(), new LinkedList<>())); - for(List value : recipeCategoryListMap.values()) - for(IRecipeDisplay recipeDisplay : value) { + for(Map.Entry> entry : recipeCategoryListMap.entrySet()) { + IRecipeCategory category = getCategory(entry.getKey()); + for(IRecipeDisplay recipeDisplay : entry.getValue()) { boolean found = false; for(List input : (List>) recipeDisplay.getInput()) { for(ItemStack itemStack : input) { - if (ItemStack.areEqualIgnoreTags(itemStack, stack)) { + if (category.checkTags() ? ItemStack.areEqual(itemStack, stack) : ItemStack.areEqualIgnoreTags(itemStack, stack)) { categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay); found = true; break; @@ -107,6 +111,7 @@ public class RecipeHelper { break; } } + } categoriesMap.keySet().removeIf(f -> categoriesMap.get(f).isEmpty()); Map> recipeCategoryListMap = Maps.newHashMap(); categories.forEach(category -> { @@ -116,11 +121,11 @@ public class RecipeHelper { return recipeCategoryListMap; } - public static List getCategories() { - return categories; + public List getCategories() { + return new LinkedList<>(categories); } - public static SpeedCraftAreaSupplier getSpeedCraftButtonArea(IRecipeCategory category) { + public SpeedCraftAreaSupplier getSpeedCraftButtonArea(IRecipeCategory category) { if (!speedCraftAreaSupplierMap.containsKey(category.getIdentifier())) return bounds -> { return new Rectangle((int) bounds.getMaxX() - 16, (int) bounds.getMaxY() - 16, 10, 10); @@ -128,17 +133,17 @@ public class RecipeHelper { return speedCraftAreaSupplierMap.get(category.getIdentifier()); } - public static void registerSpeedCraftButtonArea(Identifier category, SpeedCraftAreaSupplier rectangle) { + public void registerSpeedCraftButtonArea(Identifier category, SpeedCraftAreaSupplier rectangle) { speedCraftAreaSupplierMap.put(category, rectangle); } - public static List getSpeedCraftFunctional(IRecipeCategory category) { + public List getSpeedCraftFunctional(IRecipeCategory category) { if (speedCraftFunctionalMap.get(category.getIdentifier()) == null) return Lists.newArrayList(); return speedCraftFunctionalMap.get(category.getIdentifier()); } - public static void registerSpeedCraftFunctional(Identifier category, SpeedCraftFunctional functional) { + public void registerSpeedCraftFunctional(Identifier category, SpeedCraftFunctional functional) { List list = speedCraftFunctionalMap.containsKey(category) ? new LinkedList<>(speedCraftFunctionalMap.get(category)) : Lists.newLinkedList(); list.add(functional); speedCraftFunctionalMap.put(category, list); -- cgit