| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.ints.IntArrayList;
|
| 4 | import it.unimi.dsi.fastutil.ints.IntList;
|
| 5 | import java.util.ArrayList;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Optional;
|
| 8 |
|
| 9 | public class PlacementInfo {
|
| 10 | public static final int EMPTY_SLOT = -1;
|
| 11 | public static final PlacementInfo NOT_PLACEABLE = new PlacementInfo(List.of(), IntList.of());
|
| 12 | private final List<Ingredient> ingredients;
|
| 13 | private final IntList slotsToIngredientIndex;
|
| 14 |
|
| 15 | private PlacementInfo(final List<Ingredient> ingredients, final IntList slotsToIngredientIndex) {
|
| 16 | this.ingredients = ingredients;
|
| 17 | this.slotsToIngredientIndex = slotsToIngredientIndex;
|
| 18 | }
|
| 19 |
|
| 20 | public static PlacementInfo create(final Ingredient ingredient) {
|
| 21 | return ingredient.isEmpty() ? NOT_PLACEABLE : new PlacementInfo(List.of(ingredient), IntList.of(0));
|
| 22 | }
|
| 23 |
|
| 24 | public static PlacementInfo createFromOptionals(final List<Optional<Ingredient>> ingredients) {
|
| 25 | int ingredientCount = ingredients.size();
|
| 26 | List<Ingredient> presentIngredients = new ArrayList<>(ingredientCount);
|
| 27 | IntList slotsToIngredientIndex = new IntArrayList(ingredientCount);
|
| 28 | int placementIndex = 0;
|
| 29 |
|
| 30 | for (Optional<Ingredient> maybeIngredient : ingredients) {
|
| 31 | if (maybeIngredient.isPresent()) {
|
| 32 | Ingredient ingredient = maybeIngredient.get();
|
| 33 | if (ingredient.isEmpty()) {
|
| 34 | return NOT_PLACEABLE;
|
| 35 | }
|
| 36 |
|
| 37 | presentIngredients.add(ingredient);
|
| 38 | slotsToIngredientIndex.add(placementIndex++);
|
| 39 | } else {
|
| 40 | slotsToIngredientIndex.add(-1);
|
| 41 | }
|
| 42 | }
|
| 43 |
|
| 44 | return new PlacementInfo(presentIngredients, slotsToIngredientIndex);
|
| 45 | }
|
| 46 |
|
| 47 | public static PlacementInfo create(final List<Ingredient> ingredients) {
|
| 48 | int ingredientCount = ingredients.size();
|
| 49 | IntList slotsToIngredientIndex = new IntArrayList(ingredientCount);
|
| 50 |
|
| 51 | for (int i = 0; i < ingredientCount; i++) {
|
| 52 | Ingredient ingredient = ingredients.get(i);
|
| 53 | if (ingredient.isEmpty()) {
|
| 54 | return NOT_PLACEABLE;
|
| 55 | }
|
| 56 |
|
| 57 | slotsToIngredientIndex.add(i);
|
| 58 | }
|
| 59 |
|
| 60 | return new PlacementInfo(ingredients, slotsToIngredientIndex);
|
| 61 | }
|
| 62 |
|
| 63 | public IntList slotsToIngredientIndex() {
|
| 64 | return this.slotsToIngredientIndex;
|
| 65 | }
|
| 66 |
|
| 67 | public List<Ingredient> ingredients() {
|
| 68 | return this.ingredients;
|
| 69 | }
|
| 70 |
|
| 71 | public boolean isImpossibleToPlace() {
|
| 72 | return this.slotsToIngredientIndex.isEmpty();
|
| 73 | }
|
| 74 | }
|
| 75 |
|