From 08d80d588a36598114087a79917e36e9d2cc97c3 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 24 Dec 2019 11:31:40 +0800 Subject: Finishing workstation usage and close #220 --- .../me/shedaniel/rei/impl/RecipeHelperImpl.java | 147 ++++++++++++--------- 1 file changed, 83 insertions(+), 64 deletions(-) (limited to 'src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java') diff --git a/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java b/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java index 21ea623a0..e4dd792b5 100644 --- a/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java +++ b/src/main/java/me/shedaniel/rei/impl/RecipeHelperImpl.java @@ -7,6 +7,7 @@ package me.shedaniel.rei.impl; import com.google.common.collect.Lists; import com.google.common.collect.Maps; +import com.google.common.collect.Sets; import me.shedaniel.math.api.Rectangle; import me.shedaniel.rei.RoughlyEnoughItemsCore; import me.shedaniel.rei.api.*; @@ -31,20 +32,21 @@ import java.util.stream.Collectors; @Deprecated @Internal public class RecipeHelperImpl implements RecipeHelper { - + private static final Comparator VISIBILITY_HANDLER_COMPARATOR; - @SuppressWarnings("rawtypes") private static final Comparator RECIPE_COMPARATOR = (o1, o2) -> { + @SuppressWarnings("rawtypes") + private static final Comparator RECIPE_COMPARATOR = (o1, o2) -> { int int_1 = o1.getId().getNamespace().compareTo(o2.getId().getNamespace()); if (int_1 == 0) int_1 = o1.getId().getPath().compareTo(o2.getId().getPath()); return int_1; }; - + static { Comparator comparator = Comparator.comparingDouble(DisplayVisibilityHandler::getPriority); VISIBILITY_HANDLER_COMPARATOR = comparator.reversed(); } - + private final List autoTransferHandlers = Lists.newArrayList(); private final List recipeFunctions = Lists.newArrayList(); private final List screenClickAreas = Lists.newArrayList(); @@ -57,7 +59,7 @@ public class RecipeHelperImpl implements RecipeHelper { private final List> liveRecipeGenerators = Lists.newArrayList(); private RecipeManager recipeManager; private boolean arePluginsLoading = false; - + @Override public List findCraftableEntriesByItems(List inventoryItems) { List craftables = new ArrayList<>(); @@ -70,16 +72,13 @@ public class RecipeHelperImpl implements RecipeHelper { slotsCraftable++; continue; } - boolean slotDone = false; + back: for (EntryStack possibleType : inventoryItems) { for (EntryStack slotPossible : slot) if (possibleType.equals(slotPossible)) { slotsCraftable++; - slotDone = true; - break; + break back; } - if (slotDone) - break; } } if (slotsCraftable == recipeDisplay.getRequiredEntries().size()) @@ -87,34 +86,34 @@ public class RecipeHelperImpl implements RecipeHelper { } return craftables.stream().distinct().collect(Collectors.toList()); } - + @Override public boolean arePluginsLoading() { return arePluginsLoading; } - + @Override public void registerCategory(RecipeCategory category) { categories.add(category); recipeCategoryListMap.put(category.getIdentifier(), Lists.newLinkedList()); categoryWorkingStations.put(category.getIdentifier(), Lists.newLinkedList()); } - + @Override public void registerWorkingStations(Identifier category, List... workingStations) { categoryWorkingStations.get(category).addAll(Arrays.asList(workingStations)); } - + @Override public void registerWorkingStations(Identifier category, EntryStack... workingStations) { categoryWorkingStations.get(category).addAll(Arrays.asList(workingStations).stream().map(Collections::singletonList).collect(Collectors.toList())); } - + @Override public List> getWorkingStations(Identifier category) { return categoryWorkingStations.get(category); } - + @Override public void registerDisplay(Identifier categoryIdentifier, RecipeDisplay display) { if (!recipeCategoryListMap.containsKey(categoryIdentifier)) @@ -122,14 +121,14 @@ public class RecipeHelperImpl implements RecipeHelper { recipeCount.incrementAndGet(); 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, List> getRecipesFor(EntryStack stack) { Map> categoriesMap = new HashMap<>(); @@ -154,38 +153,58 @@ public class RecipeHelperImpl implements RecipeHelper { recipeCategoryListMap.remove(category); return recipeCategoryListMap; } - + @Override public RecipeCategory getCategory(Identifier identifier) { return CollectionUtils.findFirstOrNull(categories, category -> category.getIdentifier().equals(identifier)); } - + @Override public RecipeManager getRecipeManager() { return recipeManager; } - + + private boolean isStackWorkStationOfCategory(Identifier category, EntryStack stack) { + for (List stacks : getWorkingStations(category)) { + for (EntryStack entryStack : stacks) { + if (entryStack.equalsIgnoreTagsAndAmount(stack)) + return true; + } + } + return false; + } + @Override public Map, List> getUsagesFor(EntryStack stack) { - Map> categoriesMap = new HashMap<>(); - categories.forEach(f -> categoriesMap.put(f.getIdentifier(), Lists.newArrayList())); + Map> categoriesMap = new HashMap<>(); + categories.forEach(f -> categoriesMap.put(f.getIdentifier(), Sets.newLinkedHashSet())); for (Map.Entry> entry : recipeCategoryListMap.entrySet()) { - boolean isWorkstationCategory = getWorkingStations(entry.getKey()).stream().anyMatch(ws -> ws.contains(stack)); - for (RecipeDisplay recipeDisplay : entry.getValue()) { - if (isWorkstationCategory) { + boolean isWorkstationCategory = isStackWorkStationOfCategory(entry.getKey(), stack); + if (isWorkstationCategory) { + for (RecipeDisplay recipeDisplay : entry.getValue()) { + back: + for (List input : recipeDisplay.getInputEntries()) { + for (EntryStack otherEntry : input) { + if (otherEntry.equals(stack)) { + categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay); + break back; + } + } + } + } + for (RecipeDisplay recipeDisplay : entry.getValue()) { categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay); - } else { - boolean found = false; + } + } else { + for (RecipeDisplay recipeDisplay : entry.getValue()) { + back: for (List input : recipeDisplay.getInputEntries()) { for (EntryStack otherEntry : input) { if (otherEntry.equals(stack)) { categoriesMap.get(recipeDisplay.getRecipeCategory()).add(recipeDisplay); - found = true; - break; + break back; } } - if (found) - break; } } } @@ -203,19 +222,19 @@ public class RecipeHelperImpl implements RecipeHelper { recipeCategoryListMap.remove(category); return recipeCategoryListMap; } - + @Override public List> getAllCategories() { return Collections.unmodifiableList(categories); } - + @Override public Optional getAutoCraftButtonArea(RecipeCategory category) { if (!autoCraftAreaSupplierMap.containsKey(category.getIdentifier())) return Optional.ofNullable(bounds -> new Rectangle(bounds.getMaxX() - 16, bounds.getMaxY() - 16, 10, 10)); return Optional.ofNullable(autoCraftAreaSupplierMap.get(category.getIdentifier())); } - + @Override public void registerAutoCraftButtonArea(Identifier category, ButtonAreaSupplier rectangle) { if (rectangle == null) { @@ -224,7 +243,7 @@ public class RecipeHelperImpl implements RecipeHelper { } else autoCraftAreaSupplierMap.put(category, rectangle); } - + @SuppressWarnings("deprecation") public void recipesLoaded(RecipeManager recipeManager) { long startTime = System.currentTimeMillis(); @@ -308,7 +327,7 @@ public class RecipeHelperImpl implements RecipeHelper { public ActionResult handleDisplay(RecipeCategory category, RecipeDisplay display) { return ActionResult.SUCCESS; } - + @Override public float getPriority() { return -1f; @@ -317,34 +336,34 @@ public class RecipeHelperImpl implements RecipeHelper { // Clear Cache ((DisplayHelperImpl) DisplayHelper.getInstance()).resetCache(); ScreenHelper.getOptionalOverlay().ifPresent(overlay -> overlay.shouldReInit = true); - + long usedTime = System.currentTimeMillis() - startTime; RoughlyEnoughItemsCore.LOGGER.info("[REI] Registered %d stack entries, %d recipes displays, %d exclusion zones suppliers, %d bounds handler, %d visibility handlers and %d categories (%s) in %d ms.", EntryRegistry.getInstance().getStacksList().size(), recipeCount.get(), DisplayHelper.getInstance().getBaseBoundsHandler().supplierSize(), DisplayHelper.getInstance().getAllBoundsHandlers().size(), getDisplayVisibilityHandlers().size(), categories.size(), String.join(", ", categories.stream().map(RecipeCategory::getCategoryName).collect(Collectors.toList())), usedTime); arePluginsLoading = false; } - + @Override public AutoTransferHandler registerAutoCraftingHandler(AutoTransferHandler handler) { autoTransferHandlers.add(handler); return handler; } - + @Override public List getSortedAutoCraftingHandler() { return autoTransferHandlers.stream().sorted(Comparator.comparingDouble(AutoTransferHandler::getPriority).reversed()).collect(Collectors.toList()); } - + @Override public int getRecipeCount() { return recipeCount.get(); } - + @Override @SuppressWarnings("rawtypes") public List getAllSortedRecipes() { return getRecipeManager().values().stream().sorted(RECIPE_COMPARATOR).collect(Collectors.toList()); } - + @Override public Map, List> getAllRecipes() { Map, List> map = Maps.newLinkedHashMap(); @@ -357,33 +376,33 @@ public class RecipeHelperImpl implements RecipeHelper { } return map; } - + @Override public List getAllRecipesFromCategory(RecipeCategory category) { return recipeCategoryListMap.get(category.getIdentifier()); } - + @Override public void registerRecipeVisibilityHandler(DisplayVisibilityHandler visibilityHandler) { displayVisibilityHandlers.add(visibilityHandler); } - + @Override public void unregisterRecipeVisibilityHandler(DisplayVisibilityHandler visibilityHandler) { displayVisibilityHandlers.remove(visibilityHandler); } - + @Override public List getDisplayVisibilityHandlers() { return Collections.unmodifiableList(displayVisibilityHandlers); } - + @SuppressWarnings("deprecation") @Override public boolean isDisplayVisible(RecipeDisplay display, boolean respectConfig) { return isDisplayVisible(display); } - + @SuppressWarnings("deprecation") @Override public boolean isDisplayVisible(RecipeDisplay display) { @@ -400,74 +419,74 @@ public class RecipeHelperImpl implements RecipeHelper { } return true; } - + @Override public void registerScreenClickArea(Rectangle rectangle, Class> screenClass, Identifier... categories) { this.screenClickAreas.add(new ScreenClickAreaImpl(screenClass, rectangle, categories)); } - + @Override public > void registerRecipes(Identifier category, Class recipeClass, Function mappingFunction) { recipeFunctions.add(new RecipeFunction(category, recipe -> recipeClass.isAssignableFrom(recipe.getClass()), mappingFunction)); } - + @Override public > void registerRecipes(Identifier category, @SuppressWarnings("rawtypes") Function recipeFilter, Function mappingFunction) { recipeFunctions.add(new RecipeFunction(category, recipeFilter::apply, mappingFunction)); } - + @Override public > void registerRecipes(Identifier category, - @SuppressWarnings("rawtypes") Predicate recipeFilter, Function mappingFunction) { + @SuppressWarnings("rawtypes") Predicate recipeFilter, Function mappingFunction) { recipeFunctions.add(new RecipeFunction(category, recipeFilter, mappingFunction)); } - + @Override public void registerLiveRecipeGenerator(LiveRecipeGenerator liveRecipeGenerator) { liveRecipeGenerators.add((LiveRecipeGenerator) liveRecipeGenerator); } - + @Override public List getScreenClickAreas() { return screenClickAreas; } - + private class ScreenClickAreaImpl implements ScreenClickArea { Class> screenClass; Rectangle rectangle; Identifier[] categories; - + private ScreenClickAreaImpl(Class> screenClass, Rectangle rectangle, Identifier[] categories) { this.screenClass = screenClass; this.rectangle = rectangle; this.categories = categories; } - + public Class> getScreenClass() { return screenClass; } - + public Rectangle getRectangle() { return rectangle; } - + public Identifier[] getCategories() { return categories; } } - + @SuppressWarnings("rawtypes") private class RecipeFunction { Identifier category; Predicate recipeFilter; Function mappingFunction; - + public RecipeFunction(Identifier category, Predicate recipeFilter, Function mappingFunction) { this.category = category; this.recipeFilter = recipeFilter; this.mappingFunction = mappingFunction; } } - + } -- cgit