diff options
| author | Unknown <shekwancheung0528@gmail.com> | 2019-06-05 20:14:36 +0800 |
|---|---|---|
| committer | Unknown <shekwancheung0528@gmail.com> | 2019-06-05 20:14:36 +0800 |
| commit | fd99a42678f27d74483cfa2ae069de8bea38f893 (patch) | |
| tree | 7f76246c3d861c41ac3d93c5781e46e477d415eb /src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java | |
| parent | a1480e28813b4fc9f6d4f8ce252dd5a355c00855 (diff) | |
| download | RoughlyEnoughItems-fd99a42678f27d74483cfa2ae069de8bea38f893.tar.gz RoughlyEnoughItems-fd99a42678f27d74483cfa2ae069de8bea38f893.tar.bz2 RoughlyEnoughItems-fd99a42678f27d74483cfa2ae069de8bea38f893.zip | |
recipe id cache
Diffstat (limited to 'src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java')
| -rw-r--r-- | src/main/java/me/shedaniel/rei/client/RecipeHelperImpl.java | 47 |
1 files changed, 44 insertions, 3 deletions
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<RecipeFunction> recipeFunctions = Lists.newArrayList(); private final AtomicInteger recipeCount = new AtomicInteger(); private final Map<Identifier, List<RecipeDisplay>> recipeCategoryListMap = Maps.newHashMap(); private final Map<Identifier, DisplaySettings> 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<RecipeCategory, List<RecipeDisplay>> getRecipesFor(ItemStack stack) { Map<Identifier, List<RecipeDisplay>> categoriesMap = new HashMap<>(); @@ -166,13 +174,17 @@ public class RecipeHelperImpl implements RecipeHelper { @Override public Optional<ButtonAreaSupplier> 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<Recipe> 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 @@ -313,6 +337,11 @@ public class RecipeHelperImpl implements RecipeHelper { } @Override + public <T extends Recipe<?>> void registerRecipes(Identifier category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction) { + recipeFunctions.add(new RecipeFunction(category, recipeClass, mappingFunction)); + } + + @Override public Optional<DisplaySettings> 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<?, RecipeDisplay> mappingFunction) { + this.category = category; + this.recipeClass = recipeClass; + this.mappingFunction = mappingFunction; + } + } + } |
