diff options
| author | shedaniel <daniel@shedaniel.me> | 2022-01-28 02:32:10 +0800 |
|---|---|---|
| committer | shedaniel <daniel@shedaniel.me> | 2022-01-28 02:46:59 +0800 |
| commit | 07413ab5f5375e99b4d50b5927fdd7c8cbb54bca (patch) | |
| tree | f71582329d91c1635d8ca6ec0d8e433cd5e7cc1f /default-plugin/src/main/java/me | |
| parent | c35205ac0559d93eb92b80ad94fd98ee7087ecd2 (diff) | |
| download | RoughlyEnoughItems-07413ab5f5375e99b4d50b5927fdd7c8cbb54bca.tar.gz RoughlyEnoughItems-07413ab5f5375e99b4d50b5927fdd7c8cbb54bca.tar.bz2 RoughlyEnoughItems-07413ab5f5375e99b4d50b5927fdd7c8cbb54bca.zip | |
Close #720
Diffstat (limited to 'default-plugin/src/main/java/me')
3 files changed, 114 insertions, 23 deletions
diff --git a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java index 5e10ddd17..f4a4d0ca6 100644 --- a/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java +++ b/default-plugin/src/main/java/me/shedaniel/rei/plugin/client/categories/crafting/DefaultCraftingCategory.java @@ -34,10 +34,10 @@ import me.shedaniel.rei.api.client.registry.display.DisplayCategory; import me.shedaniel.rei.api.common.category.CategoryIdentifier; import me.shedaniel.rei.api.common.display.DisplayMerger; import me.shedaniel.rei.api.common.entry.EntryStack; +import me.shedaniel.rei.api.common.entry.InputIngredient; import me.shedaniel.rei.api.common.util.EntryStacks; import me.shedaniel.rei.plugin.common.BuiltinPlugin; import me.shedaniel.rei.plugin.common.displays.crafting.DefaultCraftingDisplay; -import me.shedaniel.rei.plugin.common.displays.crafting.DefaultShapedDisplay; import net.fabricmc.api.EnvType; import net.fabricmc.api.Environment; import net.minecraft.network.chat.Component; @@ -71,17 +71,13 @@ public class DefaultCraftingCategory implements DisplayCategory<DefaultCraftingD widgets.add(Widgets.createRecipeBase(bounds)); widgets.add(Widgets.createArrow(new Point(startPoint.x + 60, startPoint.y + 18))); widgets.add(Widgets.createResultSlotBackground(new Point(startPoint.x + 95, startPoint.y + 19))); - List<? extends List<? extends EntryStack<?>>> input = display.getInputEntries(); + List<InputIngredient<EntryStack<?>>> input = display.getInputIngredients(3, 3); List<Slot> slots = Lists.newArrayList(); for (int y = 0; y < 3; y++) for (int x = 0; x < 3; x++) slots.add(Widgets.createSlot(new Point(startPoint.x + 1 + x * 18, startPoint.y + 1 + y * 18)).markInput()); - for (int i = 0; i < input.size(); i++) { - if (display instanceof DefaultShapedDisplay) { - if (!input.get(i).isEmpty()) - slots.get(DefaultCraftingDisplay.getSlotWithSize(display, i, 3)).entries(input.get(i)); - } else if (!input.get(i).isEmpty()) - slots.get(i).entries(input.get(i)); + for (InputIngredient<EntryStack<?>> ingredient : input) { + slots.get(ingredient.getIndex()).entries(ingredient.get()); } widgets.addAll(slots); widgets.add(Widgets.createSlot(new Point(startPoint.x + 95, startPoint.y + 19)).entries(display.getOutputEntries().get(0)).disableBackground().markOutput()); 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..f93032b79 --- /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; + Size 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 fa38f183e..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 me.shedaniel.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(); |
