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/api/client/config/ConfigObject.java | 2 ++ .../client/registry/display/DisplayCategory.java | 24 +++++++++++++ .../rei/api/client/view/ViewSearchBuilder.java | 20 ++++++++--- .../shedaniel/rei/api/common/display/Display.java | 23 ++++++++++++- .../rei/api/common/display/DisplayMerger.java | 30 +++++++++++++++++ .../me/shedaniel/rei/impl/display/DisplaySpec.java | 39 ++++++++++++++++++++++ 6 files changed, 132 insertions(+), 6 deletions(-) create mode 100644 api/src/main/java/me/shedaniel/rei/api/common/display/DisplayMerger.java create mode 100644 api/src/main/java/me/shedaniel/rei/impl/display/DisplaySpec.java (limited to 'api/src/main/java/me') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java index 791d2cd39..20165ffc1 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/config/ConfigObject.java @@ -111,6 +111,8 @@ public interface ConfigObject { boolean doDebugRenderTimeRequired(); + boolean doMergeDisplayUnderOne(); + ModifierKeyCode getFavoriteKeyCode(); ModifierKeyCode getRecipeKeybind(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java index 5be94aa76..9202a18ca 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayCategory.java @@ -31,12 +31,14 @@ import me.shedaniel.rei.api.client.gui.widgets.Widget; import me.shedaniel.rei.api.client.gui.widgets.Widgets; 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.util.Identifiable; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.network.chat.Component; import net.minecraft.resources.ResourceLocation; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; import java.util.Collections; import java.util.List; @@ -119,6 +121,28 @@ public interface DisplayCategory extends Identifiable { CategoryIdentifier getCategoryIdentifier(); + @Nullable + default DisplayMerger getDisplayMerger() { + return null; + } + + static DisplayMerger getContentMerger() { + return new DisplayMerger() { + @Override + public boolean canMerge(T first, T second) { + if (!first.getCategoryIdentifier().equals(second.getCategoryIdentifier())) return false; + if (!first.getInputEntries().equals(second.getInputEntries())) return false; + if (!first.getOutputEntries().equals(second.getOutputEntries())) return false; + return true; + } + + @Override + public int hashOf(T display) { + return display.getCategoryIdentifier().hashCode() * 31 * 31 * 31 + display.getInputEntries().hashCode() * 31 * 31 + display.getOutputEntries().hashCode(); + } + }; + } + @Override default ResourceLocation getIdentifier() { return getCategoryIdentifier().getIdentifier(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/view/ViewSearchBuilder.java b/api/src/main/java/me/shedaniel/rei/api/client/view/ViewSearchBuilder.java index 589970db9..9b8a7582c 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/view/ViewSearchBuilder.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/view/ViewSearchBuilder.java @@ -31,12 +31,11 @@ import me.shedaniel.rei.api.common.display.Display; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.util.CollectionUtils; import me.shedaniel.rei.impl.ClientInternals; +import me.shedaniel.rei.impl.display.DisplaySpec; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; -import java.util.Collection; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; public interface ViewSearchBuilder { static ViewSearchBuilder builder() { @@ -66,7 +65,18 @@ public interface ViewSearchBuilder { @Nullable CategoryIdentifier getPreferredOpenedCategory(); - Map, List> buildMap(); + @Deprecated + @ApiStatus.ScheduledForRemoval + default Map, List> buildMap() { + Map, List> map = new HashMap<>(); + for (Map.Entry, List> entry : buildMapInternal().entrySet()) { + map.put(entry.getKey(), CollectionUtils.map(entry.getValue(), DisplaySpec::provideInternalDisplay)); + } + return map; + } + + @ApiStatus.Internal + Map, List> buildMapInternal(); default boolean open() { return ClientHelper.getInstance().openView(this); diff --git a/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java b/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java index 14db33133..1ee2515e3 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/display/Display.java @@ -25,8 +25,12 @@ package me.shedaniel.rei.api.common.display; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.entry.EntryIngredient; +import me.shedaniel.rei.impl.display.DisplaySpec; import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; +import java.util.Collection; +import java.util.Collections; import java.util.List; import java.util.Optional; @@ -37,7 +41,7 @@ import java.util.Optional; * @see me.shedaniel.rei.api.common.display.basic.BasicDisplay * @see me.shedaniel.rei.api.client.registry.display.DisplayRegistry */ -public interface Display { +public interface Display extends DisplaySpec { /** * @return a list of inputs */ @@ -72,4 +76,21 @@ public interface Display { default Optional getDisplayLocation() { return Optional.empty(); } + + @Override + @ApiStatus.NonExtendable + default Display provideInternalDisplay() { + return this; + } + + @Override + @ApiStatus.NonExtendable + default Collection provideInternalDisplayIds() { + Optional location = getDisplayLocation(); + if (location.isPresent()) { + return Collections.singletonList(location.get()); + } else { + return Collections.emptyList(); + } + } } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/display/DisplayMerger.java b/api/src/main/java/me/shedaniel/rei/api/common/display/DisplayMerger.java new file mode 100644 index 000000000..e13e24cc1 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/api/common/display/DisplayMerger.java @@ -0,0 +1,30 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.api.common.display; + +public interface DisplayMerger { + boolean canMerge(T first, T second); + + int hashOf(T display); +} diff --git a/api/src/main/java/me/shedaniel/rei/impl/display/DisplaySpec.java b/api/src/main/java/me/shedaniel/rei/impl/display/DisplaySpec.java new file mode 100644 index 000000000..399386a32 --- /dev/null +++ b/api/src/main/java/me/shedaniel/rei/impl/display/DisplaySpec.java @@ -0,0 +1,39 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021 shedaniel + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package me.shedaniel.rei.impl.display; + +import me.shedaniel.rei.api.common.display.Display; +import net.minecraft.resources.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; + +import java.util.Collection; + +@ApiStatus.Internal +public interface DisplaySpec { + @ApiStatus.Internal + Display provideInternalDisplay(); + + @ApiStatus.Internal + Collection provideInternalDisplayIds(); +} -- cgit