From 07e0f52ff344e6d58ed6ebf870096738ea24f816 Mon Sep 17 00:00:00 2001 From: mezz Date: Thu, 19 Sep 2024 09:18:33 +0900 Subject: [PATCH] Add a method for creating clickable ingredients with the ingredient manager --- .../jei/api/runtime/IClickableIngredient.java | 2 ++ .../jei/api/runtime/IIngredientManager.java | 17 +++++++++++++++++ .../library/ingredients/IngredientManager.java | 16 ++++++++++++++++ gradle.properties | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) diff --git a/CommonApi/src/main/java/mezz/jei/api/runtime/IClickableIngredient.java b/CommonApi/src/main/java/mezz/jei/api/runtime/IClickableIngredient.java index c60b9c590..60443ed31 100644 --- a/CommonApi/src/main/java/mezz/jei/api/runtime/IClickableIngredient.java +++ b/CommonApi/src/main/java/mezz/jei/api/runtime/IClickableIngredient.java @@ -10,6 +10,8 @@ * This can be an ingredient drawn in a GUI container slot, a fluid tank, * or anything else that holds ingredients. * + * Create one with {@link IIngredientManager#createClickableIngredient}. + * * @since 11.5.0 */ public interface IClickableIngredient { diff --git a/CommonApi/src/main/java/mezz/jei/api/runtime/IIngredientManager.java b/CommonApi/src/main/java/mezz/jei/api/runtime/IIngredientManager.java index 7ea46901b..7830149ba 100644 --- a/CommonApi/src/main/java/mezz/jei/api/runtime/IIngredientManager.java +++ b/CommonApi/src/main/java/mezz/jei/api/runtime/IIngredientManager.java @@ -11,6 +11,7 @@ import mezz.jei.api.ingredients.subtypes.UidContext; import mezz.jei.api.registration.IExtraIngredientRegistration; import mezz.jei.api.registration.IIngredientAliasRegistration; +import net.minecraft.client.renderer.Rect2i; import net.minecraft.world.item.ItemStack; import org.jetbrains.annotations.Unmodifiable; @@ -151,6 +152,22 @@ default Optional> createTypedIngredient(V ingredient) { */ ITypedIngredient normalizeTypedIngredient(ITypedIngredient typedIngredient); + /** + * Create a clickable ingredient. + * + * @see IClickableIngredient + * + * @param ingredientType the type of the ingredient being clicked + * @param ingredient the ingredient being clicked + * @param area the area that this clickable ingredient is drawn in, in absolute screen coordinates. + * @param normalize set true to normalize the ingredient (see {@link IIngredientHelper#normalizeIngredient} + * + * @return a clickable ingredient, or {@link Optional#empty()} if the ingredient is invalid (see {@link IIngredientHelper#isValidIngredient} + * + * @since 15.19.2 + */ + Optional> createClickableIngredient(IIngredientType ingredientType, V ingredient, Rect2i area, boolean normalize); + /** * Get an ingredient by the given unique id. * This uses the uids from {@link IIngredientHelper#getUniqueId(Object, UidContext)} diff --git a/Library/src/main/java/mezz/jei/library/ingredients/IngredientManager.java b/Library/src/main/java/mezz/jei/library/ingredients/IngredientManager.java index 011f70813..fd7200cf2 100644 --- a/Library/src/main/java/mezz/jei/library/ingredients/IngredientManager.java +++ b/Library/src/main/java/mezz/jei/library/ingredients/IngredientManager.java @@ -5,10 +5,14 @@ import mezz.jei.api.ingredients.IIngredientType; import mezz.jei.api.ingredients.IIngredientTypeWithSubtypes; import mezz.jei.api.ingredients.ITypedIngredient; +import mezz.jei.api.runtime.IClickableIngredient; import mezz.jei.api.runtime.IIngredientManager; +import mezz.jei.common.input.ClickableIngredient; import mezz.jei.common.util.ErrorUtil; +import mezz.jei.common.util.ImmutableRect2i; import mezz.jei.common.util.Translator; import mezz.jei.core.util.WeakList; +import net.minecraft.client.renderer.Rect2i; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.jetbrains.annotations.Unmodifiable; @@ -183,6 +187,18 @@ public ITypedIngredient normalizeTypedIngredient(ITypedIngredient type return TypedIngredient.normalize(typedIngredient, ingredientHelper); } + @Override + public Optional> createClickableIngredient(IIngredientType ingredientType, V ingredient, Rect2i area, boolean normalize) { + ErrorUtil.checkNotNull(ingredientType, "ingredientType"); + ErrorUtil.checkNotNull(ingredient, "ingredient"); + ErrorUtil.checkNotNull(area, "area"); + return TypedIngredient.createAndFilterInvalid(this, ingredientType, ingredient, normalize) + .map(typedIngredient -> { + ImmutableRect2i slotArea = new ImmutableRect2i(area); + return new ClickableIngredient<>(typedIngredient, slotArea); + }); + } + @Override @Deprecated public Optional getIngredientByUid(IIngredientType ingredientType, String ingredientUuid) { diff --git a/gradle.properties b/gradle.properties index 9f467da48..805414f13 100644 --- a/gradle.properties +++ b/gradle.properties @@ -57,4 +57,4 @@ modrinthId=u6dRKJwZ jUnitVersion=5.8.2 # Version -specificationVersion=15.19.1 +specificationVersion=15.19.2