blob: 9e458af6a58d9adf71cca930bf1dd205bc531a0f (
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
50
|
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.GTUtility;
/**
* 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 || !GTUtility.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);
}
}
|