From 1b33b3e72c1b2bdb5b733bb5afe6b478367c77e5 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 2 Nov 2021 11:09:14 +0800 Subject: Fix searching craft but does search the modid because other mods add that --- .../shedaniel/rei/impl/common/entry/AbstractEntryStack.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java index 04f364c75..3e304f41e 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/AbstractEntryStack.java @@ -36,6 +36,7 @@ import me.shedaniel.rei.api.client.gui.Renderer; import me.shedaniel.rei.api.client.gui.widgets.Tooltip; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.EntryStacks; +import me.shedaniel.rei.api.common.util.FormattingUtils; import me.shedaniel.rei.impl.client.util.CrashReportUtils; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; @@ -52,6 +53,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; import java.util.Collection; +import java.util.Iterator; @ApiStatus.Internal public abstract class AbstractEntryStack implements EntryStack, Renderer { @@ -184,11 +186,20 @@ public abstract class AbstractEntryStack implements EntryStack, Renderer { tooltip.getValue().addAllTexts(get(Settings.TOOLTIP_APPEND_EXTRA).apply(this)); tooltip.setValue(get(Settings.TOOLTIP_PROCESSOR).apply(this, tooltip.getValue())); if (tooltip.getValue() == null) return null; + ResourceLocation location = getIdentifier(); if (appendModName) { - ResourceLocation location = getIdentifier(); if (location != null) { ClientHelper.getInstance().appendModIdToTooltips(tooltip.getValue(), location.getNamespace()); } + } else { + final String modName = ClientHelper.getInstance().getModFromModId(location.getNamespace()); + Iterator iterator = tooltip.getValue().entries().iterator(); + while (iterator.hasNext()) { + Tooltip.Entry s = iterator.next(); + if (s.isText() && FormattingUtils.stripFormatting(s.getAsText().getString()).equalsIgnoreCase(modName)) { + iterator.remove(); + } + } } return tooltip.getValue(); } catch (Throwable throwable) { -- cgit From b817b2ad84966370a4adf48a192827d8de100b46 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 2 Nov 2021 12:04:25 +0800 Subject: Add option to merge displays --- .../rei/impl/client/ClientHelperImpl.java | 15 +++-- .../rei/impl/client/config/ConfigObjectImpl.java | 6 ++ .../gui/screen/AbstractDisplayViewingScreen.java | 5 +- .../gui/screen/CompositeDisplayViewingScreen.java | 17 ++--- .../gui/screen/DefaultDisplayViewingScreen.java | 21 +++--- .../impl/client/gui/widget/InternalWidgets.java | 17 ++++- .../shedaniel/rei/impl/client/view/ViewsImpl.java | 77 +++++++++++++++++++++- 7 files changed, 126 insertions(+), 32 deletions(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java index 0a1e598ca..ee054dcd8 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/ClientHelperImpl.java @@ -50,6 +50,7 @@ import me.shedaniel.rei.impl.client.gui.screen.CompositeDisplayViewingScreen; import me.shedaniel.rei.impl.client.gui.screen.DefaultDisplayViewingScreen; import me.shedaniel.rei.impl.client.gui.screen.UncertainDisplayViewingScreen; import me.shedaniel.rei.impl.client.view.ViewsImpl; +import me.shedaniel.rei.impl.display.DisplaySpec; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; @@ -245,7 +246,7 @@ public class ClientHelperImpl implements ClientHelper { } @ApiStatus.Internal - public void openRecipeViewingScreen(Map, List> map, @Nullable CategoryIdentifier category, List> ingredientNotice, List> resultNotice) { + public void openRecipeViewingScreen(Map, List> map, @Nullable CategoryIdentifier category, List> ingredientNotice, List> resultNotice) { LegacyWrapperViewSearchBuilder builder = new LegacyWrapperViewSearchBuilder(map); for (EntryStack stack : ingredientNotice) { builder.addInputNotice(stack); @@ -258,7 +259,7 @@ public class ClientHelperImpl implements ClientHelper { @Override public boolean openView(ViewSearchBuilder builder) { - Map, List> map = builder.buildMap(); + Map, List> map = builder.buildMapInternal(); if (map.isEmpty()) return false; Screen screen; if (ConfigObject.getInstance().getRecipeScreenType() == DisplayScreenType.COMPOSITE) { @@ -315,7 +316,7 @@ public class ClientHelperImpl implements ClientHelper { private final List> usagesFor = new ArrayList<>(); @Nullable private CategoryIdentifier preferredOpenedCategory = null; - private final Supplier, List>> map = Suppliers.memoize(() -> ViewsImpl.buildMapFor(this)); + private final Supplier, List>> map = Suppliers.memoize(() -> ViewsImpl.buildMapFor(this)); @Override public ViewSearchBuilder addCategory(CategoryIdentifier category) { @@ -369,14 +370,14 @@ public class ClientHelperImpl implements ClientHelper { } @Override - public Map, List> buildMap() { + public Map, List> buildMapInternal() { fillPreferredOpenedCategory(); return this.map.get(); } } public static final class LegacyWrapperViewSearchBuilder extends AbstractViewSearchBuilder { - private final Map, List> map; + private final Map, List> map; @Nullable private EntryStack inputNotice; @Nullable @@ -384,7 +385,7 @@ public class ClientHelperImpl implements ClientHelper { @Nullable private CategoryIdentifier preferredOpenedCategory = null; - public LegacyWrapperViewSearchBuilder(Map, List> map) { + public LegacyWrapperViewSearchBuilder(Map, List> map) { this.map = map; } @@ -446,7 +447,7 @@ public class ClientHelperImpl implements ClientHelper { } @Override - public Map, List> buildMap() { + public Map, List> buildMapInternal() { fillPreferredOpenedCategory(); return this.map; } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java index fba1fd4c2..3895bc4fc 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/config/ConfigObjectImpl.java @@ -221,6 +221,11 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { return advanced.layout.debugRenderTimeRequired; } + @Override + public boolean doMergeDisplayUnderOne() { + return advanced.layout.mergeDisplayUnderOne; + } + @Override public ModifierKeyCode getFavoriteKeyCode() { return basics.keyBindings.favoriteKeybind == null ? ModifierKeyCode.unknown() : basics.keyBindings.favoriteKeybind; @@ -495,6 +500,7 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { @Comment("Declares the maximum amount of recipes displayed in a page if possible.") @ConfigEntry.BoundedDiscrete(min = 2, max = 99) private int maxRecipesPerPage = 3; @Comment("Declares whether entry rendering time should be debugged.") private boolean debugRenderTimeRequired = false; + @Comment("Merges displays with equal contents under 1 display.") private boolean mergeDisplayUnderOne = true; } public static class Accessibility { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java index c9015efa3..96d290a9a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/AbstractDisplayViewingScreen.java @@ -39,6 +39,7 @@ import me.shedaniel.rei.api.common.entry.type.VanillaEntryTypes; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.impl.client.ClientHelperImpl; import me.shedaniel.rei.impl.client.gui.widget.EntryWidget; +import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; @@ -57,7 +58,7 @@ import java.util.List; import java.util.Map; public abstract class AbstractDisplayViewingScreen extends Screen implements DisplayScreen { - protected final Map, List> categoryMap; + protected final Map, List> categoryMap; protected final List> categories; protected List> ingredientStackToNotice = new ArrayList<>(); protected List> resultStackToNotice = new ArrayList<>(); @@ -65,7 +66,7 @@ public abstract class AbstractDisplayViewingScreen extends Screen implements Dis protected int tabsPerPage; protected Rectangle bounds; - protected AbstractDisplayViewingScreen(Map, List> categoryMap, @Nullable CategoryIdentifier category, int tabsPerPage) { + protected AbstractDisplayViewingScreen(Map, List> categoryMap, @Nullable CategoryIdentifier category, int tabsPerPage) { super(NarratorChatListener.NO_TITLE); this.categoryMap = categoryMap; this.categories = Lists.newArrayList(categoryMap.keySet()); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java index 115d44f3d..c207a44ab 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/CompositeDisplayViewingScreen.java @@ -50,6 +50,7 @@ import me.shedaniel.rei.impl.client.REIRuntimeImpl; import me.shedaniel.rei.impl.client.gui.widget.EntryWidget; import me.shedaniel.rei.impl.client.gui.widget.InternalWidgets; import me.shedaniel.rei.impl.client.gui.widget.TabWidget; +import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; @@ -95,7 +96,7 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen private long scrollBarAlphaFutureTime = -1; private int tabsPage = -1; - public CompositeDisplayViewingScreen(Map, List> categoryMap, @Nullable CategoryIdentifier category) { + public CompositeDisplayViewingScreen(Map, List> categoryMap, @Nullable CategoryIdentifier category) { super(categoryMap, category, 8); } @@ -118,8 +119,8 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen int largestWidth = width - 100; int largestHeight = height - 40; DisplayCategory category = (DisplayCategory) categories.get(selectedCategoryIndex); - Display display = categoryMap.get(category).get(selectedRecipeIndex); - int guiWidth = Mth.clamp(category.getDisplayWidth(display) + 30, 0, largestWidth) + 100; + DisplaySpec display = categoryMap.get(category).get(selectedRecipeIndex); + int guiWidth = Mth.clamp(category.getDisplayWidth(display.provideInternalDisplay()) + 30, 0, largestWidth) + 100; int guiHeight = Mth.clamp(category.getDisplayHeight() + 40, 166, largestHeight); this.tabsPerPage = Math.max(5, Mth.floor((guiWidth - 20d) / tabSize)); if (this.tabsPage == -1) { @@ -153,10 +154,10 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen this.scrollListBounds = new Rectangle(bounds.x + 4, bounds.y + 17, 97 + 5, guiHeight - 17 - 7); this.widgets.add(Widgets.createSlotBase(scrollListBounds)); - Rectangle recipeBounds = new Rectangle(bounds.x + 100 + (guiWidth - 100) / 2 - category.getDisplayWidth(display) / 2, bounds.y + bounds.height / 2 - category.getDisplayHeight() / 2, category.getDisplayWidth(display), category.getDisplayHeight()); + Rectangle recipeBounds = new Rectangle(bounds.x + 100 + (guiWidth - 100) / 2 - category.getDisplayWidth(display.provideInternalDisplay()) / 2, bounds.y + bounds.height / 2 - category.getDisplayHeight() / 2, category.getDisplayWidth(display.provideInternalDisplay()), category.getDisplayHeight()); List setupDisplay; try { - setupDisplay = category.setupDisplay(display, recipeBounds); + setupDisplay = category.setupDisplay(display.provideInternalDisplay(), recipeBounds); } catch (Throwable throwable) { throwable.printStackTrace(); setupDisplay = new ArrayList<>(); @@ -173,13 +174,13 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen this.widgets.addAll(setupDisplay); Optional supplier = CategoryRegistry.getInstance().get(category.getCategoryIdentifier()).getPlusButtonArea(); if (supplier.isPresent() && supplier.get().get(recipeBounds) != null) - this.widgets.add(InternalWidgets.createAutoCraftingButtonWidget(recipeBounds, supplier.get().get(recipeBounds), new TextComponent(supplier.get().getButtonText()), () -> display, setupDisplay, category)); + this.widgets.add(InternalWidgets.createAutoCraftingButtonWidget(recipeBounds, supplier.get().get(recipeBounds), new TextComponent(supplier.get().getButtonText()), display::provideInternalDisplay, display::provideInternalDisplayIds, setupDisplay, category)); int index = 0; - for (Display recipeDisplay : categoryMap.get(category)) { + for (DisplaySpec recipeDisplay : categoryMap.get(category)) { int finalIndex = index; DisplayRenderer displayRenderer; - displayRenderers.add(displayRenderer = category.getDisplayRenderer(recipeDisplay)); + displayRenderers.add(displayRenderer = category.getDisplayRenderer(recipeDisplay.provideInternalDisplay())); buttonList.add(Widgets.createButton(new Rectangle(bounds.x + 5, 0, displayRenderer.getWidth(), displayRenderer.getHeight()), NarratorChatListener.NO_TITLE) .onClick(button -> { selectedRecipeIndex = finalIndex; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java index 5e341d3b9..3478a2cc3 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/screen/DefaultDisplayViewingScreen.java @@ -56,6 +56,7 @@ import me.shedaniel.rei.impl.client.gui.widget.EntryWidget; import me.shedaniel.rei.impl.client.gui.widget.InternalWidgets; import me.shedaniel.rei.impl.client.gui.widget.TabWidget; import me.shedaniel.rei.impl.client.gui.widget.basewidgets.PanelWidget; +import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.client.Minecraft; import net.minecraft.client.gui.chat.NarratorChatListener; import net.minecraft.client.gui.components.events.GuiEventListener; @@ -89,7 +90,7 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { private Panel workingStationsBaseWidget; private Button recipeBack, recipeNext, categoryBack, categoryNext; - public DefaultDisplayViewingScreen(Map, List> categoriesMap, @Nullable CategoryIdentifier category) { + public DefaultDisplayViewingScreen(Map, List> categoriesMap, @Nullable CategoryIdentifier category) { super(categoriesMap, category, 5); this.bounds = new Rectangle(0, 0, 176, 150); } @@ -155,7 +156,7 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { this.preWidgets.clear(); this.widgets.clear(); int largestHeight = Math.max(height - 34 - 30, 100); - int maxWidthDisplay = CollectionUtils.mapAndMax(getCurrentDisplayed(), getCurrentCategory()::getDisplayWidth, Comparator.naturalOrder()).orElse(150); + int maxWidthDisplay = CollectionUtils.mapAndMax(getCurrentDisplayed(), display -> getCurrentCategory().getDisplayWidth(display.provideInternalDisplay()), Comparator.naturalOrder()).orElse(150); int maxHeight = Math.min(largestHeight, CollectionUtils., Integer>mapAndMax(categories, category -> (category.getDisplayHeight() + 4) * Math.max(1, Math.min(getRecipesPerPage(largestHeight, category) + 1, Math.max(categoryMap.get(category).size(), ConfigObject.getInstance().getMaxRecipePerPage()))) + 36, Comparator.naturalOrder()).orElse(66)); int totalDisplayHeight = (getCurrentCategory().getDisplayHeight() + 4) * Math.max(1, getRecipesPerPage(maxHeight, getCurrentCategory()) + 1) + 36; @@ -250,15 +251,15 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { private void initDisplays() { Optional plusButtonArea = CategoryRegistry.getInstance().get(getCurrentCategoryId()).getPlusButtonArea(); int displayHeight = getCurrentCategory().getDisplayHeight(); - List currentDisplayed = getCurrentDisplayed(); + List currentDisplayed = getCurrentDisplayed(); for (int i = 0; i < currentDisplayed.size(); i++) { - final Display display = currentDisplayed.get(i); - final Supplier displaySupplier = () -> display; + final DisplaySpec display = currentDisplayed.get(i); + final Supplier displaySupplier = display::provideInternalDisplay; int displayWidth = getCurrentCategory().getDisplayWidth(displaySupplier.get()); final Rectangle displayBounds = new Rectangle(getBounds().getCenterX() - displayWidth / 2, getBounds().getCenterY() + 16 - displayHeight * (getRecipesPerPage() + 1) / 2 - 2 * (getRecipesPerPage() + 1) + displayHeight * i + 4 * i, displayWidth, displayHeight); List setupDisplay; try { - setupDisplay = getCurrentCategory().setupDisplay(display, displayBounds); + setupDisplay = getCurrentCategory().setupDisplay(display.provideInternalDisplay(), displayBounds); } catch (Throwable throwable) { throwable.printStackTrace(); setupDisplay = new ArrayList<>(); @@ -275,7 +276,7 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { this.recipeBounds.put(displayBounds, setupDisplay); this.widgets.addAll(setupDisplay); if (plusButtonArea.isPresent() && plusButtonArea.get().get(displayBounds) != null) { - this.widgets.add(InternalWidgets.createAutoCraftingButtonWidget(displayBounds, plusButtonArea.get().get(displayBounds), new TextComponent(plusButtonArea.get().getButtonText()), displaySupplier, setupDisplay, getCurrentCategory())); + this.widgets.add(InternalWidgets.createAutoCraftingButtonWidget(displayBounds, plusButtonArea.get().get(displayBounds), new TextComponent(plusButtonArea.get().getButtonText()), displaySupplier, display::provideInternalDisplayIds, setupDisplay, getCurrentCategory())); } } } @@ -310,10 +311,10 @@ public class DefaultDisplayViewingScreen extends AbstractDisplayViewingScreen { return widgets; } - public List getCurrentDisplayed() { - List list = Lists.newArrayList(); + public List getCurrentDisplayed() { + List list = Lists.newArrayList(); int recipesPerPage = getRecipesPerPage(); - List displays = categoryMap.get(getCurrentCategory()); + List displays = categoryMap.get(getCurrentCategory()); for (int i = 0; i <= recipesPerPage; i++) { if (page * (recipesPerPage + 1) + i < displays.size()) { list.add(displays.get(page * (recipesPerPage + 1) + i)); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java index 1529a9e4b..27e9e79a5 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/InternalWidgets.java @@ -59,6 +59,7 @@ import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; import java.util.ArrayList; +import java.util.Collection; import java.util.List; import java.util.Objects; import java.util.function.Supplier; @@ -68,7 +69,7 @@ import java.util.function.Supplier; public final class InternalWidgets { private InternalWidgets() {} - public static Widget createAutoCraftingButtonWidget(Rectangle displayBounds, Rectangle rectangle, Component text, Supplier displaySupplier, List setupDisplay, DisplayCategory category) { + public static Widget createAutoCraftingButtonWidget(Rectangle displayBounds, Rectangle rectangle, Component text, Supplier displaySupplier, Supplier> idsSupplier, List setupDisplay, DisplayCategory category) { AbstractContainerScreen containerScreen = REIRuntime.getInstance().getPreviousContainerScreen(); boolean[] visible = {false}; List[] errorTooltip = new List[]{null}; @@ -177,8 +178,18 @@ public final class InternalWidgets { str.add(errorTooltip[0].get(0).copy().withStyle(ChatFormatting.RED)); } } - if ((Minecraft.getInstance().options.advancedItemTooltips || Screen.hasShiftDown()) && displaySupplier.get().getDisplayLocation().isPresent()) { - str.add(new TranslatableComponent("text.rei.recipe_id", "", new TextComponent(displaySupplier.get().getDisplayLocation().get().toString()).withStyle(ChatFormatting.GRAY)).withStyle(ChatFormatting.GRAY)); + if (Minecraft.getInstance().options.advancedItemTooltips || Screen.hasShiftDown()) { + Collection locations = idsSupplier.get(); + if (!locations.isEmpty()) { + str.add(new TextComponent(" ")); + for (ResourceLocation location : locations) { + String t = I18n.get("text.rei.recipe_id", "", new TextComponent(location.toString()).withStyle(ChatFormatting.GRAY)); + if (t.startsWith("\n")) { + t = t.substring("\n".length()); + } + str.add(new TextComponent(t).withStyle(ChatFormatting.GRAY)); + } + } } return str.toArray(new Component[0]); }); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java index 0710d0a17..d5e73916d 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java @@ -36,6 +36,7 @@ import me.shedaniel.rei.api.client.view.ViewSearchBuilder; import me.shedaniel.rei.api.client.view.Views; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.Display; +import me.shedaniel.rei.api.common.display.DisplayMerger; import me.shedaniel.rei.api.common.entry.EntryIngredient; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.plugins.PluginManager; @@ -47,8 +48,10 @@ import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.api.common.util.EntryIngredients; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.impl.client.gui.craftable.CraftableFilter; +import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.client.Minecraft; import net.minecraft.client.player.LocalPlayer; +import net.minecraft.resources.ResourceLocation; import net.minecraft.world.inventory.AbstractContainerMenu; import org.jetbrains.annotations.ApiStatus; @@ -58,7 +61,7 @@ import java.util.stream.Collectors; @ApiStatus.Internal public class ViewsImpl implements Views { - public static Map, List> buildMapFor(ViewSearchBuilder builder) { + public static Map, List> buildMapFor(ViewSearchBuilder builder) { if (PluginManager.areAnyReloading()) { RoughlyEnoughItemsCore.LOGGER.info("Cancelled Views buildMap since plugins have not finished reloading."); return Maps.newLinkedHashMap(); @@ -155,6 +158,76 @@ public class ViewsImpl implements Views { generateLiveDisplays(displayRegistry, generator, builder, displayConsumer); } + Map, List> resultSpeced = (Map, List>) (Map) new LinkedHashMap<>(result); + // optimize displays + if (ConfigObject.getInstance().doMergeDisplayUnderOne()) { + for (Map.Entry, List> entry : result.entrySet()) { + DisplayMerger merger = (DisplayMerger) entry.getKey().getDisplayMerger(); + + if (merger != null) { + class Wrapped implements DisplaySpec { + private Display display; + private List ids = null; + + public Wrapped(Display display) { + this.display = display; + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (!(o instanceof Wrapped)) return false; + Wrapped wrapped = (Wrapped) o; + return merger.canMerge(display, wrapped.display); + } + + @Override + public int hashCode() { + return merger.hashOf(display); + } + + @Override + public Display provideInternalDisplay() { + return display; + } + + @Override + public Collection provideInternalDisplayIds() { + if (ids == null) { + ids = new ArrayList<>(); + Optional location = display.getDisplayLocation(); + if (location.isPresent()) { + ids.add(location.get()); + } + } + return ids; + } + + public void add(Display display) { + Optional location = display.getDisplayLocation(); + if (location.isPresent()) { + provideInternalDisplayIds().add(location.get()); + } + } + } + Map wrappedSet = new LinkedHashMap<>(); + List wrappeds = new ArrayList<>(); + + for (Display display : entry.getValue()) { + Wrapped wrapped = new Wrapped(display); + if (wrappedSet.containsKey(wrapped)) { + wrappedSet.get(wrapped).add(display); + } else { + wrappedSet.put(wrapped, wrapped); + wrappeds.add(wrapped); + } + } + + resultSpeced.put(entry.getKey(), (List) (List) wrappeds); + } + } + } + String message = String.format("Built Recipe View in %s for %d categories, %d recipes for, %d usages for and %d live recipe generators.", stopwatch.stop(), categories.size(), recipesFor.size(), usagesFor.size(), generatorsCount); if (ConfigObject.getInstance().doDebugSearchTimeRequired()) { @@ -162,7 +235,7 @@ public class ViewsImpl implements Views { } else { RoughlyEnoughItemsCore.LOGGER.trace(message); } - return result; + return resultSpeced; } private static void generateLiveDisplays(DisplayRegistry displayRegistry, DynamicDisplayGenerator generator, ViewSearchBuilder builder, Consumer displayConsumer) { -- cgit From b9ab61fe8a1c10f5adb61814ad9328b29727a37a Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 2 Nov 2021 16:06:10 +0800 Subject: Refactor MenuInfoProvider to give more concrete info --- .../shedaniel/rei/impl/client/view/ViewsImpl.java | 72 +++++++++++----------- .../rei/impl/common/transfer/InputSlotCrafter.java | 16 +++-- .../impl/common/transfer/MenuInfoRegistryImpl.java | 28 ++++++++- .../autocrafting/DefaultCategoryHandler.java | 2 +- 4 files changed, 75 insertions(+), 43 deletions(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java index d5e73916d..a76cce52f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/view/ViewsImpl.java @@ -280,44 +280,46 @@ public class ViewsImpl implements Views { AbstractContainerMenu menu = Minecraft.getInstance().player.containerMenu; Set> craftables = new HashSet<>(); for (Map.Entry, List> entry : DisplayRegistry.getInstance().getAll().entrySet()) { - MenuInfo info = menu != null ? - (MenuInfo) MenuInfoRegistry.getInstance().get(entry.getKey(), menu.getClass()) - : null; - - class InfoContext implements MenuInfoContext { - private Display display; - - @Override - public AbstractContainerMenu getMenu() { - return menu; - } - - @Override - public LocalPlayer getPlayerEntity() { - return Minecraft.getInstance().player; - } - - @Override - public MenuInfo getContainerInfo() { - return info; - } + List displays = entry.getValue(); + for (Display display : displays) { + MenuInfo info = menu != null ? + MenuInfoRegistry.getInstance().getClient(display, menu) + : null; - @Override - public CategoryIdentifier getCategoryIdentifier() { - return (CategoryIdentifier) entry.getKey(); + class InfoContext implements MenuInfoContext { + private Display display; + + public InfoContext(Display display) { + this.display = display; + } + + @Override + public AbstractContainerMenu getMenu() { + return menu; + } + + @Override + public LocalPlayer getPlayerEntity() { + return Minecraft.getInstance().player; + } + + @Override + public MenuInfo getContainerInfo() { + return info; + } + + @Override + public CategoryIdentifier getCategoryIdentifier() { + return (CategoryIdentifier) entry.getKey(); + } + + @Override + public Display getDisplay() { + return display; + } } - @Override - public Display getDisplay() { - return display; - } - } - - InfoContext context = new InfoContext(); - - List displays = entry.getValue(); - for (Display display : displays) { - context.display = display; + InfoContext context = new InfoContext(display); Iterable inputSlots = info != null ? info.getInputSlots(context) : Collections.emptySet(); int slotsCraftable = 0; List requiredInput = display.getRequiredEntries(); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java index 8c5df5b53..aa233ca85 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/InputSlotCrafter.java @@ -55,16 +55,24 @@ public class InputSlotCrafter inventoryStacks; private ServerPlayer player; - private InputSlotCrafter(CategoryIdentifier category, T container, CompoundTag display, MenuInfo menuInfo) { + private InputSlotCrafter(CategoryIdentifier category, T container) { this.category = category; this.container = container; + } + + public void setDisplay(D display) { + this.display = display; + } + + public void setMenuInfo(MenuInfo menuInfo) { this.menuInfo = menuInfo; - this.display = menuInfo.read(this, display); } public static InputSlotCrafter start(CategoryIdentifier category, T menu, ServerPlayer player, CompoundTag display, boolean hasShift) { - MenuInfo menuInfo = Objects.requireNonNull(MenuInfoRegistry.getInstance().get(category, (Class) menu.getClass()), "Container Info does not exist on the server!"); - InputSlotCrafter crafter = new InputSlotCrafter<>(category, menu, display, menuInfo); + InputSlotCrafter crafter = new InputSlotCrafter<>(category, menu); + MenuInfo menuInfo = Objects.requireNonNull(MenuInfoRegistry.getInstance().get(category, menu, crafter, display), "Container Info does not exist on the server!"); + crafter.setMenuInfo(menuInfo); + crafter.setDisplay(menuInfo.read(crafter, display)); crafter.fillInputSlots(player, hasShift); return crafter; } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/MenuInfoRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/MenuInfoRegistryImpl.java index bf84c1808..a42cf8d89 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/MenuInfoRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/transfer/MenuInfoRegistryImpl.java @@ -31,12 +31,17 @@ import me.shedaniel.rei.api.common.plugins.REIServerPlugin; import me.shedaniel.rei.api.common.transfer.info.MenuInfo; import me.shedaniel.rei.api.common.transfer.info.MenuInfoProvider; import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; +import me.shedaniel.rei.api.common.transfer.info.MenuSerializationProviderContext; +import net.fabricmc.api.EnvType; +import net.fabricmc.api.Environment; +import net.minecraft.nbt.CompoundTag; import net.minecraft.world.inventory.AbstractContainerMenu; import org.jetbrains.annotations.Nullable; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.function.Function; import java.util.function.Predicate; public class MenuInfoRegistryImpl implements MenuInfoRegistry { @@ -58,11 +63,28 @@ public class MenuInfoRegistryImpl implements MenuInfoRegistry { @Override @Nullable public MenuInfo get(CategoryIdentifier category, Class menuClass) { + return getInternal(category, menuClass, provider -> provider.provide(category, menuClass)); + } + + @Override + @Nullable + @Environment(EnvType.CLIENT) + public MenuInfo getClient(D display, C menu) { + return getInternal((CategoryIdentifier) display.getCategoryIdentifier(), (Class) menu.getClass(), provider -> provider.provideClient(display, menu)); + } + + @Override + @Nullable + public MenuInfo get(CategoryIdentifier category, C menu, MenuSerializationProviderContext context, CompoundTag tag) { + return getInternal(category, (Class) menu.getClass(), provider -> provider.provide(category, menu, context, tag)); + } + + private MenuInfo getInternal(CategoryIdentifier category, Class menuClass, Function, Optional>> function) { Map, List>> infoMap = map.get(category); if (infoMap != null && !infoMap.isEmpty()) { if (infoMap.containsKey(menuClass)) { for (MenuInfoProvider provider : infoMap.get(menuClass)) { - Optional> info = ((MenuInfoProvider) provider).provide(category, menuClass); + Optional> info = function.apply((MenuInfoProvider) provider); if (info.isPresent()) { return info.get(); } @@ -71,7 +93,7 @@ public class MenuInfoRegistryImpl implements MenuInfoRegistry { for (Map.Entry, List>> entry : infoMap.entrySet()) { if (entry.getKey().isAssignableFrom(menuClass)) { for (MenuInfoProvider provider : entry.getValue()) { - Optional> info = ((MenuInfoProvider) provider).provide(category, menuClass); + Optional> info = function.apply((MenuInfoProvider) provider); if (info.isPresent()) { return info.get(); } @@ -84,7 +106,7 @@ public class MenuInfoRegistryImpl implements MenuInfoRegistry { if (entry.getKey().test(category) && !entry.getValue().isEmpty()) { List> infoList = entry.getValue(); if (!infoList.isEmpty()) { - Optional> info = ((MenuInfoProvider) infoList.get(0)).provide(category, menuClass); + Optional> info = function.apply((MenuInfoProvider) infoList.get(0)); if (info.isPresent()) { return info.get(); } diff --git a/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java b/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java index 14f50a0d7..1c9703d5e 100644 --- a/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java +++ b/runtime/src/main/java/me/shedaniel/rei/plugin/autocrafting/DefaultCategoryHandler.java @@ -61,7 +61,7 @@ public class DefaultCategoryHandler implements TransferHandler { return Result.createNotApplicable(); } AbstractContainerMenu menu = context.getMenu(); - MenuInfo menuInfo = MenuInfoRegistry.getInstance().get((CategoryIdentifier) display.getCategoryIdentifier(), (Class) menu.getClass()); + MenuInfo menuInfo = MenuInfoRegistry.getInstance().getClient(display, menu); if (menuInfo == null) { return Result.createNotApplicable(); } -- cgit From 987ee5269a9bc61b9ab4d07ea0986629b1421964 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Tue, 2 Nov 2021 16:18:43 +0800 Subject: Fix queueSearchUpdate --- .../java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java | 9 ++++++++- .../shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java | 2 +- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'runtime/src/main/java') diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java index 04901ba45..bc53fca48 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/ScreenOverlayImpl.java @@ -106,6 +106,7 @@ public class ScreenOverlayImpl extends ScreenOverlay { private static FavoritesListWidget favoritesListWidget = null; private final List widgets = Lists.newLinkedList(); public boolean shouldReload = false; + public boolean shouldReloadSearch = false; private Rectangle bounds; private Window window; private Button leftButton, rightButton; @@ -207,6 +208,10 @@ public class ScreenOverlayImpl extends ScreenOverlay { shouldReload = true; } + public void queueReloadSearch() { + shouldReloadSearch = true; + } + @Override public DraggingContext getDraggingContext() { return draggingStack; @@ -221,6 +226,7 @@ public class ScreenOverlayImpl extends ScreenOverlay { DraggableStackVisitor.from(() -> ScreenRegistry.getInstance().getDraggableVisitors())); this.shouldReload = false; + this.shouldReloadSearch = false; //Update Variables this.children().clear(); this.closeOverlayMenu(); @@ -469,7 +475,8 @@ public class ScreenOverlayImpl extends ScreenOverlay { } } } - if (ConfigManager.getInstance().isCraftableOnlyEnabled() && CraftableFilter.INSTANCE.wasDirty()) { + if (shouldReloadSearch || (ConfigManager.getInstance().isCraftableOnlyEnabled() && CraftableFilter.INSTANCE.wasDirty())) { + shouldReloadSearch = false; ENTRY_LIST_WIDGET.updateSearch(REIRuntimeImpl.getSearchField().getText(), true); } if (OverlaySearchField.isHighlighting) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java index 883f62ec1..272bd1944 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/common/entry/type/EntryRegistryImpl.java @@ -200,7 +200,7 @@ public class EntryRegistryImpl implements EntryRegistry { private void queueSearchUpdate() { if (REIRuntimeImpl.getSearchField() != null) { - ScreenOverlayImpl.getEntryListWidget().updateSearch(REIRuntimeImpl.getSearchField().getText(), true); + ScreenOverlayImpl.getInstance().queueReloadSearch(); } } -- cgit