| 1 | package net.minecraft.world.inventory;
|
| 2 |
|
| 3 | import java.util.List;
|
| 4 | import net.minecraft.core.component.DataComponents;
|
| 5 | import net.minecraft.recipebook.ServerPlaceRecipe;
|
| 6 | import net.minecraft.resources.ResourceKey;
|
| 7 | import net.minecraft.server.level.ServerLevel;
|
| 8 | import net.minecraft.util.Mth;
|
| 9 | import net.minecraft.world.Container;
|
| 10 | import net.minecraft.world.SimpleContainer;
|
| 11 | import net.minecraft.world.entity.player.Inventory;
|
| 12 | import net.minecraft.world.entity.player.Player;
|
| 13 | import net.minecraft.world.entity.player.StackedItemContents;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.crafting.AbstractCookingRecipe;
|
| 16 | import net.minecraft.world.item.crafting.RecipeHolder;
|
| 17 | import net.minecraft.world.item.crafting.RecipePropertySet;
|
| 18 | import net.minecraft.world.item.crafting.SingleRecipeInput;
|
| 19 | import net.minecraft.world.level.Level;
|
| 20 |
|
| 21 | public abstract class AbstractFurnaceMenu extends RecipeBookMenu {
|
| 22 | public static final int INGREDIENT_SLOT = 0;
|
| 23 | public static final int FUEL_SLOT = 1;
|
| 24 | public static final int RESULT_SLOT = 2;
|
| 25 | public static final int SLOT_COUNT = 3;
|
| 26 | public static final int DATA_COUNT = 4;
|
| 27 | private static final int INV_SLOT_START = 3;
|
| 28 | private static final int INV_SLOT_END = 30;
|
| 29 | private static final int USE_ROW_SLOT_START = 30;
|
| 30 | private static final int USE_ROW_SLOT_END = 39;
|
| 31 | private final Container container;
|
| 32 | private final ContainerData data;
|
| 33 | protected final Level level;
|
| 34 | private final RecipePropertySet acceptedInputs;
|
| 35 | private final RecipeBookType recipeBookType;
|
| 36 |
|
| 37 | protected AbstractFurnaceMenu(
|
| 38 | final MenuType<?> menuType,
|
| 39 | final ResourceKey<RecipePropertySet> allowedInputs,
|
| 40 | final RecipeBookType recipeBookType,
|
| 41 | final int containerId,
|
| 42 | final Inventory inventory
|
| 43 | ) {
|
| 44 | this(menuType, allowedInputs, recipeBookType, containerId, inventory, new SimpleContainer(3), new SimpleContainerData(4));
|
| 45 | }
|
| 46 |
|
| 47 | protected AbstractFurnaceMenu(
|
| 48 | final MenuType<?> menuType,
|
| 49 | final ResourceKey<RecipePropertySet> allowedInputs,
|
| 50 | final RecipeBookType recipeBookType,
|
| 51 | final int containerId,
|
| 52 | final Inventory inventory,
|
| 53 | final Container container,
|
| 54 | final ContainerData data
|
| 55 | ) {
|
| 56 | super(menuType, containerId);
|
| 57 | this.recipeBookType = recipeBookType;
|
| 58 | checkContainerSize(container, 3);
|
| 59 | checkContainerDataCount(data, 4);
|
| 60 | this.container = container;
|
| 61 | this.data = data;
|
| 62 | this.level = inventory.player.level();
|
| 63 | this.acceptedInputs = this.level.recipeAccess().propertySet(allowedInputs);
|
| 64 | this.addSlot(new Slot(container, 0, 56, 17));
|
| 65 | this.addSlot(new FurnaceFuelSlot(this, container, 1, 56, 53));
|
| 66 | this.addSlot(new FurnaceResultSlot(inventory.player, container, 2, 116, 35));
|
| 67 | this.addStandardInventorySlots(inventory, 8, 84);
|
| 68 | this.addDataSlots(data);
|
| 69 | }
|
| 70 |
|
| 71 | @Override
|
| 72 | public void fillCraftSlotsStackedContents(final StackedItemContents stackedContents) {
|
| 73 | if (this.container instanceof StackedContentsCompatible stackedContentsCompatible) {
|
| 74 | stackedContentsCompatible.fillStackedContents(stackedContents);
|
| 75 | }
|
| 76 | }
|
| 77 |
|
| 78 | public Slot getResultSlot() {
|
| 79 | return this.slots.get(2);
|
| 80 | }
|
| 81 |
|
| 82 | @Override
|
| 83 | public boolean stillValid(final Player player) {
|
| 84 | return this.container.stillValid(player);
|
| 85 | }
|
| 86 |
|
| 87 | @Override
|
| 88 | public ItemStack quickMoveStack(final Player player, final int slotIndex) {
|
| 89 | ItemStack clicked = ItemStack.EMPTY;
|
| 90 | Slot slot = this.slots.get(slotIndex);
|
| 91 | if (slot != null && slot.hasItem()) {
|
| 92 | ItemStack stack = slot.getItem();
|
| 93 | clicked = stack.copy();
|
| 94 | if (slotIndex == 2) {
|
| 95 | if (!this.moveItemStackTo(stack, 3, 39, true)) {
|
| 96 | return ItemStack.EMPTY;
|
| 97 | }
|
| 98 |
|
| 99 | slot.onQuickCraft(stack, clicked);
|
| 100 | } else if (slotIndex != 1 && slotIndex != 0) {
|
| 101 | if (this.canSmelt(stack)) {
|
| 102 | if (!this.moveItemStackTo(stack, 0, 1, false)) {
|
| 103 | return ItemStack.EMPTY;
|
| 104 | }
|
| 105 | } else if (this.isFuel(stack)) {
|
| 106 | if (!this.moveItemStackTo(stack, 1, 2, false)) {
|
| 107 | return ItemStack.EMPTY;
|
| 108 | }
|
| 109 | } else if (slotIndex >= 3 && slotIndex < 30) {
|
| 110 | if (!this.moveItemStackTo(stack, 30, 39, false)) {
|
| 111 | return ItemStack.EMPTY;
|
| 112 | }
|
| 113 | } else if (slotIndex >= 30 && slotIndex < 39 && !this.moveItemStackTo(stack, 3, 30, false)) {
|
| 114 | return ItemStack.EMPTY;
|
| 115 | }
|
| 116 | } else if (!this.moveItemStackTo(stack, 3, 39, false)) {
|
| 117 | return ItemStack.EMPTY;
|
| 118 | }
|
| 119 |
|
| 120 | if (stack.isEmpty()) {
|
| 121 | slot.setByPlayer(ItemStack.EMPTY);
|
| 122 | } else {
|
| 123 | slot.setChanged();
|
| 124 | }
|
| 125 |
|
| 126 | if (stack.getCount() == clicked.getCount()) {
|
| 127 | return ItemStack.EMPTY;
|
| 128 | }
|
| 129 |
|
| 130 | slot.onTake(player, stack);
|
| 131 | }
|
| 132 |
|
| 133 | return clicked;
|
| 134 | }
|
| 135 |
|
| 136 | protected boolean canSmelt(final ItemStack itemStack) {
|
| 137 | return this.acceptedInputs.test(itemStack);
|
| 138 | }
|
| 139 |
|
| 140 | protected boolean isFuel(final ItemStack itemStack) {
|
| 141 | return itemStack.has(DataComponents.COOKING_FUEL);
|
| 142 | }
|
| 143 |
|
| 144 | public float getBurnProgress() {
|
| 145 | int current = this.data.get(2);
|
| 146 | int total = this.data.get(3);
|
| 147 | return total != 0 && current != 0 ? Mth.clamp((float)current / total, 0.0F, 1.0F) : 0.0F;
|
| 148 | }
|
| 149 |
|
| 150 | public float getLitProgress() {
|
| 151 | int litDuration = this.data.get(1);
|
| 152 | if (litDuration == 0) {
|
| 153 | litDuration = 200;
|
| 154 | }
|
| 155 |
|
| 156 | return Mth.clamp((float)this.data.get(0) / litDuration, 0.0F, 1.0F);
|
| 157 | }
|
| 158 |
|
| 159 | public boolean isLit() {
|
| 160 | return this.data.get(0) > 0;
|
| 161 | }
|
| 162 |
|
| 163 | @Override
|
| 164 | public RecipeBookType getRecipeBookType() {
|
| 165 | return this.recipeBookType;
|
| 166 | }
|
| 167 |
|
| 168 | @Override
|
| 169 | public RecipeBookMenu.PostPlaceAction handlePlacement(
|
| 170 | final boolean useMaxItems, final boolean allowDroppingItemsToClear, final RecipeHolder<?> recipe, final ServerLevel level, final Inventory inventory
|
| 171 | ) {
|
| 172 | final List<Slot> slotsToClear = List.of(this.getSlot(0), this.getSlot(2));
|
| 173 | RecipeHolder<AbstractCookingRecipe> typedRecipe = (RecipeHolder<AbstractCookingRecipe>)recipe;
|
| 174 | return ServerPlaceRecipe.placeRecipe(new ServerPlaceRecipe.CraftingMenuAccess<AbstractCookingRecipe>() {
|
| 175 | @Override
|
| 176 | public void fillCraftSlotsStackedContents(final StackedItemContents stackedContents) {
|
| 177 | AbstractFurnaceMenu.this.fillCraftSlotsStackedContents(stackedContents);
|
| 178 | }
|
| 179 |
|
| 180 | @Override
|
| 181 | public void clearCraftingContent() {
|
| 182 | slotsToClear.forEach(s -> s.set(ItemStack.EMPTY));
|
| 183 | }
|
| 184 |
|
| 185 | @Override
|
| 186 | public boolean recipeMatches(final RecipeHolder<AbstractCookingRecipe> recipe) {
|
| 187 | return recipe.value().matches(new SingleRecipeInput(AbstractFurnaceMenu.this.container.getItem(0)), level);
|
| 188 | }
|
| 189 | }, 1, 1, List.of(this.getSlot(0)), slotsToClear, inventory, typedRecipe, useMaxItems, allowDroppingItemsToClear);
|
| 190 | }
|
| 191 | }
|
| 192 |
|