| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Map;
|
| 5 | import net.minecraft.resources.Identifier;
|
| 6 | import net.minecraft.server.level.ServerLevel;
|
| 7 | import net.minecraft.util.Prediction;
|
| 8 | import net.minecraft.world.Container;
|
| 9 | import net.minecraft.world.entity.EquipmentSlot;
|
| 10 | import net.minecraft.world.entity.player.Inventory;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.ItemStack;
|
| 13 |
|
| 14 | public class InventoryMenu extends AbstractCraftingMenu {
|
| 15 | public static final int CONTAINER_ID = 0;
|
| 16 | public static final int RESULT_SLOT = 0;
|
| 17 | private static final int CRAFTING_GRID_WIDTH = 2;
|
| 18 | private static final int CRAFTING_GRID_HEIGHT = 2;
|
| 19 | public static final int CRAFT_SLOT_START = 1;
|
| 20 | public static final int CRAFT_SLOT_COUNT = 4;
|
| 21 | public static final int CRAFT_SLOT_END = 5;
|
| 22 | public static final int ARMOR_SLOT_START = 5;
|
| 23 | public static final int ARMOR_SLOT_COUNT = 4;
|
| 24 | public static final int ARMOR_SLOT_END = 9;
|
| 25 | public static final int INV_SLOT_START = 9;
|
| 26 | public static final int INV_SLOT_END = 36;
|
| 27 | public static final int USE_ROW_SLOT_START = 36;
|
| 28 | public static final int USE_ROW_SLOT_END = 45;
|
| 29 | public static final int SHIELD_SLOT = 45;
|
| 30 | public static final Identifier EMPTY_ARMOR_SLOT_HELMET = Identifier.withDefaultNamespace("container/slot/helmet");
|
| 31 | public static final Identifier EMPTY_ARMOR_SLOT_CHESTPLATE = Identifier.withDefaultNamespace("container/slot/chestplate");
|
| 32 | public static final Identifier EMPTY_ARMOR_SLOT_LEGGINGS = Identifier.withDefaultNamespace("container/slot/leggings");
|
| 33 | public static final Identifier EMPTY_ARMOR_SLOT_BOOTS = Identifier.withDefaultNamespace("container/slot/boots");
|
| 34 | public static final Identifier EMPTY_ARMOR_SLOT_SHIELD = Identifier.withDefaultNamespace("container/slot/shield");
|
| 35 | private static final Map<EquipmentSlot, Identifier> TEXTURE_EMPTY_SLOTS = Map.of(
|
| 36 | EquipmentSlot.FEET,
|
| 37 | EMPTY_ARMOR_SLOT_BOOTS,
|
| 38 | EquipmentSlot.LEGS,
|
| 39 | EMPTY_ARMOR_SLOT_LEGGINGS,
|
| 40 | EquipmentSlot.CHEST,
|
| 41 | EMPTY_ARMOR_SLOT_CHESTPLATE,
|
| 42 | EquipmentSlot.HEAD,
|
| 43 | EMPTY_ARMOR_SLOT_HELMET
|
| 44 | );
|
| 45 | private static final EquipmentSlot[] SLOT_IDS = new EquipmentSlot[]{EquipmentSlot.HEAD, EquipmentSlot.CHEST, EquipmentSlot.LEGS, EquipmentSlot.FEET};
|
| 46 | public final boolean active;
|
| 47 | private final Player owner;
|
| 48 |
|
| 49 | public InventoryMenu(final Inventory inventory, final boolean active, final Player owner) {
|
| 50 | super(null, 0, 2, 2);
|
| 51 | this.active = active;
|
| 52 | this.owner = owner;
|
| 53 | this.addResultSlot(owner, 154, 28);
|
| 54 | this.addCraftingGridSlots(98, 18);
|
| 55 |
|
| 56 | for (int i = 0; i < 4; i++) {
|
| 57 | EquipmentSlot slot = SLOT_IDS[i];
|
| 58 | Identifier emptyIcon = TEXTURE_EMPTY_SLOTS.get(slot);
|
| 59 | this.addSlot(new ArmorSlot(inventory, owner, slot, 39 - i, 8, 8 + i * 18, emptyIcon));
|
| 60 | }
|
| 61 |
|
| 62 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 63 | this.addSlot(new Slot(inventory, 40, 77, 62) {
|
| 64 | @Override
|
| 65 | public void setByPlayer(final ItemStack itemStack, final ItemStack previous) {
|
| 66 | owner.onEquipItem(EquipmentSlot.OFFHAND, previous, itemStack);
|
| 67 | super.setByPlayer(itemStack, previous);
|
| 68 | }
|
| 69 |
|
| 70 | @Override
|
| 71 | public Identifier getNoItemIcon() {
|
| 72 | return InventoryMenu.EMPTY_ARMOR_SLOT_SHIELD;
|
| 73 | }
|
| 74 | });
|
| 75 | }
|
| 76 |
|
| 77 | public static boolean isHotbarSlot(final int slot) {
|
| 78 | return slot >= 36 && slot < 45 || slot == 45;
|
| 79 | }
|
| 80 |
|
| 81 | @Override
|
| 82 | public void slotsChanged(final Container container) {
|
| 83 | if (this.owner.level() instanceof ServerLevel level) {
|
| 84 | CraftingMenu.slotChangedCraftingGrid(this, level, this.owner, this.craftSlots, this.resultSlots, null);
|
| 85 | }
|
| 86 | }
|
| 87 |
|
| 88 | @Override
|
| 89 | public void removed(final Player player) {
|
| 90 | super.removed(player);
|
| 91 | this.resultSlots.clearContent();
|
| 92 | if (!player.level().isClientSide()) {
|
| 93 | this.clearContainer(player, this.craftSlots);
|
| 94 | }
|
| 95 | }
|
| 96 |
|
| 97 | @Override
|
| 98 | public boolean stillValid(final Player player) {
|
| 99 | return true;
|
| 100 | }
|
| 101 |
|
| 102 | @Override
|
| 103 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 104 | ItemStack clicked = ItemStack.EMPTY;
|
| 105 | Slot slot = this.slots.get(slotIndex);
|
| 106 | if (slot.hasItem()) {
|
| 107 | ItemStack stack = slot.getItem();
|
| 108 | clicked = stack.copy();
|
| 109 | EquipmentSlot eqSlot = player.getEquipmentSlotForItem(clicked);
|
| 110 | if (slotIndex == 0) {
|
| 111 | if (!this.moveItemStackTo(stack, 9, 45, true)) {
|
| 112 | return ItemStack.EMPTY;
|
| 113 | }
|
| 114 |
|
| 115 | slot.onQuickCraft(stack, clicked);
|
| 116 | } else if (slotIndex >= 1 && slotIndex < 5) {
|
| 117 | if (!this.moveItemStackTo(stack, 9, 45, false)) {
|
| 118 | return ItemStack.EMPTY;
|
| 119 | }
|
| 120 | } else if (slotIndex >= 5 && slotIndex < 9) {
|
| 121 | if (!this.moveItemStackTo(stack, 9, 45, false)) {
|
| 122 | return ItemStack.EMPTY;
|
| 123 | }
|
| 124 | } else if (eqSlot.getType() == EquipmentSlot.Type.HUMANOID_ARMOR && !this.slots.get(8 - eqSlot.getIndex()).hasItem()) {
|
| 125 | int pos = 8 - eqSlot.getIndex();
|
| 126 | if (!this.moveItemStackTo(stack, pos, pos + 1, false)) {
|
| 127 | return ItemStack.EMPTY;
|
| 128 | }
|
| 129 | } else if (eqSlot == EquipmentSlot.OFFHAND && !this.slots.get(45).hasItem()) {
|
| 130 | if (!this.moveItemStackTo(stack, 45, 46, false)) {
|
| 131 | return ItemStack.EMPTY;
|
| 132 | }
|
| 133 | } else if (slotIndex >= 9 && slotIndex < 36) {
|
| 134 | if (!this.moveItemStackTo(stack, 36, 45, false)) {
|
| 135 | return ItemStack.EMPTY;
|
| 136 | }
|
| 137 | } else if (slotIndex >= 36 && slotIndex < 45) {
|
| 138 | if (!this.moveItemStackTo(stack, 9, 36, false)) {
|
| 139 | return ItemStack.EMPTY;
|
| 140 | }
|
| 141 | } else if (!this.moveItemStackTo(stack, 9, 45, false)) {
|
| 142 | return ItemStack.EMPTY;
|
| 143 | }
|
| 144 |
|
| 145 | if (stack.isEmpty()) {
|
| 146 | slot.setByPlayer(ItemStack.EMPTY, clicked);
|
| 147 | } else {
|
| 148 | slot.setChanged();
|
| 149 | }
|
| 150 |
|
| 151 | if (stack.getCount() == clicked.getCount()) {
|
| 152 | return ItemStack.EMPTY;
|
| 153 | }
|
| 154 |
|
| 155 | slot.onTake(player, stack);
|
| 156 | if (slotIndex == 0) {
|
| 157 | player.drop(stack, false, Prediction.PREDICTED);
|
| 158 | }
|
| 159 | }
|
| 160 |
|
| 161 | return clicked;
|
| 162 | }
|
| 163 |
|
| 164 | @Override
|
| 165 | public boolean canTakeItemForPickAll(final ItemStack carried, final Slot target) {
|
| 166 | return target.container != this.resultSlots && super.canTakeItemForPickAll(carried, target);
|
| 167 | }
|
| 168 |
|
| 169 | @Override
|
| 170 | public Slot getResultSlot() {
|
| 171 | return this.slots.get(0);
|
| 172 | }
|
| 173 |
|
| 174 | @Override
|
| 175 | public List<Slot> getInputGridSlots() {
|
| 176 | return this.slots.subList(1, 5);
|
| 177 | }
|
| 178 |
|
| 179 | public CraftingContainer getCraftSlots() {
|
| 180 | return this.craftSlots;
|
| 181 | }
|
| 182 |
|
| 183 | @Override
|
| 184 | public RecipeBookType getRecipeBookType() {
|
| 185 | return RecipeBookType.CRAFTING;
|
| 186 | }
|
| 187 |
|
| 188 | @Override
|
| 189 | protected Player owner() {
|
| 190 | return this.owner;
|
| 191 | }
|
| 192 | }
|
| 193 |
|