aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/gregtech/common/gui/modularui/widget/ItemWatcherSlotWidget.java
blob: 265450d3847ea4802bd204211a97b55984a50a1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package gregtech.common.gui.modularui.widget;

import java.util.function.Supplier;

import net.minecraft.item.ItemStack;

import com.gtnewhorizons.modularui.api.forge.IItemHandlerModifiable;
import com.gtnewhorizons.modularui.api.forge.ItemStackHandler;
import com.gtnewhorizons.modularui.common.internal.wrapper.BaseSlot;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;
import gregtech.api.util.GT_Utility;

/**
 * Watches specific ItemStack and pulls changes from it. Player cannot interact with slot, other than viewing NEI recipe
 * or adding bookmark.
 */
public class ItemWatcherSlotWidget extends SlotWidget {

    private ItemStack lastItem;
    private Supplier<ItemStack> getter;

    public ItemWatcherSlotWidget() {
        super(BaseSlot.phantom(new ItemStackHandler(), 0));
        disableInteraction();
    }

    public ItemWatcherSlotWidget setGetter(Supplier<ItemStack> getter) {
        this.getter = getter;
        return this;
    }

    @Override
    public void detectAndSendChanges(boolean init) {
        ItemStack target = getter.get();
        if (init || !GT_Utility.areStacksEqual(lastItem, target)) {
            ItemStack toPut;
            if (target != null) {
                toPut = target.copy();
                toPut.stackSize = 1;
            } else {
                toPut = null;
            }
            ((IItemHandlerModifiable) getMcSlot().getItemHandler()).setStackInSlot(0, toPut);
            lastItem = target;
            getMcSlot().onSlotChanged();
        }
        super.detectAndSendChanges(init);
    }
}