diff options
| author | shedaniel <daniel@shedaniel.me> | 2020-10-01 00:27:21 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2020-10-01 00:38:34 +0800 |
| commit | e17e12e3e83b1789bb081b268195212e3d702c65 (patch) | |
| tree | cdd003ff0880120d90e2ba674977c489bad3c73b /RoughlyEnoughItems-api | |
| parent | 1b17e32f9704e9e9a112ef49886054a1f30ef2aa (diff) | |
| download | RoughlyEnoughItems-e17e12e3e83b1789bb081b268195212e3d702c65.tar.gz RoughlyEnoughItems-e17e12e3e83b1789bb081b268195212e3d702c65.tar.bz2 RoughlyEnoughItems-e17e12e3e83b1789bb081b268195212e3d702c65.zip | |
Allow a more specific click area handler.
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'RoughlyEnoughItems-api')
5 files changed, 107 insertions, 1 deletions
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<T extends Screen> { + Result handle(ClickAreaContext<T> context); + + @ApiStatus.NonExtendable + interface ClickAreaContext<T extends Screen> { + 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<ResourceLocation> categories) { + for (ResourceLocation category : categories) { + category(category); + } + return this; + } + + boolean isSuccessful(); + + Stream<ResourceLocation> 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 { */ <T extends Screen> void registerClickArea(ScreenClickAreaProvider<T> rectangleSupplier, Class<T> 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 <T> The screen type to be registered to. + * @see #registerClickArea(ScreenClickAreaProvider, Class, ResourceLocation...) for a simpler way to handle areas without custom categories. + */ + <T extends Screen> void registerClickArea(Class<T> screenClass, ClickAreaHandler<T> handler); + <T extends IRecipe<?>> void registerRecipes(ResourceLocation category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction); <T extends IRecipe<?>> void registerRecipes(ResourceLocation category, Function<IRecipe, Boolean> recipeFilter, Function<T, RecipeDisplay> mappingFunction); @ApiStatus.Internal - List<RecipeHelper.ScreenClickArea> getScreenClickAreas(); + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "6.0") + default List<RecipeHelper.ScreenClickArea> getScreenClickAreas() { + return Collections.emptyList(); + } @ApiStatus.Internal boolean arePluginsLoading(); @ApiStatus.Internal + @Deprecated + @ApiStatus.ScheduledForRemoval(inVersion = "6.0") interface ScreenClickArea { Class<? extends Screen> 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<T extends Screen> { @NotNull Rectangle provide(@NotNull T screen); + + default ClickAreaHandler<T> toHandler(Supplier<ResourceLocation[]> 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> displayHelper = Internals::throwNotSetup; private static Supplier<WidgetsProvider> widgetsProvider = Internals::throwNotSetup; private static Supplier<ClientHelper.ViewSearchBuilder> viewSearchBuilder = Internals::throwNotSetup; + private static Function<@NotNull Boolean, ClickAreaHandler.Result> clickAreaHandlerResult = (result) -> throwNotSetup(); private static BiFunction<@Nullable Point, Collection<ITextComponent>, Tooltip> tooltipProvider = (point, texts) -> throwNotSetup(); private static Supplier<BuiltinPlugin> builtinPlugin = Internals::throwNotSetup; @@ -123,6 +125,11 @@ public final class Internals { } @NotNull + public static ClickAreaHandler.Result createClickAreaHandlerResult(boolean applicable) { + return clickAreaHandlerResult.apply(applicable); + } + + @NotNull public static Tooltip createTooltip(@Nullable Point point, Collection<ITextComponent> texts) { return tooltipProvider.apply(point, texts); } |
