From e17e12e3e83b1789bb081b268195212e3d702c65 Mon Sep 17 00:00:00 2001 From: shedaniel Date: Thu, 1 Oct 2020 00:27:21 +0800 Subject: Allow a more specific click area handler. Signed-off-by: shedaniel --- .../me/shedaniel/rei/api/AutoTransferHandler.java | 2 + .../me/shedaniel/rei/api/ClickAreaHandler.java | 68 ++++++++++++++++++++++ .../java/me/shedaniel/rei/api/RecipeHelper.java | 19 +++++- .../shedaniel/rei/api/ScreenClickAreaProvider.java | 12 ++++ .../main/java/me/shedaniel/rei/impl/Internals.java | 7 +++ 5 files changed, 107 insertions(+), 1 deletion(-) create mode 100644 RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClickAreaHandler.java (limited to 'RoughlyEnoughItems-api') diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java index c6082e5a7..5b217df48 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java @@ -48,6 +48,7 @@ public interface AutoTransferHandler { @NotNull Result handle(@NotNull Context context); + @ApiStatus.NonExtendable interface Result { /** * Creates a successful result, no further handlers will be called. @@ -169,6 +170,7 @@ public interface AutoTransferHandler { IntList getIntegers(); } + @ApiStatus.NonExtendable interface Context { static Context create(boolean actuallyCrafting, ContainerScreen containerScreen, RecipeDisplay recipeDisplay) { return new ContextImpl(actuallyCrafting, containerScreen, () -> recipeDisplay); diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClickAreaHandler.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClickAreaHandler.java new file mode 100644 index 000000000..bb628d9a7 --- /dev/null +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClickAreaHandler.java @@ -0,0 +1,68 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020 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; + +import me.shedaniel.math.Point; +import me.shedaniel.rei.impl.Internals; +import net.minecraft.client.gui.screen.Screen; +import net.minecraft.util.ResourceLocation; +import org.jetbrains.annotations.ApiStatus; + +import java.util.stream.Stream; + +@FunctionalInterface +public interface ClickAreaHandler { + Result handle(ClickAreaContext context); + + @ApiStatus.NonExtendable + interface ClickAreaContext { + T getScreen(); + + Point getMousePosition(); + } + + @ApiStatus.NonExtendable + interface Result { + static Result success() { + return Internals.createClickAreaHandlerResult(true); + } + + static Result fail() { + return Internals.createClickAreaHandlerResult(false); + } + + Result category(ResourceLocation category); + + default Result categories(Iterable categories) { + for (ResourceLocation category : categories) { + category(category); + } + return this; + } + + boolean isSuccessful(); + + Stream getCategories(); + } +} diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java index d889beacd..d46d94415 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java @@ -36,6 +36,7 @@ import org.jetbrains.annotations.ApiStatus; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; +import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; @@ -302,17 +303,33 @@ public interface RecipeHelper { */ void registerClickArea(ScreenClickAreaProvider rectangleSupplier, Class screenClass, ResourceLocation... categories); + /** + * Registers a click area handler for a screen. A handler allows more specific implementation of click areas. + * + * @param screenClass The class of the screen. + * @param handler The click area handler that is offset to the window's top left corner. + * @param The screen type to be registered to. + * @see #registerClickArea(ScreenClickAreaProvider, Class, ResourceLocation...) for a simpler way to handle areas without custom categories. + */ + void registerClickArea(Class screenClass, ClickAreaHandler handler); + > void registerRecipes(ResourceLocation category, Class recipeClass, Function mappingFunction); > void registerRecipes(ResourceLocation category, Function recipeFilter, Function mappingFunction); @ApiStatus.Internal - List getScreenClickAreas(); + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "6.0") + default List getScreenClickAreas() { + return Collections.emptyList(); + } @ApiStatus.Internal boolean arePluginsLoading(); @ApiStatus.Internal + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "6.0") interface ScreenClickArea { Class getScreenClass(); diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ScreenClickAreaProvider.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ScreenClickAreaProvider.java index 8f9989b9e..5f13a3cdc 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ScreenClickAreaProvider.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ScreenClickAreaProvider.java @@ -25,10 +25,22 @@ package me.shedaniel.rei.api; import me.shedaniel.math.Rectangle; import net.minecraft.client.gui.screen.Screen; +import net.minecraft.util.ResourceLocation; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; +import java.util.function.Supplier; + @FunctionalInterface public interface ScreenClickAreaProvider { @NotNull Rectangle provide(@NotNull T screen); + + default ClickAreaHandler toHandler(Supplier categories) { + return context -> { + return provide(context.getScreen()).contains(context.getMousePosition()) + ? ClickAreaHandler.Result.success().categories(Arrays.asList(categories.get())) + : ClickAreaHandler.Result.fail(); + }; + } } diff --git a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/impl/Internals.java b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/impl/Internals.java index b87d5e767..379c33397 100644 --- a/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/impl/Internals.java +++ b/RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/impl/Internals.java @@ -44,6 +44,7 @@ import org.jetbrains.annotations.Nullable; import java.lang.reflect.Field; import java.util.Collection; import java.util.function.BiFunction; +import java.util.function.Function; import java.util.function.Supplier; @ApiStatus.Internal @@ -59,6 +60,7 @@ public final class Internals { private static Supplier displayHelper = Internals::throwNotSetup; private static Supplier widgetsProvider = Internals::throwNotSetup; private static Supplier viewSearchBuilder = Internals::throwNotSetup; + private static Function<@NotNull Boolean, ClickAreaHandler.Result> clickAreaHandlerResult = (result) -> throwNotSetup(); private static BiFunction<@Nullable Point, Collection, Tooltip> tooltipProvider = (point, texts) -> throwNotSetup(); private static Supplier builtinPlugin = Internals::throwNotSetup; @@ -122,6 +124,11 @@ public final class Internals { return viewSearchBuilder.get(); } + @NotNull + public static ClickAreaHandler.Result createClickAreaHandlerResult(boolean applicable) { + return clickAreaHandlerResult.apply(applicable); + } + @NotNull public static Tooltip createTooltip(@Nullable Point point, Collection texts) { return tooltipProvider.apply(point, texts); -- cgit