diff options
Diffstat (limited to 'runtime/src/main/java/me')
16 files changed, 776 insertions, 240 deletions
diff --git a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java index f1b1110e8..5d5d53d8f 100644 --- a/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java +++ b/runtime/src/main/java/me/shedaniel/rei/RoughlyEnoughItemsCoreClient.java @@ -78,6 +78,7 @@ import me.shedaniel.rei.impl.client.registry.category.CategoryRegistryImpl; import me.shedaniel.rei.impl.client.registry.display.DisplayRegistryImpl; import me.shedaniel.rei.impl.client.registry.screen.ScreenRegistryImpl; import me.shedaniel.rei.impl.client.search.SearchProviderImpl; +import me.shedaniel.rei.impl.client.search.SearchRuntime; import me.shedaniel.rei.impl.client.search.method.InputMethodRegistryImpl; import me.shedaniel.rei.impl.client.subsets.SubsetsRegistryImpl; import me.shedaniel.rei.impl.client.transfer.TransferHandlerRegistryImpl; @@ -231,6 +232,7 @@ public class RoughlyEnoughItemsCoreClient { new FavoriteEntryTypeRegistryImpl(), new SubsetsRegistryImpl(), new TransferHandlerRegistryImpl(), + new SearchRuntime(), new REIRuntimeImpl(), new ConfigAddonRegistryImpl()), "clientPluginManager"); } @@ -351,6 +353,7 @@ public class RoughlyEnoughItemsCoreClient { } REIRuntimeImpl.getInstance().setPreviousScreen(screen); + ((ScreenRegistryImpl) ScreenRegistry.getInstance()).getLastRendererProvider(screen); } if (ConfigObject.getInstance().doesDisableRecipeBook() && screen instanceof AbstractContainerScreen) { access.getRenderables().removeIf(widget -> widget instanceof ImageButton button && button.resourceLocation.equals(recipeButtonTex)); @@ -408,53 +411,6 @@ public class RoughlyEnoughItemsCoreClient { return EventResult.interruptFalse(); return EventResult.pass(); }); - int[] rendered = {0}; - ClientGuiEvent.RENDER_PRE.register((screen, matrices, mouseX, mouseY, delta) -> { - if (shouldReturn(screen)) - return EventResult.pass(); - rendered[0] = 0; - return EventResult.pass(); - }); - ClientGuiEvent.RENDER_CONTAINER_BACKGROUND.register((screen, matrices, mouseX, mouseY, delta) -> { - if (shouldReturn(screen)) - return; - rendered[0] = 1; - resetFocused(screen); - if (!(screen instanceof DisplayScreen)) { - getOverlay().render(matrices, mouseX, mouseY, delta); - } - resetFocused(screen); - }); - ClientGuiEvent.RENDER_CONTAINER_FOREGROUND.register((screen, matrices, mouseX, mouseY, delta) -> { - if (shouldReturn(screen)) - return; - rendered[0] = 2; - resetFocused(screen); - PoseStack poseStack = RenderSystem.getModelViewStack(); - poseStack.pushPose(); - poseStack.translate(-screen.leftPos, -screen.topPos, 0.0); - RenderSystem.applyModelViewMatrix(); - ((ScreenOverlayImpl) getOverlay()).lateRender(matrices, mouseX, mouseY, delta); - poseStack.popPose(); - RenderSystem.applyModelViewMatrix(); - resetFocused(screen); - }); - ClientGuiEvent.RENDER_POST.register((screen, matrices, mouseX, mouseY, delta) -> { - if (shouldReturn(screen) || rendered[0] == 2) - return; - if (screen instanceof AbstractContainerScreen) { - InternalLogger.getInstance().warn("Screen " + screen.getClass().getName() + " did not render background and foreground! This might cause rendering issues!"); - } - resetFocused(screen); - if (rendered[0] == 0 && !(screen instanceof DisplayScreen)) { - getOverlay().render(matrices, mouseX, mouseY, delta); - } - rendered[0] = 1; - if (rendered[0] == 1) { - ((ScreenOverlayImpl) getOverlay()).lateRender(matrices, mouseX, mouseY, delta); - } - resetFocused(screen); - }); ClientScreenInputEvent.MOUSE_DRAGGED_PRE.register((minecraftClient, screen, mouseX1, mouseY1, button, mouseX2, mouseY2) -> { if (shouldReturn(screen) || screen instanceof DisplayScreen) return EventResult.pass(); @@ -504,7 +460,7 @@ public class RoughlyEnoughItemsCoreClient { }); } - private boolean resetFocused(Screen screen) { + public static boolean resetFocused(Screen screen) { if (screen.getFocused() instanceof ScreenOverlay || screen.getFocused() == screen) { screen.setFocused(null); } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java index 2e7018ad8..fad1fdf6c 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/REIRuntimeImpl.java @@ -47,7 +47,6 @@ import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl; import me.shedaniel.rei.impl.client.gui.hints.HintProvider; import me.shedaniel.rei.impl.client.gui.widget.CachedEntryListRender; import me.shedaniel.rei.impl.client.gui.widget.search.OverlaySearchField; -import me.shedaniel.rei.impl.client.search.argument.Argument; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.Minecraft; @@ -255,7 +254,6 @@ public class REIRuntimeImpl implements REIRuntime { @Override public void startReload() { - Argument.resetCache(false); getOverlay().ifPresent(ScreenOverlay::queueReloadOverlay); lastDisplayScreen.clear(); if (!RenderSystem.isOnRenderThread()) { @@ -272,7 +270,6 @@ public class REIRuntimeImpl implements REIRuntime { @Override public void endReload(ReloadStage stage) { - Argument.resetCache(true); getOverlay().ifPresent(ScreenOverlay::queueReloadOverlay); } 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 f8e1f02d0..47f84a7c4 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 @@ -303,6 +303,11 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { } @Override + public boolean doesCacheDisplayLookup() { + return advanced.miscellaneous.cachingDisplayLookup; + } + + @Override public boolean doDebugRenderTimeRequired() { return advanced.layout.debugRenderTimeRequired; } @@ -726,6 +731,7 @@ public class ConfigObjectImpl implements ConfigObject, ConfigData { private boolean newFastEntryRendering = true; @ConfigEntry.Gui.PrefixText private boolean cachingFastEntryRendering = false; + private boolean cachingDisplayLookup = true; } public static class Filtering { 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 a95d52069..c0e9e2b01 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 @@ -54,10 +54,7 @@ import net.minecraft.util.Mth; import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Optional; +import java.util.*; @ApiStatus.Internal public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen { @@ -183,7 +180,7 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen ViewSearchBuilder.builder().addAllCategories().open(); }).tooltip(Component.translatable("text.rei.view_all_categories")).noShadow().color(0xFF404040, 0xFFBBBBBB).hoveredColor(0xFF0041FF, 0xFFFFBD4D)); - this.children().addAll(buttonList); + this.widgets.add(new ButtonListWidget(buttonList)); this.children().addAll(widgets); } @@ -270,6 +267,7 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen @Override public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { + this.widgets.sort(Comparator.comparingDouble(Widget::getZRenderingPriority)); if (ConfigObject.getInstance().isCompositeScrollBarPermanent()) { scrollBarAlphaFutureTime = System.currentTimeMillis(); scrollBarAlphaFuture = 0; @@ -294,32 +292,12 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen } scrolling.updatePosition(delta); renderBackground(matrices); - int yOffset = 0; getOverlay().render(matrices, mouseX, mouseY, delta); super.render(matrices, mouseX, mouseY, delta); for (Widget widget : widgets) { RenderSystem.setShaderColor(1.0F, 1.0F, 1.0F, 1.0F); widget.render(matrices, mouseX, mouseY, delta); } - matrices.pushPose(); - ScissorsHandler.INSTANCE.scissor(scrolling.getBounds()); - for (Button button : buttonList) { - button.getBounds().y = scrollListBounds.y + 1 + yOffset - scrolling.scrollAmountInt(); - if (button.getBounds().getMaxY() > scrollListBounds.getMinY() && button.getBounds().getMinY() < scrollListBounds.getMaxY()) { - button.render(matrices, mouseX, mouseY, delta); - } - yOffset += button.getBounds().height; - } - for (int i = 0; i < buttonList.size(); i++) { - if (buttonList.get(i).getBounds().getMaxY() > scrollListBounds.getMinY() && buttonList.get(i).getBounds().getMinY() < scrollListBounds.getMaxY()) { - displayRenderers.get(i).setZ(1); - displayRenderers.get(i).render(matrices, buttonList.get(i).getBounds(), mouseX, mouseY, delta); - Optional.ofNullable(displayRenderers.get(i).getTooltip(TooltipContext.of(new Point(mouseX, mouseY)))).ifPresent(Tooltip::queue); - } - } - scrolling.renderScrollBar(0, scrollBarAlpha, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); - ScissorsHandler.INSTANCE.removeLastScissor(); - matrices.popPose(); } @Override @@ -379,4 +357,41 @@ public class CompositeDisplayViewingScreen extends AbstractDisplayViewingScreen } return super.keyPressed(keyCode, scanCode, modifiers); } + + private class ButtonListWidget extends Widget { + private final List<Button> buttonList; + + public ButtonListWidget(List<Button> buttonList) { + this.buttonList = buttonList; + } + + @Override + public void render(PoseStack matrices, int mouseX, int mouseY, float delta) { + int yOffset = 0; + matrices.pushPose(); + ScissorsHandler.INSTANCE.scissor(scrolling.getBounds()); + for (Button button : buttonList) { + button.getBounds().y = scrollListBounds.y + 1 + yOffset - scrolling.scrollAmountInt(); + if (button.getBounds().getMaxY() > scrollListBounds.getMinY() && button.getBounds().getMinY() < scrollListBounds.getMaxY()) { + button.render(matrices, mouseX, mouseY, delta); + } + yOffset += button.getBounds().height; + } + for (int i = 0; i < buttonList.size(); i++) { + if (buttonList.get(i).getBounds().getMaxY() > scrollListBounds.getMinY() && buttonList.get(i).getBounds().getMinY() < scrollListBounds.getMaxY()) { + displayRenderers.get(i).setZ(1); + displayRenderers.get(i).render(matrices, buttonList.get(i).getBounds(), mouseX, mouseY, delta); + Optional.ofNullable(displayRenderers.get(i).getTooltip(TooltipContext.of(new Point(mouseX, mouseY)))).ifPresent(Tooltip::queue); + } + } + scrolling.renderScrollBar(0, scrollBarAlpha, REIRuntime.getInstance().isDarkThemeEnabled() ? 0.8f : 1f); + ScissorsHandler.INSTANCE.removeLastScissor(); + matrices.popPose(); + } + + @Override + public List<? extends GuiEventListener> children() { + return buttonList; + } + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java index 7d23a89d7..777b44d06 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/EntryWidget.java @@ -60,6 +60,8 @@ import me.shedaniel.rei.impl.client.gui.InternalTextures; import me.shedaniel.rei.impl.client.gui.ScreenOverlayImpl; import me.shedaniel.rei.impl.client.gui.dragging.CurrentDraggingStack; import me.shedaniel.rei.impl.client.gui.widget.favorites.FavoritesListWidget; +import me.shedaniel.rei.impl.client.registry.display.DisplayRegistryImpl; +import me.shedaniel.rei.impl.client.registry.display.DisplaysHolder; import me.shedaniel.rei.impl.client.view.ViewsImpl; import net.minecraft.ChatFormatting; import net.minecraft.CrashReport; @@ -317,22 +319,24 @@ public class EntryWidget extends Slot implements DraggableStackProviderWidget { try { DisplayRegistry displayRegistry = DisplayRegistry.getInstance(); + DisplaysHolder displaysHolder = ((DisplayRegistryImpl) displayRegistry).displaysHolder(); CategoryRegistry categoryRegistry = CategoryRegistry.getInstance(); Map<CategoryIdentifier<?>, Boolean> filteringQuickCraftCategories = ConfigObject.getInstance().getFilteringQuickCraftCategories(); - for (Map.Entry<CategoryIdentifier<?>, List<Display>> entry : displayRegistry.getAll().entrySet()) { + boolean shouldFilterDisplays = ConfigObject.getInstance().shouldFilterDisplays(); + + for (Display display : displaysHolder.getAllDisplaysByOutputs(getEntries())) { + CategoryIdentifier<?> categoryIdentifier = display.getCategoryIdentifier(); Optional<? extends CategoryRegistry.CategoryConfiguration<?>> configuration; - if ((configuration = categoryRegistry.tryGet(entry.getKey())).isEmpty() - || categoryRegistry.isCategoryInvisible(configuration.get().getCategory())) continue; - if (!filteringQuickCraftCategories.getOrDefault(entry.getKey(), configuration.get().isQuickCraftingEnabledByDefault())) continue; - for (Display display : entry.getValue()) { - if ((!ConfigObject.getInstance().shouldFilterDisplays() || displayRegistry.isDisplayVisible(display)) - && ViewsImpl.isRecipesFor(getEntries(), display)) { - AutoCraftingEvaluator.AutoCraftingResult result = AutoCraftingEvaluator.evaluateAutoCrafting(false, false, display, null); - if (result.successful) { - this.display = display; - this.displayTooltipComponent = Suppliers.memoize(() -> new DisplayTooltipComponent(display)); - return result.successfulHandler; - } + if ((configuration = categoryRegistry.tryGet(categoryIdentifier)).isEmpty() + || categoryRegistry.isCategoryInvisible(configuration.get().getCategory())) continue; + if (!filteringQuickCraftCategories.getOrDefault(categoryIdentifier, configuration.get().isQuickCraftingEnabledByDefault())) + continue; + if ((!shouldFilterDisplays || displayRegistry.isDisplayVisible(display))) { + AutoCraftingEvaluator.AutoCraftingResult result = AutoCraftingEvaluator.evaluateAutoCrafting(false, false, display, null); + if (result.successful) { + this.display = display; + this.displayTooltipComponent = Suppliers.memoize(() -> new DisplayTooltipComponent(display)); + return result.successfulHandler; } } } @@ -351,7 +355,7 @@ public class EntryWidget extends Slot implements DraggableStackProviderWidget { } if (display != null) { - if (ViewsImpl.isRecipesFor(getEntries(), display)) { + if (ViewsImpl.isRecipesFor(null, getEntries(), display)) { AutoCraftingEvaluator.AutoCraftingResult result = AutoCraftingEvaluator.evaluateAutoCrafting(false, false, display, null); if (result.successful) { return result.successfulHandler; @@ -428,7 +432,8 @@ public class EntryWidget extends Slot implements DraggableStackProviderWidget { } } - protected void drawExtra(PoseStack matrices, int mouseX, int mouseY, float delta) {} + protected void drawExtra(PoseStack matrices, int mouseX, int mouseY, float delta) { + } @Override @Nullable diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/PanelWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/PanelWidget.java index 2980f5016..f2233fb8a 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/PanelWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/basewidgets/PanelWidget.java @@ -56,7 +56,7 @@ public final class PanelWidget extends Panel { .asFloat(); public static boolean isRendering(Panel panel) { - return ConfigObject.getInstance().getRecipeScreenType() != DisplayScreenType.COMPOSITE; + return true; } public PanelWidget(Rectangle bounds) { diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/favorites/history/DisplayEntry.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/favorites/history/DisplayEntry.java index 148840d08..2fea96c39 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/favorites/history/DisplayEntry.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/favorites/history/DisplayEntry.java @@ -44,10 +44,7 @@ import net.minecraft.network.chat.Component; import org.jetbrains.annotations.Nullable; import org.joml.Vector4f; -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.UUID; +import java.util.*; import java.util.concurrent.TimeUnit; import java.util.function.Supplier; @@ -99,7 +96,7 @@ public class DisplayEntry extends WidgetWithBounds { CategoryRegistry.CategoryConfiguration<Display> configuration = CategoryRegistry.getInstance().get((CategoryIdentifier<Display>) display.getCategoryIdentifier()); DisplayCategory<Display> category = configuration.getCategory(); Rectangle displayBounds = new Rectangle(0, 0, category.getDisplayWidth(display), category.getDisplayHeight()); - List<Widget> widgets = configuration.getView(display).setupDisplay(display, displayBounds); + List<Widget> widgets = setupDisplay(configuration, displayBounds); float scale = 1.0F; if (parentBounds.width * scale < displayBounds.width) { scale = Math.min(scale, parentBounds.width * scale / (float) displayBounds.width); @@ -124,6 +121,19 @@ public class DisplayEntry extends WidgetWithBounds { return widgets; } + private List<Widget> setupDisplay(CategoryRegistry.CategoryConfiguration<Display> configuration, Rectangle displayBounds) { + try { + return configuration.getView(display).setupDisplay(display, displayBounds); + } catch (Throwable throwable) { + throwable.printStackTrace(); + List<Widget> w = new ArrayList<>(); + w.add(Widgets.createRecipeBase(displayBounds).color(0xFFBB0000)); + w.add(Widgets.createLabel(new Point(displayBounds.getCenterX(), displayBounds.getCenterY() - 8), Component.literal("Failed to initiate setupDisplay"))); + w.add(Widgets.createLabel(new Point(displayBounds.getCenterX(), displayBounds.getCenterY() + 1), Component.literal("Check console for error"))); + return w; + } + } + @Override public Rectangle getBounds() { return bounds.value().getBounds(); diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/hint/HintsContainerWidget.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/hint/HintsContainerWidget.java index ce31a07c3..28fda4336 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/hint/HintsContainerWidget.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/gui/widget/hint/HintsContainerWidget.java @@ -85,8 +85,9 @@ public class HintsContainerWidget extends DelegateWidget { if (!uuid.equals(this.config.UUID)) { this.config = new HintsConfig(); } - } catch (IOException e) { + } catch (Throwable e) { e.printStackTrace(); + this.config = new HintsConfig(); } } this.config.UUID = uuid; diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java index b875b8530..1aff7f720 100644 --- a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplayRegistryImpl.java @@ -24,10 +24,6 @@ package me.shedaniel.rei.impl.client.registry.display; import com.google.common.base.Preconditions; -import com.google.common.collect.ForwardingMap; -import com.google.common.collect.ForwardingMapEntry; -import com.google.common.collect.Iterators; -import com.google.common.collect.Sets; import dev.architectury.event.EventResult; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; import me.shedaniel.rei.api.client.registry.category.CategoryRegistry; @@ -43,9 +39,7 @@ import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.impl.common.InternalLogger; import me.shedaniel.rei.impl.common.registry.RecipeManagerContextImpl; import net.minecraft.world.item.crafting.Recipe; -import org.apache.commons.lang3.mutable.MutableInt; import org.apache.commons.lang3.mutable.MutableLong; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import java.util.*; @@ -53,127 +47,17 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.function.BiPredicate; import java.util.function.Function; import java.util.function.Predicate; -import java.util.function.UnaryOperator; public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugin> implements DisplayRegistry { - private final WeakHashMap<Display, Object> displaysBase = new WeakHashMap<>(); - private final Map<CategoryIdentifier<?>, DisplaysList> displays = new ConcurrentHashMap<>(); - private final Map<CategoryIdentifier<?>, List<Display>> unmodifiableDisplays; private final Map<CategoryIdentifier<?>, List<DynamicDisplayGenerator<?>>> displayGenerators = new ConcurrentHashMap<>(); private final List<DynamicDisplayGenerator<?>> globalDisplayGenerators = new ArrayList<>(); private final List<DisplayVisibilityPredicate> visibilityPredicates = new ArrayList<>(); private final List<DisplayFiller<?>> fillers = new ArrayList<>(); - private final MutableInt displayCount = new MutableInt(0); + private final MutableLong lastAddWarning = new MutableLong(-1); + private DisplaysHolder displaysHolder = new DisplaysHolderImpl(false); public DisplayRegistryImpl() { super(RecipeManagerContextImpl.supplier()); - - this.unmodifiableDisplays = new RemappingMap<>(Collections.unmodifiableMap(displays), list -> { - if (list == null) { - return null; - } else { - return ((DisplaysList) list).unmodifiableList; - } - }, key -> CategoryRegistry.getInstance().tryGet(key).isPresent()); - } - - private static class RemappingMap<K, V> extends ForwardingMap<K, V> { - protected final Map<K, V> map; - protected final UnaryOperator<V> remapper; - protected final Predicate<K> keyPredicate; - - public RemappingMap(Map<K, V> map, UnaryOperator<V> remapper, Predicate<K> keyPredicate) { - this.map = map; - this.remapper = remapper; - this.keyPredicate = keyPredicate; - } - - @Override - @NotNull - protected Map<K, V> delegate() { - return map; - } - - @Override - public V get(Object key) { - if (keyPredicate.test((K) key)) { - return remapper.apply(super.get(key)); - } else { - return null; - } - } - - @Override - public boolean containsKey(@Nullable Object key) { - return super.containsKey(key) && keyPredicate.test((K) key); - } - - @Override - public Set<K> keySet() { - return Sets.filter(super.keySet(), keyPredicate::test); - } - - @SuppressWarnings("UnstableApiUsage") - @Override - @NotNull - public Set<Entry<K, V>> entrySet() { - return this.new StandardEntrySet() { - @Override - public Iterator<Entry<K, V>> iterator() { - return mapIterator(map.entrySet().iterator()); - } - }; - } - - @Override - public int size() { - return keySet().size(); - } - - @Override - public Collection<V> values() { - return new AbstractCollection<V>() { - @Override - public Iterator<V> iterator() { - return Iterators.transform(entrySet().iterator(), Entry::getValue); - } - - @Override - public int size() { - return RemappingMap.this.size(); - } - }; - } - - private Iterator<Entry<K, V>> mapIterator(Iterator<Entry<K, V>> iterator) { - return Iterators.transform(Iterators.filter(iterator, entry -> this.keyPredicate.test(entry.getKey())), - this::mapEntry); - } - - private Entry<K, V> mapEntry(Entry<K, V> entry) { - return new ForwardingMapEntry<>() { - @Override - @NotNull - protected Entry<K, V> delegate() { - return entry; - } - - @Override - public V getValue() { - return remapper.apply(entry.getValue()); - } - }; - } - } - - private static class DisplaysList extends ArrayList<Display> { - private final List<Display> unmodifiableList; - private final List<Display> synchronizedList; - - public DisplaysList() { - this.synchronizedList = Collections.synchronizedList(this); - this.unmodifiableList = Collections.unmodifiableList(synchronizedList); - } } @Override @@ -183,11 +67,9 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi @Override public int displaySize() { - return displayCount.getValue(); + return this.displaysHolder.size(); } - private MutableLong lastAddWarning = new MutableLong(-1); - @Override public void add(Display display, @Nullable Object origin) { if (!PluginManager.areAnyReloading()) { @@ -199,19 +81,12 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi } } - displays.computeIfAbsent(display.getCategoryIdentifier(), location -> new DisplaysList()) - .add(display); - displayCount.increment(); - if (origin != null) { - synchronized (displaysBase) { - displaysBase.put(display, origin); - } - } + this.displaysHolder.add(display, origin); } @Override public Map<CategoryIdentifier<?>, List<Display>> getAll() { - return unmodifiableDisplays; + return this.displaysHolder.get(); } @Override @@ -287,11 +162,10 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi @Override public void startReload() { super.startReload(); - this.displays.clear(); + this.displaysHolder = new DisplaysHolderImpl(true); this.displayGenerators.clear(); this.visibilityPredicates.clear(); this.fillers.clear(); - this.displayCount.setValue(0); } @Override @@ -304,13 +178,19 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi } } - for (CategoryIdentifier<?> identifier : displays.keySet()) { + for (CategoryIdentifier<?> identifier : getAll().keySet()) { if (CategoryRegistry.getInstance().tryGet(identifier).isEmpty()) { InternalLogger.getInstance().error("Found displays registered for unknown registry", new IllegalStateException(identifier.toString())); } } - InternalLogger.getInstance().debug("Registered %d displays", displayCount.getValue()); + this.displaysHolder.endReload(); + + InternalLogger.getInstance().debug("Registered %d displays", displaySize()); + } + + public DisplaysHolder displaysHolder() { + return displaysHolder; } @Override @@ -349,12 +229,13 @@ public class DisplayRegistryImpl extends RecipeManagerContextImpl<REIClientPlugi @Override @Nullable public Object getDisplayOrigin(Display display) { - return displaysBase.get(display); + return this.displaysHolder.getDisplayOrigin(display); } private record DisplayFiller<D extends Display>( BiPredicate<Object, DisplayAdditionReasons> predicate, Function<Object, D> mappingFunction - ) {} + ) { + } } diff --git a/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java new file mode 100644 index 000000000..07b5879cf --- /dev/null +++ b/runtime/src/main/java/me/shedaniel/rei/impl/client/registry/display/DisplaysHolder.java @@ -0,0 +1,96 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022, 2023 shedaniel + * < |
