| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import net.minecraft.advancements.triggers.CriteriaTriggers;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.resources.Identifier;
|
| 6 | import net.minecraft.server.level.ServerPlayer;
|
| 7 | import net.minecraft.world.Container;
|
| 8 | import net.minecraft.world.SimpleContainer;
|
| 9 | import net.minecraft.world.entity.player.Inventory;
|
| 10 | import net.minecraft.world.entity.player.Player;
|
| 11 | import net.minecraft.world.item.ItemStack;
|
| 12 | import net.minecraft.world.item.alchemy.PotionContents;
|
| 13 | import net.minecraft.world.item.crafting.PotionIngredient;
|
| 14 | import net.minecraft.world.item.crafting.RecipeAccess;
|
| 15 | import net.minecraft.world.item.crafting.RecipePropertySet;
|
| 16 |
|
| 17 | public class BrewingStandMenu extends AbstractContainerMenu {
|
| 18 | private static final Identifier EMPTY_SLOT_FUEL = Identifier.withDefaultNamespace("container/slot/brewing_fuel");
|
| 19 | private static final Identifier EMPTY_SLOT_POTION = Identifier.withDefaultNamespace("container/slot/potion");
|
| 20 | private static final int BOTTLE_SLOT_START = 0;
|
| 21 | private static final int BOTTLE_SLOT_END = 2;
|
| 22 | private static final int INGREDIENT_SLOT = 3;
|
| 23 | private static final int FUEL_SLOT = 4;
|
| 24 | private static final int SLOT_COUNT = 5;
|
| 25 | private static final int DATA_COUNT = 4;
|
| 26 | private static final int INV_SLOT_START = 5;
|
| 27 | private static final int INV_SLOT_END = 32;
|
| 28 | private static final int USE_ROW_SLOT_START = 32;
|
| 29 | private static final int USE_ROW_SLOT_END = 41;
|
| 30 | private final Container brewingStand;
|
| 31 | private final ContainerData brewingStandData;
|
| 32 | private final Slot ingredientSlot;
|
| 33 |
|
| 34 | public BrewingStandMenu(final int containerId, final Inventory inventory) {
|
| 35 | this(containerId, inventory, new SimpleContainer(5), new SimpleContainerData(4));
|
| 36 | }
|
| 37 |
|
| 38 | public BrewingStandMenu(final int containerId, final Inventory inventory, final Container brewingStand, final ContainerData brewingStandData) {
|
| 39 | super(MenuType.BREWING_STAND, containerId);
|
| 40 | checkContainerSize(brewingStand, 5);
|
| 41 | checkContainerDataCount(brewingStandData, 4);
|
| 42 | this.brewingStand = brewingStand;
|
| 43 | this.brewingStandData = brewingStandData;
|
| 44 | RecipeAccess recipeAccess = inventory.player.level().recipeAccess();
|
| 45 | this.addSlot(new BrewingStandMenu.PotionSlot(recipeAccess, brewingStand, 0, 56, 51));
|
| 46 | this.addSlot(new BrewingStandMenu.PotionSlot(recipeAccess, brewingStand, 1, 79, 58));
|
| 47 | this.addSlot(new BrewingStandMenu.PotionSlot(recipeAccess, brewingStand, 2, 102, 51));
|
| 48 | this.ingredientSlot = this.addSlot(
|
| 49 | new BrewingStandMenu.IngredientsSlot(recipeAccess.propertySet(RecipePropertySet.BREWING_REAGENTS), brewingStand, 3, 79, 17)
|
| 50 | );
|
| 51 | this.addSlot(new BrewingStandMenu.FuelSlot(brewingStand, 4, 17, 17));
|
| 52 | this.addDataSlots(brewingStandData);
|
| 53 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 54 | }
|
| 55 |
|
| 56 | @Override
|
| 57 | public boolean stillValid(final Player player) {
|
| 58 | return this.brewingStand.stillValid(player);
|
| 59 | }
|
| 60 |
|
| 61 | @Override
|
| 62 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 63 | ItemStack clicked = ItemStack.EMPTY;
|
| 64 | Slot slot = this.slots.get(slotIndex);
|
| 65 | RecipeAccess recipeAccess = player.level().recipeAccess();
|
| 66 | if (slot != null && slot.hasItem()) {
|
| 67 | ItemStack stack = slot.getItem();
|
| 68 | clicked = stack.copy();
|
| 69 | if ((slotIndex < 0 || slotIndex > 2) && slotIndex != 3 && slotIndex != 4) {
|
| 70 | if (BrewingStandMenu.FuelSlot.mayPlaceItem(clicked)) {
|
| 71 | if (this.moveItemStackTo(stack, 4, 5, false) || this.ingredientSlot.mayPlace(stack) && !this.moveItemStackTo(stack, 3, 4, false)) {
|
| 72 | return ItemStack.EMPTY;
|
| 73 | }
|
| 74 | } else if (this.ingredientSlot.mayPlace(stack)) {
|
| 75 | if (!this.moveItemStackTo(stack, 3, 4, false)) {
|
| 76 | return ItemStack.EMPTY;
|
| 77 | }
|
| 78 | } else if (PotionIngredient.isPotionInput(clicked, recipeAccess)) {
|
| 79 | if (!this.moveItemStackTo(stack, 0, 3, false)) {
|
| 80 | return ItemStack.EMPTY;
|
| 81 | }
|
| 82 | } else if (slotIndex >= 5 && slotIndex < 32) {
|
| 83 | if (!this.moveItemStackTo(stack, 32, 41, false)) {
|
| 84 | return ItemStack.EMPTY;
|
| 85 | }
|
| 86 | } else if (slotIndex >= 32 && slotIndex < 41) {
|
| 87 | if (!this.moveItemStackTo(stack, 5, 32, false)) {
|
| 88 | return ItemStack.EMPTY;
|
| 89 | }
|
| 90 | } else if (!this.moveItemStackTo(stack, 5, 41, false)) {
|
| 91 | return ItemStack.EMPTY;
|
| 92 | }
|
| 93 | } else {
|
| 94 | if (!this.moveItemStackTo(stack, 5, 41, true)) {
|
| 95 | return ItemStack.EMPTY;
|
| 96 | }
|
| 97 |
|
| 98 | slot.onQuickCraft(stack, clicked);
|
| 99 | }
|
| 100 |
|
| 101 | if (stack.isEmpty()) {
|
| 102 | slot.setByPlayer(ItemStack.EMPTY);
|
| 103 | } else {
|
| 104 | slot.setChanged();
|
| 105 | }
|
| 106 |
|
| 107 | if (stack.getCount() == clicked.getCount()) {
|
| 108 | return ItemStack.EMPTY;
|
| 109 | }
|
| 110 |
|
| 111 | slot.onTake(player, clicked);
|
| 112 | }
|
| 113 |
|
| 114 | return clicked;
|
| 115 | }
|
| 116 |
|
| 117 | public int getFuel() {
|
| 118 | return this.brewingStandData.get(1);
|
| 119 | }
|
| 120 |
|
| 121 | public int getTotalFuel() {
|
| 122 | return this.brewingStandData.get(3);
|
| 123 | }
|
| 124 |
|
| 125 | public int getBrewingTicks() {
|
| 126 | return this.brewingStandData.get(0);
|
| 127 | }
|
| 128 |
|
| 129 | public int getTotalBrewingTicks() {
|
| 130 | return this.brewingStandData.get(2);
|
| 131 | }
|
| 132 |
|
| 133 | private static class FuelSlot extends Slot {
|
| 134 | public FuelSlot(final Container container, final int slot, final int x, final int y) {
|
| 135 | super(container, slot, x, y);
|
| 136 | }
|
| 137 |
|
| 138 | @Override
|
| 139 | public boolean mayPlace(final ItemStack itemStack) {
|
| 140 | return mayPlaceItem(itemStack);
|
| 141 | }
|
| 142 |
|
| 143 | public static boolean mayPlaceItem(final ItemStack itemStack) {
|
| 144 | return itemStack.has(DataComponents.BREWING_FUEL);
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public Identifier getNoItemIcon() {
|
| 149 | return BrewingStandMenu.EMPTY_SLOT_FUEL;
|
| 150 | }
|
| 151 | }
|
| 152 |
|
| 153 | private static class IngredientsSlot extends Slot {
|
| 154 | private final RecipePropertySet propertySet;
|
| 155 |
|
| 156 | public IngredientsSlot(final RecipePropertySet propertySet, final Container container, final int slot, final int x, final int y) {
|
| 157 | super(container, slot, x, y);
|
| 158 | this.propertySet = propertySet;
|
| 159 | }
|
| 160 |
|
| 161 | @Override
|
| 162 | public boolean mayPlace(final ItemStack itemStack) {
|
| 163 | return this.propertySet.test(itemStack);
|
| 164 | }
|
| 165 | }
|
| 166 |
|
| 167 | private static class PotionSlot extends Slot {
|
| 168 | private final RecipeAccess recipeAccess;
|
| 169 |
|
| 170 | public PotionSlot(final RecipeAccess recipeAccess, final Container container, final int slot, final int x, final int y) {
|
| 171 | super(container, slot, x, y);
|
| 172 | this.recipeAccess = recipeAccess;
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public boolean mayPlace(final ItemStack itemStack) {
|
| 177 | return PotionIngredient.isPotionInput(itemStack, this.recipeAccess);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public int getMaxStackSize() {
|
| 182 | return 1;
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public void onTake(final Player player, final ItemStack carried) {
|
| 187 | PotionContents potionContents = carried.get(DataComponents.POTION_CONTENTS);
|
| 188 | if (potionContents != null && player instanceof ServerPlayer serverPlayer) {
|
| 189 | CriteriaTriggers.BREWED_POTION.trigger(serverPlayer, potionContents);
|
| 190 | }
|
| 191 |
|
| 192 | super.onTake(player, carried);
|
| 193 | }
|
| 194 |
|
| 195 | @Override
|
| 196 | public Identifier getNoItemIcon() {
|
| 197 | return BrewingStandMenu.EMPTY_SLOT_POTION;
|
| 198 | }
|
| 199 | }
|
| 200 | }
|
| 201 |
|