diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-01-27 21:44:50 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-01-27 22:05:59 +0800 |
| commit | 7e6321f649111cdeb0b391ef3203864247bd7b9d (patch) | |
| tree | c5532344f3cdde505afb00f3b75fd16152371702 /api/src/main/java/me | |
| parent | ef23b11140085651287ce35deca87723daf34e78 (diff) | |
| download | RoughlyEnoughItems-7e6321f649111cdeb0b391ef3203864247bd7b9d.tar.gz RoughlyEnoughItems-7e6321f649111cdeb0b391ef3203864247bd7b9d.tar.bz2 RoughlyEnoughItems-7e6321f649111cdeb0b391ef3203864247bd7b9d.zip | |
Fix #730
Diffstat (limited to 'api/src/main/java/me')
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ClickArea.java | 25 | ||||
| -rw-r--r-- | api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ScreenRegistry.java | 56 |
2 files changed, 80 insertions, 1 deletions
diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ClickArea.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ClickArea.java index 536951c66..3f6803d97 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ClickArea.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ClickArea.java @@ -29,8 +29,12 @@ import me.shedaniel.rei.impl.ClientInternals; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.client.gui.screens.Screen; +import net.minecraft.network.chat.Component; import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; +import java.util.function.BooleanSupplier; +import java.util.function.Supplier; import java.util.stream.Stream; @FunctionalInterface @@ -55,6 +59,18 @@ public interface ClickArea<T extends Screen> { return ClientInternals.createClickAreaHandlerResult(false); } + /** + * Sets a custom execute function. + * Returns {@code true} to indicate that the click area was executed. + * Returns {@code false} to indicate that the click area was not executed, and + * leaves REI to handle the click area. + * + * @param task the task to execute + * @return this + */ + @ApiStatus.Experimental + Result executor(BooleanSupplier task); + Result category(CategoryIdentifier<?> category); default Result categories(Iterable<? extends CategoryIdentifier<?>> categories) { @@ -64,8 +80,17 @@ public interface ClickArea<T extends Screen> { return this; } + @ApiStatus.Experimental + Result tooltip(Supplier<Component @Nullable []> tooltip); + boolean isSuccessful(); + @ApiStatus.Experimental + boolean execute(); + + @ApiStatus.Experimental + Component @Nullable [] getTooltips(); + Stream<CategoryIdentifier<?>> getCategories(); } } diff --git a/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ScreenRegistry.java b/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ScreenRegistry.java index e89712cca..7e8722928 100644 --- a/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ScreenRegistry.java +++ b/api/src/main/java/me/shedaniel/rei/api/client/registry/screen/ScreenRegistry.java @@ -31,19 +31,29 @@ import me.shedaniel.rei.api.client.gui.drag.DraggableStackProviderWidget; import me.shedaniel.rei.api.client.gui.drag.DraggableStackVisitor; import me.shedaniel.rei.api.client.gui.drag.DraggableStackVisitorWidget; import me.shedaniel.rei.api.client.plugins.REIClientPlugin; +import me.shedaniel.rei.api.client.view.ViewSearchBuilder; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.entry.EntryStack; import me.shedaniel.rei.api.common.plugins.PluginManager; import me.shedaniel.rei.api.common.registry.Reloadable; +import me.shedaniel.rei.api.common.util.CollectionUtils; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; +import net.minecraft.client.Minecraft; import net.minecraft.client.gui.screens.Screen; import net.minecraft.client.gui.screens.inventory.AbstractContainerScreen; +import net.minecraft.client.resources.sounds.SimpleSoundInstance; +import net.minecraft.network.chat.Component; +import net.minecraft.sounds.SoundEvents; import net.minecraft.world.inventory.AbstractContainerMenu; +import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.Nullable; +import java.util.Arrays; +import java.util.Collections; import java.util.List; import java.util.Set; +import java.util.stream.Collectors; @Environment(EnvType.CLIENT) public interface ScreenRegistry extends Reloadable<REIClientPlugin> { @@ -182,5 +192,49 @@ public interface ScreenRegistry extends Reloadable<REIClientPlugin> { * @param <T> the type of screen * @return the collection of category identifiers, may be null if there are no click area handlers. */ - @Nullable <T extends Screen> Set<CategoryIdentifier<?>> handleClickArea(Class<T> screenClass, ClickArea.ClickAreaContext<T> context); + @Nullable + @Deprecated + @ApiStatus.ScheduledForRemoval + default <T extends Screen> Set<CategoryIdentifier<?>> handleClickArea(Class<T> screenClass, ClickArea.ClickAreaContext<T> context) { + List<ClickArea.Result> results = evaluateClickArea(screenClass, context); + Set<CategoryIdentifier<?>> identifiers = results.stream().flatMap(ClickArea.Result::getCategories).collect(Collectors.toSet()); + return identifiers.isEmpty() ? null : identifiers; + } + + /** + * Handles the click area, returns the list of successful results. + * + * @param screenClass the class of the screen + * @param context the click area context + * @param <T> the type of screen + * @return the list of successful results, may be empty. + */ + <T extends Screen> List<ClickArea.Result> evaluateClickArea(Class<T> screenClass, ClickArea.ClickAreaContext<T> context); + + @Nullable + default <T extends Screen> List<Component> getClickAreaTooltips(Class<T> screenClass, ClickArea.ClickAreaContext<T> context) { + List<Component> tooltips = CollectionUtils.flatMap(evaluateClickArea(screenClass, context), result -> { + Component[] components = result.getTooltips(); + return components == null ? Collections.emptyList() : Arrays.asList(components); + }); + return tooltips.isEmpty() ? null : tooltips; + } + + default <T extends Screen> boolean executeClickArea(Class<T> screenClass, ClickArea.ClickAreaContext<T> context) { + List<ClickArea.Result> results = evaluateClickArea(screenClass, context); + for (ClickArea.Result result : results) { + if (result.execute()) { + return true; + } + } + + Set<CategoryIdentifier<?>> categories = results.stream().flatMap(ClickArea.Result::getCategories).collect(Collectors.toSet()); + if (!categories.isEmpty()) { + ViewSearchBuilder.builder().addCategories(categories).open(); + Minecraft.getInstance().getSoundManager().play(SimpleSoundInstance.forUI(SoundEvents.UI_BUTTON_CLICK, 1.0F)); + return true; + } + + return false; + } } |
