| 1 | package net.minecraft.world.item.crafting;
|
| 2 |
|
| 3 | import java.util.ArrayList;
|
| 4 | import java.util.List;
|
| 5 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 6 | import net.minecraft.world.item.ItemStack;
|
| 7 |
|
| 8 | public class CraftingInput implements RecipeInput {
|
| 9 | public static final CraftingInput EMPTY = new CraftingInput(0, 0, List.of());
|
| 10 | private final int width;
|
| 11 | private final int height;
|
| 12 | private final List<ItemStack> items;
|
| 13 | private final StackedItemContents stackedContents = new StackedItemContents();
|
| 14 | private final int ingredientCount;
|
| 15 |
|
| 16 | private CraftingInput(final int width, final int height, final List<ItemStack> items) {
|
| 17 | this.width = width;
|
| 18 | this.height = height;
|
| 19 | this.items = items;
|
| 20 | int ingredientCount = 0;
|
| 21 |
|
| 22 | for (ItemStack item : items) {
|
| 23 | if (!item.isEmpty()) {
|
| 24 | ingredientCount++;
|
| 25 | this.stackedContents.accountStack(item, 1);
|
| 26 | }
|
| 27 | }
|
| 28 |
|
| 29 | this.ingredientCount = ingredientCount;
|
| 30 | }
|
| 31 |
|
| 32 | public static CraftingInput of(final int width, final int height, final List<ItemStack> items) {
|
| 33 | return ofPositioned(width, height, items).input();
|
| 34 | }
|
| 35 |
|
| 36 | public static CraftingInput.Positioned ofPositioned(final int width, final int height, final List<ItemStack> items) {
|
| 37 | if (width != 0 && height != 0) {
|
| 38 | int left = width - 1;
|
| 39 | int right = 0;
|
| 40 | int top = height - 1;
|
| 41 | int bottom = 0;
|
| 42 |
|
| 43 | for (int y = 0; y < height; y++) {
|
| 44 | boolean rowEmpty = true;
|
| 45 |
|
| 46 | for (int x = 0; x < width; x++) {
|
| 47 | ItemStack item = items.get(x + y * width);
|
| 48 | if (!item.isEmpty()) {
|
| 49 | left = Math.min(left, x);
|
| 50 | right = Math.max(right, x);
|
| 51 | rowEmpty = false;
|
| 52 | }
|
| 53 | }
|
| 54 |
|
| 55 | if (!rowEmpty) {
|
| 56 | top = Math.min(top, y);
|
| 57 | bottom = Math.max(bottom, y);
|
| 58 | }
|
| 59 | }
|
| 60 |
|
| 61 | int newWidth = right - left + 1;
|
| 62 | int newHeight = bottom - top + 1;
|
| 63 | if (newWidth <= 0 || newHeight <= 0) {
|
| 64 | return CraftingInput.Positioned.EMPTY;
|
| 65 | }
|
| 66 |
|
| 67 | if (newWidth == width && newHeight == height) {
|
| 68 | return new CraftingInput.Positioned(new CraftingInput(width, height, items), left, top);
|
| 69 | }
|
| 70 |
|
| 71 | List<ItemStack> newItems = new ArrayList<>(newWidth * newHeight);
|
| 72 |
|
| 73 | for (int y = 0; y < newHeight; y++) {
|
| 74 | for (int x = 0; x < newWidth; x++) {
|
| 75 | int index = x + left + (y + top) * width;
|
| 76 | newItems.add(items.get(index));
|
| 77 | }
|
| 78 | }
|
| 79 |
|
| 80 | return new CraftingInput.Positioned(new CraftingInput(newWidth, newHeight, newItems), left, top);
|
| 81 | } else {
|
| 82 | return CraftingInput.Positioned.EMPTY;
|
| 83 | }
|
| 84 | }
|
| 85 |
|
| 86 | @Override
|
| 87 | public ItemStack getItem(final int index) {
|
| 88 | return this.items.get(index);
|
| 89 | }
|
| 90 |
|
| 91 | public ItemStack getItem(final int x, final int y) {
|
| 92 | return this.items.get(x + y * this.width);
|
| 93 | }
|
| 94 |
|
| 95 | @Override
|
| 96 | public int size() {
|
| 97 | return this.items.size();
|
| 98 | }
|
| 99 |
|
| 100 | @Override
|
| 101 | public boolean isEmpty() {
|
| 102 | return this.ingredientCount == 0;
|
| 103 | }
|
| 104 |
|
| 105 | public StackedItemContents stackedContents() {
|
| 106 | return this.stackedContents;
|
| 107 | }
|
| 108 |
|
| 109 | public List<ItemStack> items() {
|
| 110 | return this.items;
|
| 111 | }
|
| 112 |
|
| 113 | public int ingredientCount() {
|
| 114 | return this.ingredientCount;
|
| 115 | }
|
| 116 |
|
| 117 | public int width() {
|
| 118 | return this.width;
|
| 119 | }
|
| 120 |
|
| 121 | public int height() {
|
| 122 | return this.height;
|
| 123 | }
|
| 124 |
|
| 125 | @Override
|
| 126 | public boolean equals(final Object obj) {
|
| 127 | if (obj == this) {
|
| 128 | return true;
|
| 129 | } else {
|
| 130 | return !(obj instanceof CraftingInput input)
|
| 131 | ? false
|
| 132 | : this.width == input.width
|
| 133 | && this.height == input.height
|
| 134 | && this.ingredientCount == input.ingredientCount
|
| 135 | && ItemStack.listMatches(this.items, input.items);
|
| 136 | }
|
| 137 | }
|
| 138 |
|
| 139 | @Override
|
| 140 | public int hashCode() {
|
| 141 | int result = ItemStack.hashStackList(this.items);
|
| 142 | result = 31 * result + this.width;
|
| 143 | return 31 * result + this.height;
|
| 144 | }
|
| 145 |
|
| 146 | public record Positioned(CraftingInput input, int left, int top) {
|
| 147 | public static final CraftingInput.Positioned EMPTY = new CraftingInput.Positioned(CraftingInput.EMPTY, 0, 0);
|
| 148 | }
|
| 149 | }
|
| 150 |
|