| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.recipebook.ServerPlaceRecipe;
|
| 5 | import net.minecraft.server.level.ServerLevel;
|
| 6 | import net.minecraft.world.entity.player.Inventory;
|
| 7 | import net.minecraft.world.entity.player.Player;
|
| 8 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 9 | import net.minecraft.world.item.crafting.CraftingRecipe;
|
| 10 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 11 |
|
| 12 | public abstract class AbstractCraftingMenu extends RecipeBookMenu {
|
| 13 | private final int width;
|
| 14 | private final int height;
|
| 15 | protected final CraftingContainer craftSlots;
|
| 16 | protected final ResultContainer resultSlots = new ResultContainer();
|
| 17 |
|
| 18 | public AbstractCraftingMenu(final MenuType<?> menuType, final int containerId, final int width, final int height) {
|
| 19 | super(menuType, containerId);
|
| 20 | this.width = width;
|
| 21 | this.height = height;
|
| 22 | this.craftSlots = new TransientCraftingContainer(this, width, height);
|
| 23 | }
|
| 24 |
|
| 25 | protected Slot addResultSlot(final Player player, final int x, final int y) {
|
| 26 | return this.addSlot(new ResultSlot(player, this.craftSlots, this.resultSlots, 0, x, y));
|
| 27 | }
|
| 28 |
|
| 29 | protected void addCraftingGridSlots(final int left, final int top) {
|
| 30 | for (int y = 0; y < this.width; y++) {
|
| 31 | for (int x = 0; x < this.height; x++) {
|
| 32 | this.addSlot(new Slot(this.craftSlots, x + y * this.width, left + x * 18, top + y * 18));
|
| 33 | }
|
| 34 | }
|
| 35 | }
|
| 36 |
|
| 37 | @Override
|
| 38 | public RecipeBookMenu.PostPlaceAction handlePlacement(
|
| 39 | final boolean useMaxItems, final boolean allowDroppingItemsToClear, final RecipeHolder<?> recipe, final ServerLevel level, final Inventory inventory
|
| 40 | ) {
|
| 41 | RecipeHolder<CraftingRecipe> typedRecipe = (RecipeHolder<CraftingRecipe>)recipe;
|
| 42 | this.beginPlacingRecipe();
|
| 43 |
|
| 44 | try {
|
| 45 | List<Slot> inputSlots = this.getInputGridSlots();
|
| 46 | return ServerPlaceRecipe.placeRecipe(new ServerPlaceRecipe.CraftingMenuAccess<CraftingRecipe>() {
|
| 47 | @Override
|
| 48 | public void fillCraftSlotsStackedContents(final StackedItemContents stackedContents) {
|
| 49 | AbstractCraftingMenu.this.fillCraftSlotsStackedContents(stackedContents);
|
| 50 | }
|
| 51 |
|
| 52 | @Override
|
| 53 | public void clearCraftingContent() {
|
| 54 | AbstractCraftingMenu.this.resultSlots.clearContent();
|
| 55 | AbstractCraftingMenu.this.craftSlots.clearContent();
|
| 56 | }
|
| 57 |
|
| 58 | @Override
|
| 59 | public boolean recipeMatches(final RecipeHolder<CraftingRecipe> recipe) {
|
| 60 | return recipe.value().matches(AbstractCraftingMenu.this.craftSlots.asCraftInput(), AbstractCraftingMenu.this.owner().level());
|
| 61 | }
|
| 62 | }, this.width, this.height, inputSlots, inputSlots, inventory, typedRecipe, useMaxItems, allowDroppingItemsToClear);
|
| 63 | } finally {
|
| 64 | this.finishPlacingRecipe(level, typedRecipe);
|
| 65 | }
|
| 66 | }
|
| 67 |
|
| 68 | protected void beginPlacingRecipe() {
|
| 69 | }
|
| 70 |
|
| 71 | protected void finishPlacingRecipe(final ServerLevel level, final RecipeHolder<CraftingRecipe> recipe) {
|
| 72 | }
|
| 73 |
|
| 74 | public abstract Slot getResultSlot();
|
| 75 |
|
| 76 | public abstract List<Slot> getInputGridSlots();
|
| 77 |
|
| 78 | public int getGridWidth() {
|
| 79 | return this.width;
|
| 80 | }
|
| 81 |
|
| 82 | public int getGridHeight() {
|
| 83 | return this.height;
|
| 84 | }
|
| 85 |
|
| 86 | protected abstract Player owner();
|
| 87 |
|
| 88 | @Override
|
| 89 | public void fillCraftSlotsStackedContents(final StackedItemContents stackedContents) {
|
| 90 | this.craftSlots.fillStackedContents(stackedContents);
|
| 91 | }
|
| 92 | }
|
| 93 |
|