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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
package gregtech.common.gui.modularui.widget;
import java.util.function.Consumer;
import java.util.function.Function;
import net.minecraft.item.ItemStack;
import com.gtnewhorizons.modularui.api.forge.IItemHandlerModifiable;
import com.gtnewhorizons.modularui.api.widget.Interactable;
import com.gtnewhorizons.modularui.common.internal.network.NetworkUtils;
import com.gtnewhorizons.modularui.common.internal.wrapper.BaseSlot;
import com.gtnewhorizons.modularui.common.widget.SlotWidget;
import gregtech.api.gui.modularui.IDataFollowerWidget;
import gregtech.api.util.ISerializableObject;
public class CoverDataFollowerSlotWidget<T extends ISerializableObject> extends SlotWidget
implements IDataFollowerWidget<T, ItemStack> {
private Function<T, ItemStack> dataToStateGetter;
private Consumer<ItemStack> dataSetter;
public CoverDataFollowerSlotWidget(BaseSlot slot) {
super(slot);
}
public CoverDataFollowerSlotWidget(IItemHandlerModifiable handler, int index, boolean phantom) {
this(new BaseSlot(handler, index, phantom));
}
public CoverDataFollowerSlotWidget(IItemHandlerModifiable handler, int index) {
this(handler, index, false);
}
@Override
public CoverDataFollowerSlotWidget<T> setDataToStateGetter(Function<T, ItemStack> dataToStateGetter) {
this.dataToStateGetter = dataToStateGetter;
return this;
}
@Override
public CoverDataFollowerSlotWidget<T> setStateSetter(Consumer<ItemStack> setter) {
this.dataSetter = setter;
return this;
}
@Override
public void updateState(T data) {
getMcSlot().putStack(dataToStateGetter.apply(data));
}
@Override
public void detectAndSendChanges(boolean init) {}
// Slot sync is handled differently from other DataFollowers,
// so we need to also sync slot content directly to server.
@Override
public ClickResult onClick(int buttonId, boolean doubleClick) {
if (interactionDisabled) return ClickResult.REJECT;
if (isPhantom()) {
ClickData clickData = ClickData.create(buttonId, doubleClick);
syncToServer(2, clickData::writeToPacket);
phantomClick(clickData);
dataSetter.accept(getMcSlot().getStack());
return ClickResult.ACCEPT;
}
return ClickResult.REJECT;
}
@Override
public boolean onMouseScroll(int direction) {
if (interactionDisabled) return false;
if (isPhantom()) {
if (Interactable.hasShiftDown()) {
direction *= 8;
}
final int finalDirection = direction;
syncToServer(3, buffer -> buffer.writeVarIntToBuffer(finalDirection));
phantomScroll(finalDirection);
dataSetter.accept(getMcSlot().getStack());
return true;
}
return false;
}
@Override
public boolean handleDragAndDrop(ItemStack draggedStack, int button) {
if (interactionDisabled) return false;
if (!isPhantom()) return false;
ClickData clickData = ClickData.create(button, false);
syncToServer(5, buffer -> {
clickData.writeToPacket(buffer);
NetworkUtils.writeItemStack(buffer, draggedStack);
});
phantomClick(clickData, draggedStack);
dataSetter.accept(getMcSlot().getStack());
draggedStack.stackSize = 0;
return true;
}
}
|