From 88472ebfdce663fb3cbc5448cfa1137358ed9b16 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Sat, 28 Aug 2021 04:27:25 +0800 Subject: Fix #500 --- .../client/registry/category/CategoryRegistry.java | 2 ++ .../client/registry/display/DisplayRegistry.java | 25 ++++++++++++++++++++-- .../rei/api/common/plugins/PluginView.java | 24 +++++++++++++++++---- .../transfer/info/MenuTransferException.java | 16 +++++++++++++- .../rei/api/common/util/CollectionUtils.java | 8 +++++++ .../main/java/me/shedaniel/rei/impl/Internals.java | 2 ++ 6 files changed, 70 insertions(+), 7 deletions(-) (limited to 'api/src/main/java') diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java index 35817b419..6e83dfac2 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/category/CategoryRegistry.java @@ -109,6 +109,8 @@ public interface CategoryRegistry extends Reloadable, Iterable< CategoryConfiguration get(CategoryIdentifier category); + Optional> tryGet(CategoryIdentifier category); + void configure(CategoryIdentifier category, Consumer> action); int size(); diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java index be9c107ef..2ecb40967 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/display/DisplayRegistry.java @@ -34,6 +34,7 @@ import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.RecipeType; +import org.jetbrains.annotations.Nullable; import java.util.*; import java.util.function.Function; @@ -82,14 +83,31 @@ public interface DisplayRegistry extends RecipeManagerContext { * * @param display the display */ - void add(Display display); + default void add(Display display) { + add(display, null); + } + + /** + * Registers a display with an origin attached. + * + * @param display the display + */ + void add(Display display, @Nullable Object origin); /** * Registers a display by the object provided, to be filled during {@link #tryFillDisplay(Object)}. * * @param object the object to be filled */ - void add(Object object); + default void add(Object object) { + if (object instanceof Display) { + add((Display) object, null); + } else { + for (Display display : tryFillDisplay(object)) { + add(display, object); + } + } + } /** * Returns an unmodifiable map of displays visible to the player @@ -271,4 +289,7 @@ public interface DisplayRegistry extends RecipeManagerContext { * @return the collection of displays */ Collection tryFillDisplay(T value); + + @Nullable + Object getDisplayOrigin(Display display); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java b/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java index 101f62b0d..ddd74a367 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/plugins/PluginView.java @@ -31,7 +31,6 @@ import net.fabricmc.api.Environment; import org.jetbrains.annotations.ApiStatus; @ApiStatus.Internal -@FunctionalInterface public interface PluginView

> { @Environment(EnvType.CLIENT) static PluginView getClientInstance() { @@ -54,9 +53,26 @@ public interface PluginView

> { void registerPlugin(REIPluginProvider plugin); default PluginView

then(PluginView view) { - return plugin -> { - registerPlugin(plugin); - view.registerPlugin(plugin); + return new PluginView

() { + @Override + public void registerPlugin(REIPluginProvider plugin) { + PluginView.this.registerPlugin(plugin); + view.registerPlugin(plugin); + } + + @Override + public void pre() { + PluginView.this.pre(); + } + + @Override + public void post() { + PluginView.this.post(); + } }; } + + void pre(); + + void post(); } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuTransferException.java b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuTransferException.java index 5fff31cb7..0016e0866 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuTransferException.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/transfer/info/MenuTransferException.java @@ -28,16 +28,30 @@ import net.minecraft.network.chat.TranslatableComponent; public class MenuTransferException extends Exception { private final Component component; + private final boolean applicable; - public MenuTransferException(Component component) { + private MenuTransferException(Component component, boolean applicable) { this.component = component; + this.applicable = applicable; + } + + public MenuTransferException(Component component) { + this(component, true); } public MenuTransferException(String message) { this(new TranslatableComponent(message)); } + public static MenuTransferException createNotApplicable() { + return new MenuTransferException(null, false); + } + public Component getError() { return component; } + + public boolean isApplicable() { + return applicable; + } } diff --git a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java index 3f4054cbc..97934437d 100644 --- a/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java +++ b/api/src/main/java/me/shedaniel/rei/api/common/util/CollectionUtils.java @@ -138,6 +138,14 @@ public class CollectionUtils { return l; } + public static List flatMap(T[] list, Function> function) { + List l = new ArrayList<>(); + for (T t : list) { + l.addAll(function.apply(t)); + } + return l; + } + public static IntList mapToInt(Collection list, ToIntFunction function) { IntList l = new IntArrayList(list.size() + 1); for (T t : list) { diff --git a/api/src/main/java/me/shedaniel/rei/impl/Internals.java b/api/src/main/java/me/shedaniel/rei/impl/Internals.java index a5912535e..cbbc257ea 100644 --- a/api/src/main/java/me/shedaniel/rei/impl/Internals.java +++ b/api/src/main/java/me/shedaniel/rei/impl/Internals.java @@ -33,6 +33,7 @@ import me.shedaniel.rei.api.common.entry.type.EntryType; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.plugins.REIPlugin; import me.shedaniel.rei.api.common.plugins.REIServerPlugin; +import me.shedaniel.rei.api.common.transfer.info.MenuInfoRegistry; import net.minecraft.nbt.Tag; import net.minecraft.resources.ResourceLocation; import net.minecraft.util.Unit; @@ -51,6 +52,7 @@ public final class Internals { private static Supplier> serverPluginManager = Internals::throwNotSetup; private static Supplier nbtHasherProvider = Internals::throwNotSetup; private static Function> categoryIdentifier = (object) -> throwNotSetup(); + private static Supplier stubMenuInfoRegistry = Internals::throwNotSetup; private static T throwNotSetup() { throw new AssertionError("REI Internals have not been initialized!"); -- cgit