diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-01-28 02:32:10 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-01-28 02:32:10 +0800 |
| commit | d21c7a39267c2bdc78cba0cde6da926830006552 (patch) | |
| tree | b4afd145c1522653c00139723a4c56a87ab638e8 /default-plugin/src/main/java/me/shedaniel/rei/plugin/common | |
| parent | c6b9a9ac32da50f9af309c553e3c5594d5ec0cbf (diff) | |
| download | RoughlyEnoughItems-d21c7a39267c2bdc78cba0cde6da926830006552.tar.gz RoughlyEnoughItems-d21c7a39267c2bdc78cba0cde6da926830006552.tar.bz2 RoughlyEnoughItems-d21c7a39267c2bdc78cba0cde6da926830006552.zip | |
Close #720
Diffstat (limited to 'default-plugin/src/main/java/me/shedaniel/rei/plugin/common')
2 files changed, 110 insertions, 15 deletions
diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/CraftingRecipeSizeProvider.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/CraftingRecipeSizeProvider.java new file mode 100644 index 000000000..cad260049 --- /dev/null +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/CraftingRecipeSizeProvider.java @@ -0,0 +1,76 @@ +/* + * This file is licensed under the MIT License, part of Roughly Enough Items. + * Copyright (c) 2018, 2019, 2020, 2021, 2022 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.plugin.common.displays.crafting; + +import net.minecraft.world.item.crafting.Recipe; +import org.jetbrains.annotations.ApiStatus; +import org.jetbrains.annotations.Nullable; + +import java.util.Objects; + +@ApiStatus.Experimental +@FunctionalInterface +public interface CraftingRecipeSizeProvider<R extends Recipe<?>> { + final class Size { + private final int width; + private final int height; + + public Size(int width, int height) { + this.width = width; + this.height = height; + } + + public int getWidth() { + return width; + } + + public int getHeight() { + return height; + } + + @Override + public boolean equals(Object obj) { + if (obj == this) return true; + if (obj == null || obj.getClass() != this.getClass()) return false; + var that = (Size) obj; + return this.width == that.width && + this.height == that.height; + } + + @Override + public int hashCode() { + return Objects.hash(width, height); + } + + @Override + public String toString() { + return "Size[" + + "width=" + width + ", " + + "height=" + height + ']'; + } + } + + @Nullable + Size getSize(R recipe); +} diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java index a921b82b1..d405d9f5c 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/common/displays/crafting/DefaultCraftingDisplay.java @@ -24,8 +24,6 @@ package me.shedaniel.rei.plugin.common.displays.crafting; import dev.architectury.injectables.annotations.ExpectPlatform; -import dev.architectury.injectables.annotations.PlatformOnly; -import dev.architectury.platform.Platform; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.SimpleGridMenuDisplay; import me.shedaniel.rei.api.common.display.basic.BasicDisplay; @@ -47,7 +45,6 @@ import net.minecraft.world.item.crafting.Ingredient; import net.minecraft.world.item.crafting.Recipe; import net.minecraft.world.item.crafting.ShapedRecipe; import net.minecraft.world.item.crafting.ShapelessRecipe; -import org.apache.commons.lang3.tuple.Pair; import org.jetbrains.annotations.Nullable; import java.util.ArrayList; @@ -67,6 +64,24 @@ public abstract class DefaultCraftingDisplay<C extends Recipe<?>> extends BasicD this.recipe = recipe; } + private static final List<CraftingRecipeSizeProvider<?>> SIZE_PROVIDER = new ArrayList<>(); + + static { + registerPlatformSizeProvider(); + } + + /** + * Registers a size provider for crafting recipes. + * This is not reloadable, please statically register your provider, and + * do not repeatedly register it. + * + * @param sizeProvider the provider to register + * @param <R> the recipe type + */ + public static <R extends Recipe<?>> void registerSizeProvider(CraftingRecipeSizeProvider<R> sizeProvider) { + SIZE_PROVIDER.add(0, sizeProvider); + } + @Nullable public static DefaultCraftingDisplay<?> of(Recipe<?> recipe) { if (recipe instanceof ShapelessRecipe) { @@ -75,26 +90,25 @@ public abstract class DefaultCraftingDisplay<C extends Recipe<?>> extends BasicD return new DefaultShapedDisplay((ShapedRecipe) recipe); } else if (!recipe.isSpecial()) { NonNullList<Ingredient> ingredients = recipe.getIngredients(); - Pair<Integer, Integer> size = Platform.isFabric() ? null : getSize(recipe); - - if (!ingredients.isEmpty()) { - if (size == null) { - return new DefaultCustomDisplay(recipe, EntryIngredients.ofIngredients(recipe.getIngredients()), - Collections.singletonList(EntryIngredients.of(recipe.getResultItem()))); - } else { + for (CraftingRecipeSizeProvider<?> pair : SIZE_PROVIDER) { + CraftingRecipeSizeProvider.Size size = ((CraftingRecipeSizeProvider<Recipe<?>>) pair).getSize(recipe); + + if (size != null) { return new DefaultCustomShapedDisplay(recipe, EntryIngredients.ofIngredients(recipe.getIngredients()), Collections.singletonList(EntryIngredients.of(recipe.getResultItem())), - size.getLeft(), size.getRight()); + size.getWidth(), size.getHeight()); } } + + return new DefaultCustomDisplay(recipe, EntryIngredients.ofIngredients(recipe.getIngredients()), + Collections.singletonList(EntryIngredients.of(recipe.getResultItem()))); } return null; } @ExpectPlatform - @PlatformOnly(PlatformOnly.FORGE) - private static Pair<Integer, Integer> getSize(Recipe<?> recipe) { + private static void registerPlatformSizeProvider() { throw new AssertionError(); } @@ -146,8 +160,6 @@ public abstract class DefaultCraftingDisplay<C extends Recipe<?>> extends BasicD @Override public List<InputIngredient<EntryStack<?>>> getInputIngredients(MenuSerializationContext<?, ?, ?> context, MenuInfo<?, ?> info, boolean fill) { - int inputWidth = Math.max(3, getInputWidth()); - int inputHeight = Math.max(3, getInputHeight()); int craftingWidth = 3, craftingHeight = 3; if (info instanceof SimpleGridMenuInfo && fill) { @@ -155,6 +167,13 @@ public abstract class DefaultCraftingDisplay<C extends Recipe<?>> extends BasicD craftingHeight = ((SimpleGridMenuInfo<AbstractContainerMenu, ?>) info).getCraftingHeight(context.getMenu()); } + return getInputIngredients(craftingWidth, craftingHeight); + } + + public List<InputIngredient<EntryStack<?>>> getInputIngredients(int craftingWidth, int craftingHeight) { + int inputWidth = Math.max(3, getInputWidth()); + int inputHeight = Math.max(3, getInputHeight()); + InputIngredient<EntryStack<?>>[][] grid = new InputIngredient[Math.max(inputWidth, craftingWidth)][Math.max(inputHeight, craftingHeight)]; List<EntryIngredient> inputEntries = getInputEntries(); |
