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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
|
package io.github.cottonmc.cotton.gui;
import java.util.ArrayList;
import javax.annotation.Nullable;
import io.github.cottonmc.cotton.gui.client.BackgroundPainter;
import io.github.cottonmc.cotton.gui.client.LibGuiClient;
import io.github.cottonmc.cotton.gui.widget.*;
import net.fabricmc.api.EnvType;
import net.fabricmc.api.Environment;
import net.minecraft.block.Block;
import net.minecraft.block.BlockState;
import net.minecraft.block.InventoryProvider;
import net.minecraft.block.entity.BlockEntity;
import net.minecraft.container.ArrayPropertyDelegate;
import net.minecraft.container.BlockContext;
import net.minecraft.container.CraftingContainer;
import net.minecraft.container.PropertyDelegate;
import net.minecraft.container.Slot;
import net.minecraft.container.SlotActionType;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.entity.player.PlayerInventory;
import net.minecraft.inventory.Inventory;
import net.minecraft.item.ItemStack;
import net.minecraft.recipe.Recipe;
import net.minecraft.recipe.RecipeFinder;
import net.minecraft.recipe.RecipeInputProvider;
import net.minecraft.recipe.RecipeType;
import net.minecraft.world.World;
public class CottonCraftingController extends CraftingContainer<Inventory> implements GuiDescription {
protected Inventory blockInventory;
protected PlayerInventory playerInventory;
protected RecipeType<?> recipeType;
protected World world;
protected PropertyDelegate propertyDelegate;
protected WPanel rootPanel = new WGridPanel();
protected int titleColor = WLabel.DEFAULT_TEXT_COLOR;
protected int darkTitleColor = WLabel.DEFAULT_DARKMODE_TEXT_COLOR;
protected WWidget focus;
public CottonCraftingController(RecipeType<?> recipeType, int syncId, PlayerInventory playerInventory) {
super(null, syncId);
this.blockInventory = null;
this.playerInventory = playerInventory;
this.recipeType = recipeType;
this.world = playerInventory.player.world;
this.propertyDelegate = null;//new ArrayPropertyDelegate(1);
}
public CottonCraftingController(RecipeType<?> recipeType, int syncId, PlayerInventory playerInventory, Inventory blockInventory, PropertyDelegate propertyDelegate) {
super(null, syncId);
this.blockInventory = blockInventory;
this.playerInventory = playerInventory;
this.recipeType = recipeType;
this.world = playerInventory.player.world;
this.propertyDelegate = propertyDelegate;
if (propertyDelegate!=null && propertyDelegate.size()>0) this.addProperties(propertyDelegate);
}
public WPanel getRootPanel() {
return rootPanel;
}
public int getTitleColor() {
return LibGuiClient.config.darkMode ? darkTitleColor : titleColor;
}
public CottonCraftingController setRootPanel(WPanel panel) {
this.rootPanel = panel;
return this;
}
public CottonCraftingController setTitleColor(int color) {
this.titleColor = color;
return this;
}
@Environment(EnvType.CLIENT)
public void addPainters() {
if (this.rootPanel!=null) {
this.rootPanel.setBackgroundPainter(BackgroundPainter.VANILLA_9PATCH);
}
}
public void addSlotPeer(ValidatedSlot slot) {
this.addSlot(slot);
}
@Override
public ItemStack onSlotClick(int slotNumber, int button, SlotActionType action, PlayerEntity player) {
if (action==SlotActionType.QUICK_MOVE) {
if (slotNumber < 0) {
return ItemStack.EMPTY;
}
if (slotNumber>=this.slots.size()) return ItemStack.EMPTY;
Slot slot = this.slots.get(slotNumber);
if (slot == null || !slot.canTakeItems(player)) {
return ItemStack.EMPTY;
}
ItemStack remaining = ItemStack.EMPTY;
if (slot != null && slot.hasStack()) {
ItemStack toTransfer = slot.getStack();
remaining = toTransfer.copy();
//if (slot.inventory==blockInventory) {
if (blockInventory!=null) {
if (slot.inventory==blockInventory) {
//Try to transfer the item from the block into the player's inventory
if (!this.insertItem(toTransfer, this.playerInventory, true)) {
return ItemStack.EMPTY;
}
} else if (!this.insertItem(toTransfer, this.blockInventory, false)) { //Try to transfer the item from the player to the block
return ItemStack.EMPTY;
}
} else {
//There's no block, just swap between the player's storage and their hotbar
if (!swapHotbar(toTransfer, slotNumber, this.playerInventory)) {
return ItemStack.EMPTY;
}
}
if (toTransfer.isEmpty()) {
slot.setStack(ItemStack.EMPTY);
} else {
slot.markDirty();
}
}
return remaining;
} else {
return super.onSlotClick(slotNumber, button, action, player);
}
}
/** WILL MODIFY toInsert! Returns true if anything was inserted. */
private boolean insertIntoExisting(ItemStack toInsert, Slot slot) {
ItemStack curSlotStack = slot.getStack();
if (!curSlotStack.isEmpty() && canStacksCombine(toInsert, curSlotStack)) {
int combinedAmount = curSlotStack.getCount() + toInsert.getCount();
if (combinedAmount <= toInsert.getMaxCount()) {
toInsert.setCount(0);
curSlotStack.setCount(combinedAmount);
slot.markDirty();
return true;
} else if (curSlotStack.getCount() < toInsert.getMaxCount()) {
toInsert.decrement(toInsert.getMaxCount() - curSlotStack.getCount());
curSlotStack.setCount(toInsert.getMaxCount());
slot.markDirty();
return true;
}
}
return false;
}
/** WILL MODIFY toInsert! Returns true if anything was inserted. */
private boolean insertIntoEmpty(ItemStack toInsert, Slot slot) {
ItemStack curSlotStack = slot.getStack();
if (curSlotStack.isEmpty() && slot.canInsert(toInsert)) {
if (toInsert.getCount() > slot.getMaxStackAmount()) {
slot.setStack(toInsert.split(slot.getMaxStackAmount()));
} else {
slot.setStack(toInsert.split(toInsert.getCount()));
}
slot.markDirty();
return true;
}
return false;
}
private boolean insertItem(ItemStack toInsert, Inventory inventory, boolean walkBackwards) {
//Make a unified list of slots *only from this inventory*
ArrayList<Slot> inventorySlots = new ArrayList<>();
for(Slot slot : slots) {
if (slot.inventory==inventory) inventorySlots.add(slot);
}
if (inventorySlots.isEmpty()) return false;
//Try to insert it on top of existing stacks
boolean inserted = false;
if (walkBackwards) {
for(int i=inventorySlots.size()-1; i>=0; i--) {
Slot curSlot = inventorySlots.get(i);
if (insertIntoExisting(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
} else {
for(int i=0; i<inventorySlots.size(); i++) {
Slot curSlot = inventorySlots.get(i);
if (insertIntoExisting(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
}
//If we still have any, shove them into empty slots
if (!toInsert.isEmpty()) {
if (walkBackwards) {
for(int i=inventorySlots.size()-1; i>=0; i--) {
Slot curSlot = inventorySlots.get(i);
if (insertIntoEmpty(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
} else {
for(int i=0; i<inventorySlots.size(); i++) {
Slot curSlot = inventorySlots.get(i);
if (insertIntoEmpty(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
}
}
return inserted;
}
private boolean swapHotbar(ItemStack toInsert, int slotNumber, Inventory inventory) {
//Feel out the slots to see what's storage versus hotbar
ArrayList<Slot> storageSlots = new ArrayList<>();
ArrayList<Slot> hotbarSlots = new ArrayList<>();
boolean swapToStorage = true;
boolean inserted = false;
for(Slot slot : slots) {
if (slot.inventory==inventory && slot instanceof ValidatedSlot) {
int index = ((ValidatedSlot)slot).getInventoryIndex();
if (PlayerInventory.isValidHotbarIndex(index)) {
hotbarSlots.add(slot);
} else {
storageSlots.add(slot);
if (index==slotNumber) swapToStorage = false;
}
}
}
if (storageSlots.isEmpty() || hotbarSlots.isEmpty()) return false;
if (swapToStorage) {
//swap from hotbar to storage
for(int i=0; i<storageSlots.size(); i++) {
Slot curSlot = storageSlots.get(i);
if (insertIntoExisting(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
if (!toInsert.isEmpty()) {
for(int i=0; i<storageSlots.size(); i++) {
Slot curSlot = storageSlots.get(i);
if (insertIntoEmpty(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
}
} else {
//swap from storage to hotbar
for(int i=0; i<hotbarSlots.size(); i++) {
Slot curSlot = hotbarSlots.get(i);
if (insertIntoExisting(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
if (!toInsert.isEmpty()) {
for(int i=0; i<hotbarSlots.size(); i++) {
Slot curSlot = hotbarSlots.get(i);
if (insertIntoEmpty(toInsert, curSlot)) inserted = true;
if (toInsert.isEmpty()) break;
}
}
}
return inserted;
}
@Nullable
public WWidget doMouseUp(int x, int y, int state) {
if (rootPanel!=null) return rootPanel.onMouseUp(x, y, state);
return null;
}
@Nullable
public WWidget doMouseDown(int x, int y, int button) {
if (rootPanel!=null) return rootPanel.onMouseDown(x, y, button);
return null;
}
public void doMouseDrag(int x, int y, int button, double deltaX, double deltaY) {
if (rootPanel!=null) rootPanel.onMouseDrag(x, y, button, deltaX, deltaY);
}
public void doClick(int x, int y, int button) {
if (focus!=null) {
int wx = focus.getAbsoluteX();
int wy = focus.getAbsoluteY();
if (x>=wx && x<wx+focus.getWidth() && y>=wy && y<wy+focus.getHeight()) {
//Do nothing, focus will get the click soon
} else {
//Invalidate the component first
WWidget lastFocus = focus;
focus = null;
lastFocus.onFocusLost();
}
}
//if (rootPanel!=null) rootPanel.onClick(x, y, button);
}
public void doCharType(char ch) {
if (focus!=null) focus.onCharTyped(ch);
}
//public void doKeyPress(int key) {
// if (focus!=null) focus.onKeyPressed(key);
//}
//public void doKeyRelease(int key) {
// if (focus!=null) focus.onKeyReleased(key);
//}
@Nullable
@Override
public PropertyDelegate getPropertyDelegate() {
return propertyDelegate;
}
@Override
public GuiDescription setPropertyDelegate(PropertyDelegate delegate) {
this.propertyDelegate = delegate;
return this;
}
public WPlayerInvPanel createPlayerInventoryPanel() {
return new WPlayerInvPanel(this.playerInventory);
}
public static Inventory getBlockInventory(BlockContext ctx) {
return ctx.run((world, pos) -> {
BlockState state = world.getBlockState(pos);
Block b = state.getBlock();
if (b instanceof InventoryProvider) {
return ((InventoryProvider)b).getInventory(state, world, pos);
}
BlockEntity be = world.getBlockEntity(pos);
if (be!=null) {
if (be instanceof InventoryProvider) {
return ((InventoryProvider)be).getInventory(state, world, pos);
} else if (be instanceof Inventory) {
return (Inventory)be;
}
}
return EmptyInventory.INSTANCE;
}).orElse(EmptyInventory.INSTANCE);
}
public static PropertyDelegate getBlockPropertyDelegate(BlockContext ctx) {
return ctx.run((world, pos) -> {
BlockState state = world.getBlockState(pos);
Block block = state.getBlock();
if (block instanceof PropertyDelegateHolder) {
return ((PropertyDelegateHolder)block).getPropertyDelegate();
}
BlockEntity be = world.getBlockEntity(pos);
if (be!=null && be instanceof PropertyDelegateHolder) {
return ((PropertyDelegateHolder)be).getPropertyDelegate();
}
return new ArrayPropertyDelegate(0);
}).orElse(new ArrayPropertyDelegate(0));
}
//extends CraftingContainer<Inventory> {
@Override
public void populateRecipeFinder(RecipeFinder recipeFinder) {
if (this.blockInventory instanceof RecipeInputProvider) {
((RecipeInputProvider)this.blockInventory).provideRecipeInputs(recipeFinder);
}
}
@Override
public void clearCraftingSlots() {
if (this.blockInventory!=null) this.blockInventory.clear();
}
@Override
public boolean matches(Recipe<? super Inventory> recipe) {
if (blockInventory==null || world==null) return false;
return false; //TODO recipe support
}
@Override
public int getCraftingResultSlotIndex() {
return -1;
}
@Override
public int getCraftingWidth() {
return 1;
}
@Override
public int getCraftingHeight() {
return 1;
}
@Override
@Environment(EnvType.CLIENT)
public int getCraftingSlotCount() {
return 1;
}
//(implied) extends Container {
@Override
public boolean canUse(PlayerEntity entity) {
return (blockInventory!=null) ? blockInventory.canPlayerUseInv(entity) : true;
}
//}
//}
@Override
public boolean isFocused(WWidget widget) {
return focus == widget;
}
@Override
public WWidget getFocus() {
return focus;
}
@Override
public void requestFocus(WWidget widget) {
//TODO: Are there circumstances where focus can't be stolen?
if (focus==widget) return; //Nothing happens if we're already focused
if (!widget.canFocus()) return; //This is kind of a gotcha but needs to happen
if (focus!=null) focus.onFocusLost();
focus = widget;
focus.onFocusGained();
}
@Override
public void releaseFocus(WWidget widget) {
if (focus==widget) {
focus = null;
widget.onFocusLost();
}
}
}
|