| 1 | package net.minecraft.recipebook;
|
| 2 |
|
| 3 | import java.util.Iterator;
|
| 4 | import net.minecraft.util.Mth;
|
| 5 | import net.minecraft.world.item.crafting.Recipe;
|
| 6 | import net.minecraft.world.item.crafting.ShapedRecipe;
|
| 7 |
|
| 8 | public interface PlaceRecipeHelper {
|
| 9 | static <T> void placeRecipe(
|
| 10 | final int gridWidth, final int gridHeight, final Recipe<?> recipe, final Iterable<T> entries, final PlaceRecipeHelper.Output<T> output
|
| 11 | ) {
|
| 12 | if (recipe instanceof ShapedRecipe shapedRecipe) {
|
| 13 | placeRecipe(gridWidth, gridHeight, shapedRecipe.getWidth(), shapedRecipe.getHeight(), entries, output);
|
| 14 | } else {
|
| 15 | placeRecipe(gridWidth, gridHeight, gridWidth, gridHeight, entries, output);
|
| 16 | }
|
| 17 | }
|
| 18 |
|
| 19 | static <T> void placeRecipe(
|
| 20 | final int gridWidth, final int gridHeight, final int recipeWidth, final int recipeHeight, final Iterable<T> entries, final PlaceRecipeHelper.Output<T> output
|
| 21 | ) {
|
| 22 | Iterator<T> iterator = entries.iterator();
|
| 23 | int gridIndex = 0;
|
| 24 |
|
| 25 | for (int gridYPos = 0; gridYPos < gridHeight; gridYPos++) {
|
| 26 | boolean shouldCenterRecipe = recipeHeight < gridHeight / 2.0F;
|
| 27 | int startPosCenterRecipe = Mth.floor(gridHeight / 2.0F - recipeHeight / 2.0F);
|
| 28 | if (shouldCenterRecipe && startPosCenterRecipe > gridYPos) {
|
| 29 | gridIndex += gridWidth;
|
| 30 | gridYPos++;
|
| 31 | }
|
| 32 |
|
| 33 | for (int gridXPos = 0; gridXPos < gridWidth; gridXPos++) {
|
| 34 | if (!iterator.hasNext()) {
|
| 35 | return;
|
| 36 | }
|
| 37 |
|
| 38 | shouldCenterRecipe = recipeWidth < gridWidth / 2.0F;
|
| 39 | startPosCenterRecipe = Mth.floor(gridWidth / 2.0F - recipeWidth / 2.0F);
|
| 40 | int totalRecipeWidthInGrid = recipeWidth;
|
| 41 | boolean addIngredientToSlot = gridXPos < recipeWidth;
|
| 42 | if (shouldCenterRecipe) {
|
| 43 | totalRecipeWidthInGrid = startPosCenterRecipe + recipeWidth;
|
| 44 | addIngredientToSlot = startPosCenterRecipe <= gridXPos && gridXPos < startPosCenterRecipe + recipeWidth;
|
| 45 | }
|
| 46 |
|
| 47 | if (addIngredientToSlot) {
|
| 48 | output.addItemToSlot(iterator.next(), gridIndex, gridXPos, gridYPos);
|
| 49 | } else if (totalRecipeWidthInGrid == gridXPos) {
|
| 50 | gridIndex += gridWidth - gridXPos;
|
| 51 | break;
|
| 52 | }
|
| 53 |
|
| 54 | gridIndex++;
|
| 55 | }
|
| 56 | }
|
| 57 | }
|
| 58 |
|
| 59 | @FunctionalInterface
|
| 60 | interface Output<T> {
|
| 61 | void addItemToSlot(T item, int gridIndex, int gridXPos, int gridYPos);
|
| 62 | }
|
| 63 | }
|
| 64 |
|