From fd99a42678f27d74483cfa2ae069de8bea38f893 Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 5 Jun 2019 20:14:36 +0800 Subject: recipe id cache --- .../me/shedaniel/rei/client/RecipeHelperImpl.java | 47 ++++++++++++++++++++-- 1 file changed, 44 insertions(+), 3 deletions(-) (limited to 'src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java') diff --git a/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java b/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java index 9c4b21ed5..5255f2d0d 100644 --- a/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java +++ b/src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java @@ -13,12 +13,12 @@ import net.minecraft.item.ItemStack; import net.minecraft.recipe.Recipe; import net.minecraft.recipe.RecipeManager; import net.minecraft.util.Identifier; -import org.apache.logging.log4j.Level; import java.awt.*; import java.util.List; import java.util.*; import java.util.concurrent.atomic.AtomicInteger; +import java.util.function.Function; import java.util.stream.Collectors; public class RecipeHelperImpl implements RecipeHelper { @@ -36,6 +36,7 @@ public class RecipeHelperImpl implements RecipeHelper { VISIBILITY_HANDLER_COMPARATOR = comparator.reversed(); } + public final List recipeFunctions = Lists.newArrayList(); private final AtomicInteger recipeCount = new AtomicInteger(); private final Map> recipeCategoryListMap = Maps.newHashMap(); private final Map categoryDisplaySettingsMap = Maps.newHashMap(); @@ -91,6 +92,13 @@ public class RecipeHelperImpl implements RecipeHelper { recipeCategoryListMap.get(categoryIdentifier).add(display); } + private void registerDisplay(Identifier categoryIdentifier, RecipeDisplay display, int index) { + if (!recipeCategoryListMap.containsKey(categoryIdentifier)) + return; + recipeCount.incrementAndGet(); + recipeCategoryListMap.get(categoryIdentifier).add(index, display); + } + @Override public Map> getRecipesFor(ItemStack stack) { Map> categoriesMap = new HashMap<>(); @@ -166,13 +174,17 @@ public class RecipeHelperImpl implements RecipeHelper { @Override public Optional getSpeedCraftButtonArea(RecipeCategory category) { if (!speedCraftAreaSupplierMap.containsKey(category.getIdentifier())) - return Optional.empty(); + return Optional.ofNullable(bounds -> new Rectangle((int) bounds.getMaxX() - 16, (int) bounds.getMaxY() - 16, 10, 10)); return Optional.ofNullable(speedCraftAreaSupplierMap.get(category.getIdentifier())); } @Override public void registerSpeedCraftButtonArea(Identifier category, ButtonAreaSupplier rectangle) { - speedCraftAreaSupplierMap.put(category, rectangle); + if (rectangle == null) { + if (speedCraftAreaSupplierMap.containsKey(category)) + speedCraftAreaSupplierMap.remove(category); + } else + speedCraftAreaSupplierMap.put(category, rectangle); } @Override @@ -203,6 +215,7 @@ public class RecipeHelperImpl implements RecipeHelper { this.speedCraftAreaSupplierMap.clear(); this.speedCraftFunctionalMap.clear(); this.categoryDisplaySettingsMap.clear(); + this.recipeFunctions.clear(); this.displayVisibilityHandlers.clear(); this.liveRecipeGenerators.clear(); ((DisplayHelperImpl) RoughlyEnoughItemsCore.getDisplayHelper()).resetCache(); @@ -235,6 +248,17 @@ public class RecipeHelperImpl implements RecipeHelper { RoughlyEnoughItemsCore.LOGGER.error("[REI] " + identifier.toString() + " plugin failed to load!", e); } }); + if (!recipeFunctions.isEmpty()) { + List allSortedRecipes = getAllSortedRecipes(); + Collections.reverse(allSortedRecipes); + recipeFunctions.forEach(recipeFunction -> { + try { + allSortedRecipes.stream().filter(recipe -> recipeFunction.recipeClass.isAssignableFrom(recipe.getClass())).forEach(t -> registerDisplay(recipeFunction.category, (RecipeDisplay) recipeFunction.mappingFunction.apply(t), 0)); + } catch (Exception e) { + RoughlyEnoughItemsCore.LOGGER.error("[REI] Failed to add recipes!", e); + } + }); + } if (getDisplayVisibilityHandlers().isEmpty()) registerRecipeVisibilityHandler(new DisplayVisibilityHandler() { @Override @@ -312,6 +336,11 @@ public class RecipeHelperImpl implements RecipeHelper { return true; } + @Override + public > void registerRecipes(Identifier category, Class recipeClass, Function mappingFunction) { + recipeFunctions.add(new RecipeFunction(category, recipeClass, mappingFunction)); + } + @Override public Optional getCachedCategorySettings(Identifier category) { return categoryDisplaySettingsMap.entrySet().stream().filter(entry -> entry.getKey().equals(category)).map(Map.Entry::getValue).findAny(); @@ -322,4 +351,16 @@ public class RecipeHelperImpl implements RecipeHelper { liveRecipeGenerators.add(liveRecipeGenerator); } + private class RecipeFunction { + Identifier category; + Class recipeClass; + Function mappingFunction; + + public RecipeFunction(Identifier category, Class recipeClass, Function mappingFunction) { + this.category = category; + this.recipeClass = recipeClass; + this.mappingFunction = mappingFunction; + } + } + } -- cgit