| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import java.util.Optional;
|
| 5 | import net.minecraft.sounds.SoundEvents;
|
| 6 | import net.minecraft.sounds.SoundSource;
|
| 7 | import net.minecraft.util.Prediction;
|
| 8 | import net.minecraft.world.Container;
|
| 9 | import net.minecraft.world.SimpleContainer;
|
| 10 | import net.minecraft.world.entity.player.Inventory;
|
| 11 | import net.minecraft.world.entity.player.Player;
|
| 12 | import net.minecraft.world.item.Item;
|
| 13 | import net.minecraft.world.item.ItemStack;
|
| 14 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 15 | import net.minecraft.world.item.crafting.SelectableRecipe;
|
| 16 | import net.minecraft.world.item.crafting.SingleRecipeInput;
|
| 17 | import net.minecraft.world.item.crafting.StonecutterRecipe;
|
| 18 | import net.minecraft.world.level.Level;
|
| 19 | import net.minecraft.world.level.block.Blocks;
|
| 20 |
|
| 21 | public class StonecutterMenu extends AbstractContainerMenu {
|
| 22 | public static final int INPUT_SLOT = 0;
|
| 23 | public static final int RESULT_SLOT = 1;
|
| 24 | private static final int INV_SLOT_START = 2;
|
| 25 | private static final int INV_SLOT_END = 29;
|
| 26 | private static final int USE_ROW_SLOT_START = 29;
|
| 27 | private static final int USE_ROW_SLOT_END = 38;
|
| 28 | private final ContainerLevelAccess access;
|
| 29 | private final DataSlot selectedRecipeIndex = DataSlot.standalone();
|
| 30 | private final Level level;
|
| 31 | private SelectableRecipe.SingleInputSet<StonecutterRecipe> recipesForInput = SelectableRecipe.SingleInputSet.empty();
|
| 32 | private ItemStack input = ItemStack.EMPTY;
|
| 33 | private long lastSoundTime;
|
| 34 | private final Slot inputSlot;
|
| 35 | private final Slot resultSlot;
|
| 36 | private Runnable slotUpdateListener = () -> {};
|
| 37 | public final Container container = new SimpleContainer(1) {
|
| 38 | @Override
|
| 39 | public void setChanged() {
|
| 40 | super.setChanged();
|
| 41 | StonecutterMenu.this.slotsChanged(this);
|
| 42 | StonecutterMenu.this.slotUpdateListener.run();
|
| 43 | }
|
| 44 | };
|
| 45 | private final ResultContainer resultContainer = new ResultContainer();
|
| 46 |
|
| 47 | public StonecutterMenu(final int containerId, final Inventory inventory) {
|
| 48 | this(containerId, inventory, ContainerLevelAccess.NULL);
|
| 49 | }
|
| 50 |
|
| 51 | public StonecutterMenu(final int containerId, final Inventory inventory, final ContainerLevelAccess access) {
|
| 52 | super(MenuType.STONECUTTER, containerId);
|
| 53 | this.access = access;
|
| 54 | this.level = inventory.player.level();
|
| 55 | this.inputSlot = this.addSlot(new Slot(this.container, 0, 20, 33));
|
| 56 | this.resultSlot = this.addSlot(new Slot(this.resultContainer, 1, 143, 33) {
|
| 57 | @Override
|
| 58 | public boolean mayPlace(final ItemStack itemStack) {
|
| 59 | return false;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public void onTake(final Player player, final ItemStack carried) {
|
| 64 | carried.onCraftedBy(player, carried.getCount());
|
| 65 | StonecutterMenu.this.resultContainer.awardUsedRecipes(player, this.getRelevantItems());
|
| 66 | ItemStack remaining = StonecutterMenu.this.inputSlot.remove(1);
|
| 67 | if (!remaining.isEmpty()) {
|
| 68 | StonecutterMenu.this.setupResultSlot(StonecutterMenu.this.selectedRecipeIndex.get());
|
| 69 | }
|
| 70 |
|
| 71 | access.execute((level, pos) -> {
|
| 72 | long gameTime = level.getGameTime();
|
| 73 | if (StonecutterMenu.this.lastSoundTime != gameTime) {
|
| 74 | level.playSound(null, pos, SoundEvents.UI_STONECUTTER_TAKE_RESULT, SoundSource.BLOCKS, 1.0F, 1.0F);
|
| 75 | StonecutterMenu.this.lastSoundTime = gameTime;
|
| 76 | }
|
| 77 | });
|
| 78 | super.onTake(player, carried);
|
| 79 | }
|
| 80 |
|
| 81 | private List<ItemStack> getRelevantItems() {
|
| 82 | return List.of(StonecutterMenu.this.inputSlot.getItem());
|
| 83 | }
|
| 84 | });
|
| 85 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 86 | this.addDataSlot(this.selectedRecipeIndex);
|
| 87 | }
|
| 88 |
|
| 89 | public int getSelectedRecipeIndex() {
|
| 90 | return this.selectedRecipeIndex.get();
|
| 91 | }
|
| 92 |
|
| 93 | public SelectableRecipe.SingleInputSet<StonecutterRecipe> getVisibleRecipes() {
|
| 94 | return this.recipesForInput;
|
| 95 | }
|
| 96 |
|
| 97 | public int getNumberOfVisibleRecipes() {
|
| 98 | return this.recipesForInput.size();
|
| 99 | }
|
| 100 |
|
| 101 | public boolean hasInputItem() {
|
| 102 | return this.inputSlot.hasItem() && !this.recipesForInput.isEmpty();
|
| 103 | }
|
| 104 |
|
| 105 | @Override
|
| 106 | public boolean stillValid(final Player player) {
|
| 107 | return stillValid(this.access, player, Blocks.STONECUTTER);
|
| 108 | }
|
| 109 |
|
| 110 | @Override
|
| 111 | public boolean clickMenuButton(final Player player, final int buttonId) {
|
| 112 | if (this.selectedRecipeIndex.get() == buttonId) {
|
| 113 | return false;
|
| 114 | }
|
| 115 |
|
| 116 | if (this.isValidRecipeIndex(buttonId)) {
|
| 117 | this.selectedRecipeIndex.set(buttonId);
|
| 118 | this.setupResultSlot(buttonId);
|
| 119 | }
|
| 120 |
|
| 121 | return true;
|
| 122 | }
|
| 123 |
|
| 124 | private boolean isValidRecipeIndex(final int buttonId) {
|
| 125 | return buttonId >= 0 && buttonId < this.recipesForInput.size();
|
| 126 | }
|
| 127 |
|
| 128 | @Override
|
| 129 | public void slotsChanged(final Container container) {
|
| 130 | ItemStack input = this.inputSlot.getItem();
|
| 131 | if (!input.is(this.input.getItem())) {
|
| 132 | this.input = input.copy();
|
| 133 | this.setupRecipeList(input);
|
| 134 | }
|
| 135 | }
|
| 136 |
|
| 137 | private void setupRecipeList(final ItemStack item) {
|
| 138 | this.selectedRecipeIndex.set(-1);
|
| 139 | this.resultSlot.set(ItemStack.EMPTY);
|
| 140 | if (!item.isEmpty()) {
|
| 141 | this.recipesForInput = this.level.recipeAccess().stonecutterRecipes().selectByInput(item);
|
| 142 | } else {
|
| 143 | this.recipesForInput = SelectableRecipe.SingleInputSet.empty();
|
| 144 | }
|
| 145 | }
|
| 146 |
|
| 147 | private void setupResultSlot(final int index) {
|
| 148 | Optional<RecipeHolder<StonecutterRecipe>> usedRecipe;
|
| 149 | if (!this.recipesForInput.isEmpty() && this.isValidRecipeIndex(index)) {
|
| 150 | SelectableRecipe.SingleInputEntry<StonecutterRecipe> entry = this.recipesForInput.entries().get(index);
|
| 151 | usedRecipe = entry.recipe().recipe();
|
| 152 | } else {
|
| 153 | usedRecipe = Optional.empty();
|
| 154 | }
|
| 155 |
|
| 156 | usedRecipe.ifPresentOrElse(recipe -> {
|
| 157 | this.resultContainer.setRecipeUsed((RecipeHolder<?>)recipe);
|
| 158 | this.resultSlot.set(recipe.value().assemble(new SingleRecipeInput(this.container.getItem(0))));
|
| 159 | }, () -> {
|
| 160 | this.resultSlot.set(ItemStack.EMPTY);
|
| 161 | this.resultContainer.setRecipeUsed(null);
|
| 162 | });
|
| 163 | this.broadcastChanges();
|
| 164 | }
|
| 165 |
|
| 166 | @Override
|
| 167 | public MenuType<?> getType() {
|
| 168 | return MenuType.STONECUTTER;
|
| 169 | }
|
| 170 |
|
| 171 | public void registerUpdateListener(final Runnable slotUpdateListener) {
|
| 172 | this.slotUpdateListener = slotUpdateListener;
|
| 173 | }
|
| 174 |
|
| 175 | @Override
|
| 176 | public boolean canTakeItemForPickAll(final ItemStack carried, final Slot target) {
|
| 177 | return target.container != this.resultContainer && super.canTakeItemForPickAll(carried, target);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 182 | ItemStack clicked = ItemStack.EMPTY;
|
| 183 | Slot slot = this.slots.get(slotIndex);
|
| 184 | if (slot != null && slot.hasItem()) {
|
| 185 | ItemStack stack = slot.getItem();
|
| 186 | Item item = stack.getItem();
|
| 187 | clicked = stack.copy();
|
| 188 | if (slotIndex == 1) {
|
| 189 | item.onCraftedBy(stack, player);
|
| 190 | if (!this.moveItemStackTo(stack, 2, 38, true)) {
|
| 191 | return ItemStack.EMPTY;
|
| 192 | }
|
| 193 |
|
| 194 | slot.onQuickCraft(stack, clicked);
|
| 195 | } else if (slotIndex == 0) {
|
| 196 | if (!this.moveItemStackTo(stack, 2, 38, false)) {
|
| 197 | return ItemStack.EMPTY;
|
| 198 | }
|
| 199 | } else if (this.level.recipeAccess().stonecutterRecipes().acceptsInput(stack)) {
|
| 200 | if (!this.moveItemStackTo(stack, 0, 1, false)) {
|
| 201 | return ItemStack.EMPTY;
|
| 202 | }
|
| 203 | } else if (slotIndex >= 2 && slotIndex < 29) {
|
| 204 | if (!this.moveItemStackTo(stack, 29, 38, false)) {
|
| 205 | return ItemStack.EMPTY;
|
| 206 | }
|
| 207 | } else if (slotIndex >= 29 && slotIndex < 38 && !this.moveItemStackTo(stack, 2, 29, false)) {
|
| 208 | return ItemStack.EMPTY;
|
| 209 | }
|
| 210 |
|
| 211 | if (stack.isEmpty()) {
|
| 212 | slot.setByPlayer(ItemStack.EMPTY);
|
| 213 | }
|
| 214 |
|
| 215 | slot.setChanged();
|
| 216 | if (stack.getCount() == clicked.getCount()) {
|
| 217 | return ItemStack.EMPTY;
|
| 218 | }
|
| 219 |
|
| 220 | slot.onTake(player, stack);
|
| 221 | if (slotIndex == 1) {
|
| 222 | player.drop(stack, false, Prediction.PREDICTED);
|
| 223 | }
|
| 224 |
|
| 225 | this.broadcastChanges();
|
| 226 | }
|
| 227 |
|
| 228 | return clicked;
|
| 229 | }
|
| 230 |
|
| 231 | @Override
|
| 232 | public void removed(final Player player) {
|
| 233 | super.removed(player);
|
| 234 | this.resultContainer.removeItemNoUpdate(1);
|
| 235 | this.access.execute((level, pos) -> this.clearContainer(player, this.container));
|
| 236 | }
|
| 237 | }
|
| 238 |
|