-
Notifications
You must be signed in to change notification settings - Fork 214
Expand file tree
/
Copy pathCraftingInputSlot.java
More file actions
229 lines (191 loc) · 7.89 KB
/
CraftingInputSlot.java
File metadata and controls
229 lines (191 loc) · 7.89 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package gregtech.common.mui.widget.workbench;
import gregtech.api.util.GTUtility;
import gregtech.api.util.JEIUtil;
import gregtech.client.utils.RenderUtil;
import gregtech.common.metatileentities.storage.CraftingRecipeLogic;
import net.minecraft.item.ItemStack;
import net.minecraft.network.PacketBuffer;
import net.minecraftforge.items.IItemHandlerModifiable;
import net.minecraftforge.items.ItemHandlerHelper;
import com.cleanroommc.modularui.api.widget.IGuiAction;
import com.cleanroommc.modularui.api.widget.Interactable;
import com.cleanroommc.modularui.integration.jei.JeiGhostIngredientSlot;
import com.cleanroommc.modularui.integration.jei.JeiIngredientProvider;
import com.cleanroommc.modularui.network.NetworkUtils;
import com.cleanroommc.modularui.screen.RichTooltip;
import com.cleanroommc.modularui.screen.viewport.ModularGuiContext;
import com.cleanroommc.modularui.theme.WidgetTheme;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.value.sync.SyncHandler;
import com.cleanroommc.modularui.widget.Widget;
import com.cleanroommc.modularui.widgets.slot.IOnSlotChanged;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public class CraftingInputSlot extends Widget<CraftingOutputSlot> implements Interactable,
JeiGhostIngredientSlot<ItemStack>,
JeiIngredientProvider {
private final InputSyncHandler syncHandler;
public boolean hasIngredients = true;
private static boolean dragging = false;
private CraftingInputSlot(IItemHandlerModifiable handler, int index) {
this.syncHandler = new InputSyncHandler(handler, index);
setSyncHandler(this.syncHandler);
tooltipAutoUpdate(true);
tooltipBuilder(tooltip -> {
if (!isSynced()) return;
ItemStack stack = this.syncHandler.getStack();
if (stack.isEmpty()) return;
tooltip.addFromItem(stack);
});
// for hovering with items in hand
listenGuiAction((IGuiAction.MouseDrag) (m, t) -> {
if (isHovering() && dragging && syncHandler.isValid()) {
var player = syncHandler.getSyncManager().getCursorItem();
if (!ItemHandlerHelper.canItemStacksStack(player, getStack()))
syncHandler.syncStack();
return true;
}
return false;
});
// dragging has stopped
listenGuiAction((IGuiAction.MouseReleased) mouseButton -> {
dragging = false;
return true;
});
}
public static CraftingInputSlot create(CraftingRecipeLogic logic, IItemHandlerModifiable handler, int index) {
var slot = new CraftingInputSlot(handler, index);
logic.setInputSlot(slot, index);
return slot;
}
@Override
public boolean isValidSyncHandler(SyncHandler syncHandler) {
return syncHandler instanceof InputSyncHandler;
}
@Override
public void onInit() {
getContext().getJeiSettings().addJeiGhostIngredientSlot(this);
}
public CraftingInputSlot changeListener(IOnSlotChanged listener) {
this.syncHandler.listener = listener;
return this;
}
@NotNull
@Override
public Result onMousePressed(int mouseButton) {
if (!this.syncHandler.isValid() || dragging)
return Result.IGNORE;
this.syncHandler.syncStack();
return Result.SUCCESS;
}
@Override
public void onMouseDrag(int mouseButton, long timeSinceClick) {
if (!dragging && timeSinceClick > 100) {
dragging = true;
}
}
@Override
public void draw(ModularGuiContext context, WidgetTheme widgetTheme) {
ItemStack itemstack = this.syncHandler.getStack();
boolean jeiIngredientBeingHovered = JEIUtil.hoveringOverIngredient(this);
if (!itemstack.isEmpty()) {
if (!jeiIngredientBeingHovered && !this.hasIngredients) {
RenderUtil.renderRect(0, 0, 18, 18, 200, 0x80FF0000);
}
RenderUtil.renderItem(itemstack, 1, 1, 16, 16);
}
if (jeiIngredientBeingHovered) {
RenderUtil.drawJEIGhostSlotOverlay(this);
} else {
RenderUtil.handleSlotOverlay(this, widgetTheme);
}
}
@Override
public void drawForeground(ModularGuiContext context) {
RichTooltip tooltip = getTooltip();
if (tooltip != null && isHoveringFor(tooltip.getShowUpTimer())) {
tooltip.draw(getContext(), this.syncHandler.getStack());
}
}
@Override
public void setGhostIngredient(@NotNull ItemStack ingredient) {
syncHandler.setStack(ingredient, true);
}
@Override
public @Nullable ItemStack castGhostIngredientIfValid(@NotNull Object ingredient) {
ingredient = JEIUtil.getBookStackIfEnchantment(ingredient);
return areAncestorsEnabled() && ingredient instanceof ItemStack ? (ItemStack) ingredient : null;
}
@Override
public @NotNull ItemStack getIngredient() {
return this.getStack();
}
public ItemStack getStack() {
return syncHandler.getStack();
}
public int getIndex() {
return syncHandler.index;
}
public void setStack(ItemStack stack) {
this.syncHandler.setStack(stack, true);
}
protected static class InputSyncHandler extends SyncHandler {
public static final int SLOT_CHANGED = 1;
private final IItemHandlerModifiable handler;
private final int index;
private ItemStack lastStoredItem;
private IOnSlotChanged listener = IOnSlotChanged.DEFAULT;
public InputSyncHandler(IItemHandlerModifiable handler, int index) {
this.handler = handler;
this.index = index;
}
@Override
public void init(String key, PanelSyncManager syncHandler) {
super.init(key, syncHandler);
this.lastStoredItem = getStack().copy();
}
@Override
public void readOnClient(int id, PacketBuffer buf) {
if (id == SLOT_CHANGED) {
var stack = NetworkUtils.readItemStack(buf);
setStack(stack, false);
}
}
@Override
public void readOnServer(int id, PacketBuffer buf) {
if (id == SLOT_CHANGED) {
var stack = NetworkUtils.readItemStack(buf);
this.setStack(stack, false);
}
}
@Override
public void detectAndSendChanges(boolean init) {
ItemStack itemStack = getStack();
if (itemStack.isEmpty() && this.lastStoredItem.isEmpty()) return;
if (init || !ItemHandlerHelper.canItemStacksStack(this.lastStoredItem, itemStack)) {
this.listener.onChange(itemStack, false, false, init);
this.lastStoredItem = itemStack.isEmpty() ? ItemStack.EMPTY : itemStack.copy();
syncToClient(SLOT_CHANGED, buffer -> NetworkUtils.writeItemStack(buffer, itemStack));
}
}
public void syncStack() {
final var cursorStack = GTUtility.copy(1, getSyncManager().getCursorItem());
setStack(cursorStack, true);
}
public ItemStack getStack() {
return this.handler.getStackInSlot(this.index);
}
/**
* Sets the stack in this slot and calls the onChange listener.
*
* @param stack stack to put into this slot
*/
public void setStack(ItemStack stack, boolean sync) {
var old = getStack();
boolean onlyAmt = ItemHandlerHelper.canItemStacksStackRelaxed(stack, old);
this.handler.setStackInSlot(this.index, stack);
this.listener.onChange(stack, onlyAmt, getSyncManager().isClient(), false);
if (sync) syncToServer(SLOT_CHANGED, buffer -> NetworkUtils.writeItemStack(buffer, getStack()));
}
}
}