aboutsummaryrefslogtreecommitdiff
path: root/RoughlyEnoughItems-api/src/main/java
diff options
context:
space:
mode:
authorshedaniel <daniel@shedaniel.me>2020-10-01 00:27:21 +0800
committershedaniel <daniel@shedaniel.me>2020-10-01 00:27:21 +0800
commit868f4d2970aa7bf6105c627393b8ea3baf7f029b (patch)
tree646bb61a4ac41d0d8b2870be48111892d01f56ae /RoughlyEnoughItems-api/src/main/java
parentb7778439101429fe432d42287707bc5d57dd3764 (diff)
downloadRoughlyEnoughItems-868f4d2970aa7bf6105c627393b8ea3baf7f029b.tar.gz
RoughlyEnoughItems-868f4d2970aa7bf6105c627393b8ea3baf7f029b.tar.bz2
RoughlyEnoughItems-868f4d2970aa7bf6105c627393b8ea3baf7f029b.zip
Allow a more specific click area handler.
Signed-off-by: shedaniel <daniel@shedaniel.me>
Diffstat (limited to 'RoughlyEnoughItems-api/src/main/java')
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/AutoTransferHandler.java2
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ClickAreaHandler.java68
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/RecipeHelper.java19
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/api/ScreenClickAreaProvider.java12
-rw-r--r--RoughlyEnoughItems-api/src/main/java/me/shedaniel/rei/impl/Internals.java7
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 809165a8f..34d6cc66d 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, AbstractContainerScreen<?> 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..40a0624b4
--- /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.screens.Screen;
+import net.minecraft.resources.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 2c1848f37..205608e14 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 Recipe<?>> void registerRecipes(ResourceLocation category, Class<T> recipeClass, Function<T, RecipeDisplay> mappingFunction);
<T extends Recipe<?>> void registerRecipes(ResourceLocation category, Function<Recipe, 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 3e57fb5cc..3ced900f3 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.screens.Screen;
+import net.minecraft.resources.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 4c1316023..d8fc9fc6d 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
@@ -45,6 +45,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
@@ -60,6 +61,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<Component>, Tooltip> tooltipProvider = (point, texts) -> throwNotSetup();
private static Supplier<BuiltinPlugin> builtinPlugin = Internals::throwNotSetup;
@@ -124,6 +126,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<Component> texts) {
return tooltipProvider.apply(point, texts);
}