| 1 | package net.minecraft.client.gui.screens.recipebook;
|
| 2 |
|
| 3 | import it.unimi.dsi.fastutil.objects.Reference2ObjectArrayMap;
|
| 4 | import it.unimi.dsi.fastutil.objects.Reference2ObjectMap;
|
| 5 | import java.util.List;
|
| 6 | import net.fabricmc.api.EnvType;
|
| 7 | import net.fabricmc.api.Environment;
|
| 8 | import net.minecraft.client.Minecraft;
|
| 9 | import net.minecraft.client.gui.GuiGraphicsExtractor;
|
| 10 | import net.minecraft.client.gui.screens.Screen;
|
| 11 | import net.minecraft.core.component.DataComponents;
|
| 12 | import net.minecraft.util.context.ContextMap;
|
| 13 | import net.minecraft.world.inventory.Slot;
|
| 14 | import net.minecraft.world.item.ItemStack;
|
| 15 | import net.minecraft.world.item.crafting.display.SlotDisplay;
|
| 16 | import org.jspecify.annotations.Nullable;
|
| 17 |
|
| 18 | @Environment(EnvType.CLIENT)
|
| 19 | public class GhostSlots {
|
| 20 | private final Reference2ObjectMap<Slot, GhostSlots.GhostSlot> ingredients = new Reference2ObjectArrayMap<>();
|
| 21 | private final SlotSelectTime slotSelectTime;
|
| 22 |
|
| 23 | public GhostSlots(final SlotSelectTime slotSelectTime) {
|
| 24 | this.slotSelectTime = slotSelectTime;
|
| 25 | }
|
| 26 |
|
| 27 | public void clear() {
|
| 28 | this.ingredients.clear();
|
| 29 | }
|
| 30 |
|
| 31 | private void setSlot(final Slot slot, final ContextMap context, final SlotDisplay contents, final boolean isResult) {
|
| 32 | List<ItemStack> entries = contents.resolveForStacks(context);
|
| 33 | if (!entries.isEmpty()) {
|
| 34 | this.ingredients.put(slot, new GhostSlots.GhostSlot(entries, isResult));
|
| 35 | }
|
| 36 | }
|
| 37 |
|
| 38 | protected void setInput(final Slot slot, final ContextMap context, final SlotDisplay contents) {
|
| 39 | this.setSlot(slot, context, contents, false);
|
| 40 | }
|
| 41 |
|
| 42 | protected void setResult(final Slot slot, final ContextMap context, final SlotDisplay contents) {
|
| 43 | this.setSlot(slot, context, contents, true);
|
| 44 | }
|
| 45 |
|
| 46 | public void extractRenderState(final GuiGraphicsExtractor graphics, final Minecraft minecraft, final boolean isResultSlotBig) {
|
| 47 | this.ingredients.forEach((slot, ingredient) -> {
|
| 48 | int x = slot.x;
|
| 49 | int y = slot.y;
|
| 50 | if (ingredient.isResultSlot && isResultSlotBig) {
|
| 51 | graphics.fill(x - 4, y - 4, x + 20, y + 20, 822018048);
|
| 52 | } else {
|
| 53 | graphics.fill(x, y, x + 16, y + 16, 822018048);
|
| 54 | }
|
| 55 |
|
| 56 | ItemStack itemStack = ingredient.getItem(this.slotSelectTime.currentIndex());
|
| 57 | graphics.fakeItem(itemStack, x, y);
|
| 58 | graphics.fill(x, y, x + 16, y + 16, 822083583);
|
| 59 | if (ingredient.isResultSlot) {
|
| 60 | graphics.itemDecorations(minecraft.font, itemStack, x, y);
|
| 61 | }
|
| 62 | });
|
| 63 | }
|
| 64 |
|
| 65 | public void extractTooltip(
|
| 66 | final GuiGraphicsExtractor graphics, final Minecraft minecraft, final int mouseX, final int mouseY, final @Nullable Slot hoveredSlot
|
| 67 | ) {
|
| 68 | if (hoveredSlot != null) {
|
| 69 | GhostSlots.GhostSlot hoveredGhostSlot = this.ingredients.get(hoveredSlot);
|
| 70 | if (hoveredGhostSlot != null) {
|
| 71 | ItemStack hoveredItem = hoveredGhostSlot.getItem(this.slotSelectTime.currentIndex());
|
| 72 | graphics.setComponentTooltipForNextFrame(
|
| 73 | minecraft.font, Screen.getTooltipFromItem(minecraft, hoveredItem), mouseX, mouseY, hoveredItem.get(DataComponents.TOOLTIP_STYLE)
|
| 74 | );
|
| 75 | }
|
| 76 | }
|
| 77 | }
|
| 78 |
|
| 79 | @Environment(EnvType.CLIENT)
|
| 80 | private record GhostSlot(List<ItemStack> items, boolean isResultSlot) {
|
| 81 | public ItemStack getItem(final int itemIndex) {
|
| 82 | int size = this.items.size();
|
| 83 | return size == 0 ? ItemStack.EMPTY : this.items.get(itemIndex % size);
|
| 84 | }
|
| 85 | }
|
| 86 | }
|
| 87 |
|