-
Notifications
You must be signed in to change notification settings - Fork 211
Expand file tree
/
Copy pathCoverConveyor.java
More file actions
748 lines (645 loc) · 31.2 KB
/
Copy pathCoverConveyor.java
File metadata and controls
748 lines (645 loc) · 31.2 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
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
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
package gregtech.common.covers;
import gregtech.api.capability.GregtechDataCodes;
import gregtech.api.capability.GregtechTileCapabilities;
import gregtech.api.capability.IControllable;
import gregtech.api.capability.impl.ItemHandlerDelegate;
import gregtech.api.cover.CoverBase;
import gregtech.api.cover.CoverDefinition;
import gregtech.api.cover.CoverWithUI;
import gregtech.api.cover.CoverableView;
import gregtech.api.mui.GTGuiTextures;
import gregtech.api.util.GTTransferUtils;
import gregtech.api.util.ItemStackHashStrategy;
import gregtech.client.renderer.texture.Textures;
import gregtech.client.renderer.texture.cube.SimpleSidedCubeRenderer;
import gregtech.common.covers.filter.ItemFilterContainer;
import gregtech.common.pipelike.itempipe.tile.TileEntityItemPipe;
import net.minecraft.client.renderer.texture.TextureAtlasSprite;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.network.PacketBuffer;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.BlockRenderLayer;
import net.minecraft.util.EnumActionResult;
import net.minecraft.util.EnumFacing;
import net.minecraft.util.EnumHand;
import net.minecraft.util.IStringSerializable;
import net.minecraft.util.ITickable;
import net.minecraft.util.math.MathHelper;
import net.minecraftforge.common.capabilities.Capability;
import net.minecraftforge.fml.relauncher.Side;
import net.minecraftforge.fml.relauncher.SideOnly;
import net.minecraftforge.items.CapabilityItemHandler;
import net.minecraftforge.items.IItemHandler;
import codechicken.lib.raytracer.CuboidRayTraceResult;
import codechicken.lib.render.CCRenderState;
import codechicken.lib.render.pipeline.IVertexOperation;
import codechicken.lib.vec.Cuboid6;
import codechicken.lib.vec.Matrix4;
import com.cleanroommc.modularui.api.drawable.IDrawable;
import com.cleanroommc.modularui.drawable.DynamicDrawable;
import com.cleanroommc.modularui.factory.SidedPosGuiData;
import com.cleanroommc.modularui.screen.ModularPanel;
import com.cleanroommc.modularui.utils.Color;
import com.cleanroommc.modularui.utils.MouseData;
import com.cleanroommc.modularui.value.sync.EnumSyncValue;
import com.cleanroommc.modularui.value.sync.IntSyncValue;
import com.cleanroommc.modularui.value.sync.PanelSyncManager;
import com.cleanroommc.modularui.value.sync.StringSyncValue;
import com.cleanroommc.modularui.widget.ParentWidget;
import com.cleanroommc.modularui.widgets.ButtonWidget;
import com.cleanroommc.modularui.widgets.layout.Flow;
import com.cleanroommc.modularui.widgets.textfield.TextFieldWidget;
import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap;
import it.unimi.dsi.fastutil.ints.IntArrayList;
import it.unimi.dsi.fastutil.ints.IntList;
import it.unimi.dsi.fastutil.objects.Object2ObjectOpenCustomHashMap;
import it.unimi.dsi.fastutil.objects.ObjectOpenCustomHashSet;
import org.jetbrains.annotations.NotNull;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
public class CoverConveyor extends CoverBase implements CoverWithUI, ITickable, IControllable {
public final int tier;
public final int maxItemTransferRate;
private int transferRate;
protected ConveyorMode conveyorMode;
protected DistributionMode distributionMode;
protected ManualImportExportMode manualImportExportMode = ManualImportExportMode.DISABLED;
protected final ItemFilterContainer itemFilterContainer;
protected int itemsLeftToTransferLastSecond;
private CoverableItemHandlerWrapper itemHandlerWrapper;
protected boolean isWorkingAllowed = true;
public CoverConveyor(@NotNull CoverDefinition definition, @NotNull CoverableView coverableView,
@NotNull EnumFacing attachedSide, int tier, int itemsPerSecond) {
super(definition, coverableView, attachedSide);
this.tier = tier;
this.maxItemTransferRate = itemsPerSecond;
this.transferRate = maxItemTransferRate;
this.itemsLeftToTransferLastSecond = transferRate;
this.conveyorMode = ConveyorMode.EXPORT;
this.distributionMode = DistributionMode.INSERT_FIRST;
this.itemFilterContainer = new ItemFilterContainer(this);
}
public void setTransferRate(int transferRate) {
this.transferRate = MathHelper.clamp(transferRate, 1, maxItemTransferRate);
CoverableView coverable = getCoverableView();
coverable.markDirty();
if (getWorld() != null && getWorld().isRemote) {
// tile at cover holder pos
TileEntity te = getTileEntityHere();
if (te instanceof TileEntityItemPipe) {
((TileEntityItemPipe) te).resetTransferred();
}
// tile neighbour to holder pos at attached side
te = getNeighbor(getAttachedSide());
if (te instanceof TileEntityItemPipe) {
((TileEntityItemPipe) te).resetTransferred();
}
}
}
public int getTransferRate() {
return transferRate;
}
protected void adjustTransferRate(int amount) {
setTransferRate(MathHelper.clamp(transferRate + amount, 1, maxItemTransferRate));
}
public void setConveyorMode(ConveyorMode conveyorMode) {
this.conveyorMode = conveyorMode;
writeCustomData(GregtechDataCodes.UPDATE_COVER_MODE, buf -> buf.writeEnumValue(conveyorMode));
markDirty();
}
public ConveyorMode getConveyorMode() {
return conveyorMode;
}
public DistributionMode getDistributionMode() {
return distributionMode;
}
public void setDistributionMode(DistributionMode distributionMode) {
this.distributionMode = distributionMode;
markDirty();
}
public ManualImportExportMode getManualImportExportMode() {
return manualImportExportMode;
}
protected void setManualImportExportMode(ManualImportExportMode manualImportExportMode) {
this.manualImportExportMode = manualImportExportMode;
markDirty();
}
public ItemFilterContainer getItemFilterContainer() {
return itemFilterContainer;
}
@Override
public void update() {
CoverableView coverable = getCoverableView();
long timer = coverable.getOffsetTimer();
if (timer % 5 == 0 && isWorkingAllowed && itemsLeftToTransferLastSecond > 0) {
EnumFacing side = getAttachedSide();
TileEntity tileEntity = coverable.getNeighbor(side);
IItemHandler itemHandler = tileEntity == null ? null :
tileEntity.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side.getOpposite());
IItemHandler myItemHandler = coverable.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, side);
if (itemHandler != null && myItemHandler != null) {
int totalTransferred = doTransferItems(itemHandler, myItemHandler, itemsLeftToTransferLastSecond);
this.itemsLeftToTransferLastSecond -= totalTransferred;
}
}
if (timer % 20 == 0) {
this.itemsLeftToTransferLastSecond = transferRate;
}
}
protected int doTransferItems(IItemHandler itemHandler, IItemHandler myItemHandler, int maxTransferAmount) {
return doTransferItemsAny(itemHandler, myItemHandler, maxTransferAmount);
}
protected int doTransferItemsAny(IItemHandler itemHandler, IItemHandler myItemHandler, int maxTransferAmount) {
if (conveyorMode == ConveyorMode.IMPORT) {
return moveInventoryItems(itemHandler, myItemHandler, maxTransferAmount);
} else if (conveyorMode == ConveyorMode.EXPORT) {
return moveInventoryItems(myItemHandler, itemHandler, maxTransferAmount);
}
return 0;
}
protected int doTransferItemsByGroup(IItemHandler itemHandler, IItemHandler myItemHandler,
Map<Integer, GroupItemInfo> itemInfos, int maxTransferAmount) {
if (conveyorMode == ConveyorMode.IMPORT) {
return moveInventoryItems(itemHandler, myItemHandler, itemInfos, maxTransferAmount);
} else if (conveyorMode == ConveyorMode.EXPORT) {
return moveInventoryItems(myItemHandler, itemHandler, itemInfos, maxTransferAmount);
}
return 0;
}
protected Map<Integer, GroupItemInfo> doCountDestinationInventoryItemsByMatchIndex(IItemHandler itemHandler,
IItemHandler myItemHandler) {
if (conveyorMode == ConveyorMode.IMPORT) {
return countInventoryItemsByMatchSlot(myItemHandler);
} else if (conveyorMode == ConveyorMode.EXPORT) {
return countInventoryItemsByMatchSlot(itemHandler);
}
return Collections.emptyMap();
}
protected Map<ItemStack, TypeItemInfo> doCountSourceInventoryItemsByType(IItemHandler itemHandler,
IItemHandler myItemHandler) {
if (conveyorMode == ConveyorMode.IMPORT) {
return countInventoryItemsByType(itemHandler);
} else if (conveyorMode == ConveyorMode.EXPORT) {
return countInventoryItemsByType(myItemHandler);
}
return Collections.emptyMap();
}
protected boolean doTransferItemsExact(IItemHandler itemHandler, IItemHandler myItemHandler,
TypeItemInfo itemInfo) {
if (conveyorMode == ConveyorMode.IMPORT) {
return moveInventoryItemsExact(itemHandler, myItemHandler, itemInfo);
} else if (conveyorMode == ConveyorMode.EXPORT) {
return moveInventoryItemsExact(myItemHandler, itemHandler, itemInfo);
}
return false;
}
protected static boolean moveInventoryItemsExact(IItemHandler sourceInventory, IItemHandler targetInventory,
TypeItemInfo itemInfo) {
// first, compute how much can we extract in reality from the machine,
// because totalCount is based on what getStackInSlot returns, which may differ from what
// extractItem() will return
ItemStack resultStack = itemInfo.itemStack.copy();
int totalExtractedCount = 0;
int itemsLeftToExtract = itemInfo.totalCount;
for (int i = 0; i < itemInfo.slots.size(); i++) {
int slotIndex = itemInfo.slots.get(i);
ItemStack extractedStack = sourceInventory.extractItem(slotIndex, itemsLeftToExtract, true);
if (!extractedStack.isEmpty() &&
ItemStack.areItemsEqual(resultStack, extractedStack) &&
ItemStack.areItemStackTagsEqual(resultStack, extractedStack)) {
totalExtractedCount += extractedStack.getCount();
itemsLeftToExtract -= extractedStack.getCount();
}
if (itemsLeftToExtract == 0) {
break;
}
}
// if amount of items extracted is not equal to the amount of items we
// wanted to extract, abort item extraction
if (totalExtractedCount != itemInfo.totalCount) {
return false;
}
// adjust size of the result stack accordingly
resultStack.setCount(totalExtractedCount);
// now, see how much we can insert into destination inventory
// if we can't insert as much as itemInfo requires, and remainder is empty, abort, abort
ItemStack remainder = GTTransferUtils.insertItem(targetInventory, resultStack, true);
if (!remainder.isEmpty()) {
return false;
}
// otherwise, perform real insertion and then remove items from the source inventory
GTTransferUtils.insertItem(targetInventory, resultStack, false);
// perform real extraction of the items from the source inventory now
itemsLeftToExtract = itemInfo.totalCount;
for (int i = 0; i < itemInfo.slots.size(); i++) {
int slotIndex = itemInfo.slots.get(i);
ItemStack extractedStack = sourceInventory.extractItem(slotIndex, itemsLeftToExtract, false);
if (!extractedStack.isEmpty() &&
ItemStack.areItemsEqual(resultStack, extractedStack) &&
ItemStack.areItemStackTagsEqual(resultStack, extractedStack)) {
itemsLeftToExtract -= extractedStack.getCount();
}
if (itemsLeftToExtract == 0) {
break;
}
}
return true;
}
protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targetInventory,
Map<Integer, GroupItemInfo> itemInfos, int maxTransferAmount) {
int itemsLeftToTransfer = maxTransferAmount;
for (int i = 0; i < sourceInventory.getSlots(); i++) {
ItemStack itemStack = sourceInventory.getStackInSlot(i);
if (itemStack.isEmpty()) {
continue;
}
var matchResult = itemFilterContainer.match(itemStack);
int matchSlotIndex = matchResult.getFilterIndex();
if (!matchResult.isMatched() || !itemInfos.containsKey(matchSlotIndex)) {
continue;
}
GroupItemInfo itemInfo = itemInfos.get(matchSlotIndex);
ItemStack extractedStack = sourceInventory.extractItem(i,
Math.min(itemInfo.totalCount, itemsLeftToTransfer), true);
ItemStack remainderStack = GTTransferUtils.insertItem(targetInventory, extractedStack, true);
int amountToInsert = extractedStack.getCount() - remainderStack.getCount();
if (amountToInsert > 0) {
extractedStack = sourceInventory.extractItem(i, amountToInsert, false);
if (!extractedStack.isEmpty()) {
GTTransferUtils.insertItem(targetInventory, extractedStack, false);
itemsLeftToTransfer -= extractedStack.getCount();
itemInfo.totalCount -= extractedStack.getCount();
if (itemInfo.totalCount == 0) {
itemInfos.remove(matchSlotIndex);
if (itemInfos.isEmpty()) {
break;
}
}
if (itemsLeftToTransfer == 0) {
break;
}
}
}
}
return maxTransferAmount - itemsLeftToTransfer;
}
protected int moveInventoryItems(IItemHandler sourceInventory, IItemHandler targetInventory,
int maxTransferAmount) {
int itemsLeftToTransfer = maxTransferAmount;
for (int srcIndex = 0; srcIndex < sourceInventory.getSlots(); srcIndex++) {
ItemStack sourceStack = sourceInventory.extractItem(srcIndex, itemsLeftToTransfer, true);
if (sourceStack.isEmpty()) {
continue;
}
var result = itemFilterContainer.match(sourceStack);
if (!result.isMatched()) continue;
ItemStack remainder = GTTransferUtils.insertItem(targetInventory, sourceStack, true);
int amountToInsert = sourceStack.getCount() - remainder.getCount();
if (amountToInsert > 0) {
sourceStack = sourceInventory.extractItem(srcIndex, amountToInsert, false);
if (!sourceStack.isEmpty()) {
GTTransferUtils.insertItem(targetInventory, sourceStack, false);
itemsLeftToTransfer -= sourceStack.getCount();
if (itemsLeftToTransfer == 0) {
break;
}
}
}
}
return maxTransferAmount - itemsLeftToTransfer;
}
protected static class TypeItemInfo {
public final ItemStack itemStack;
public final int filterSlot;
public final IntList slots;
public int totalCount;
public TypeItemInfo(ItemStack itemStack, int filterSlot, IntList slots, int totalCount) {
this.itemStack = itemStack;
this.filterSlot = filterSlot;
this.slots = slots;
this.totalCount = totalCount;
}
}
protected static class GroupItemInfo {
public final int filterSlot;
public final Set<ItemStack> itemStackTypes;
public int totalCount;
public GroupItemInfo(int filterSlot, Set<ItemStack> itemStackTypes, int totalCount) {
this.filterSlot = filterSlot;
this.itemStackTypes = itemStackTypes;
this.totalCount = totalCount;
}
}
@NotNull
protected Map<ItemStack, TypeItemInfo> countInventoryItemsByType(@NotNull IItemHandler inventory) {
Map<ItemStack, TypeItemInfo> result = new Object2ObjectOpenCustomHashMap<>(
ItemStackHashStrategy.comparingAllButCount());
for (int srcIndex = 0; srcIndex < inventory.getSlots(); srcIndex++) {
ItemStack itemStack = inventory.getStackInSlot(srcIndex);
if (itemStack.isEmpty()) {
continue;
}
var matchResult = itemFilterContainer.match(itemStack);
if (!matchResult.isMatched()) continue;
if (!result.containsKey(itemStack)) {
TypeItemInfo itemInfo = new TypeItemInfo(itemStack.copy(), matchResult.getFilterIndex(),
new IntArrayList(), 0);
itemInfo.totalCount += itemStack.getCount();
itemInfo.slots.add(srcIndex);
result.put(itemStack.copy(), itemInfo);
} else {
TypeItemInfo itemInfo = result.get(itemStack);
itemInfo.totalCount += itemStack.getCount();
itemInfo.slots.add(srcIndex);
}
}
return result;
}
@NotNull
protected Map<Integer, GroupItemInfo> countInventoryItemsByMatchSlot(@NotNull IItemHandler inventory) {
Map<Integer, GroupItemInfo> result = new Int2ObjectOpenHashMap<>();
for (int srcIndex = 0; srcIndex < inventory.getSlots(); srcIndex++) {
ItemStack itemStack = inventory.getStackInSlot(srcIndex);
if (itemStack.isEmpty()) {
continue;
}
var matchResult = itemFilterContainer.match(itemStack);
if (!matchResult.isMatched()) continue;
int matchedSlot = matchResult.getFilterIndex();
if (!result.containsKey(matchedSlot)) {
GroupItemInfo itemInfo = new GroupItemInfo(matchedSlot,
new ObjectOpenCustomHashSet<>(ItemStackHashStrategy.comparingAllButCount()), 0);
itemInfo.itemStackTypes.add(itemStack.copy());
itemInfo.totalCount += itemStack.getCount();
result.put(matchedSlot, itemInfo);
} else {
GroupItemInfo itemInfo = result.get(matchedSlot);
itemInfo.itemStackTypes.add(itemStack.copy());
itemInfo.totalCount += itemStack.getCount();
}
}
return result;
}
@Override
public boolean canAttach(@NotNull CoverableView coverable, @NotNull EnumFacing side) {
return coverable.getCapability(CapabilityItemHandler.ITEM_HANDLER_CAPABILITY, getAttachedSide()) != null;
}
@Override
public boolean canInteractWithOutputSide() {
return true;
}
@Override
public void onRemoval() {
dropInventoryContents(itemFilterContainer);
}
@Override
public void renderCover(CCRenderState renderState, Matrix4 translation, IVertexOperation[] pipeline,
Cuboid6 plateBox, BlockRenderLayer layer) {
if (conveyorMode == ConveyorMode.EXPORT) {
Textures.CONVEYOR_OVERLAY.renderSided(getAttachedSide(), plateBox, renderState, pipeline, translation);
} else {
Textures.CONVEYOR_OVERLAY_INVERTED.renderSided(getAttachedSide(), plateBox, renderState, pipeline,
translation);
}
}
@Override
public @NotNull EnumActionResult onScrewdriverClick(@NotNull EntityPlayer playerIn, @NotNull EnumHand hand,
@NotNull CuboidRayTraceResult hitResult) {
if (!getCoverableView().getWorld().isRemote) {
openUI((EntityPlayerMP) playerIn);
}
return EnumActionResult.SUCCESS;
}
@Override
public <T> T getCapability(Capability<T> capability, T defaultValue) {
if (capability == CapabilityItemHandler.ITEM_HANDLER_CAPABILITY) {
if (defaultValue == null) {
return null;
}
IItemHandler delegate = (IItemHandler) defaultValue;
if (itemHandlerWrapper == null || itemHandlerWrapper.delegate != delegate) {
this.itemHandlerWrapper = new CoverableItemHandlerWrapper(delegate);
}
return CapabilityItemHandler.ITEM_HANDLER_CAPABILITY.cast(itemHandlerWrapper);
}
if (capability == GregtechTileCapabilities.CAPABILITY_CONTROLLABLE) {
return GregtechTileCapabilities.CAPABILITY_CONTROLLABLE.cast(this);
}
return defaultValue;
}
@Override
public boolean usesMui2() {
return true;
}
@Override
public ModularPanel confgurePanel(ModularPanel panel, boolean isSmallGui) {
return panel.height(210);
}
public @NotNull ParentWidget<?> createUI(SidedPosGuiData data, PanelSyncManager guiSyncManager) {
getItemFilterContainer().setMaxTransferSize(getMaxStackSize());
var column = Flow.column().top(24).margin(7, 0)
.widthRel(1f).coverChildrenHeight();
EnumSyncValue<ManualImportExportMode> manualIOmode = new EnumSyncValue<>(ManualImportExportMode.class,
this::getManualImportExportMode, this::setManualImportExportMode);
EnumSyncValue<ConveyorMode> conveyorMode = new EnumSyncValue<>(ConveyorMode.class,
this::getConveyorMode, this::setConveyorMode);
IntSyncValue throughput = new IntSyncValue(this::getTransferRate, this::setTransferRate);
StringSyncValue formattedThroughput = new StringSyncValue(throughput::getStringValue,
throughput::setStringValue);
EnumSyncValue<DistributionMode> distributionMode = new EnumSyncValue<>(DistributionMode.class,
this::getDistributionMode, this::setDistributionMode);
guiSyncManager.syncValue("manual_io", manualIOmode);
guiSyncManager.syncValue("conveyor_mode", conveyorMode);
guiSyncManager.syncValue("distribution_mode", distributionMode);
guiSyncManager.syncValue("throughput", throughput);
if (createThroughputRow())
column.child(Flow.row().coverChildrenHeight()
.marginBottom(2).widthRel(1f)
.child(new ButtonWidget<>()
.left(0).width(18)
.onMousePressed(mouseButton -> {
int val = throughput.getValue() - getIncrementValue(MouseData.create(mouseButton));
throughput.setValue(val, true, true);
return true;
})
.onUpdateListener(w -> w.overlay(createAdjustOverlay(false))))
.child(new TextFieldWidget()
.left(18).right(18)
.setTextColor(Color.WHITE.darker(1))
.setNumbers(1, maxItemTransferRate)
.value(formattedThroughput)
.background(GTGuiTextures.DISPLAY))
.child(new ButtonWidget<>()
.right(0).width(18)
.onMousePressed(mouseButton -> {
int val = throughput.getValue() + getIncrementValue(MouseData.create(mouseButton));
throughput.setValue(val, true, true);
return true;
})
.onUpdateListener(w -> w.overlay(createAdjustOverlay(true)))));
if (createFilterRow())
column.child(getItemFilterContainer().initUI(data, guiSyncManager));
if (createManualIOModeRow())
column.child(new EnumRowBuilder<>(ManualImportExportMode.class)
.value(manualIOmode)
.lang("cover.generic.manual_io")
.overlay(new IDrawable[] {
new DynamicDrawable(() -> conveyorMode.getValue().isImport() ?
GTGuiTextures.MANUAL_IO_OVERLAY_OUT[0] : GTGuiTextures.MANUAL_IO_OVERLAY_IN[0]),
new DynamicDrawable(() -> conveyorMode.getValue().isImport() ?
GTGuiTextures.MANUAL_IO_OVERLAY_OUT[1] : GTGuiTextures.MANUAL_IO_OVERLAY_IN[1]),
new DynamicDrawable(() -> conveyorMode.getValue().isImport() ?
GTGuiTextures.MANUAL_IO_OVERLAY_OUT[2] : GTGuiTextures.MANUAL_IO_OVERLAY_IN[2])
})
.build());
if (createConveyorModeRow())
column.child(new EnumRowBuilder<>(ConveyorMode.class)
.value(conveyorMode)
.lang("cover.generic.io")
.overlay(GTGuiTextures.CONVEYOR_MODE_OVERLAY)
.build());
if (createDistributionModeRow())
column.child(new EnumRowBuilder<>(DistributionMode.class)
.value(distributionMode)
.overlay(16, GTGuiTextures.DISTRIBUTION_MODE_OVERLAY)
.lang("cover.conveyor.distribution.name")
.build());
return column;
}
protected boolean createThroughputRow() {
return true;
}
protected boolean createFilterRow() {
return true;
}
protected boolean createManualIOModeRow() {
return true;
}
protected boolean createConveyorModeRow() {
return true;
}
protected boolean createDistributionModeRow() {
return true;
}
protected int getMaxStackSize() {
return 1;
}
@Override
public boolean isWorkingEnabled() {
return isWorkingAllowed;
}
@Override
public void setWorkingEnabled(boolean isActivationAllowed) {
this.isWorkingAllowed = isActivationAllowed;
}
@Override
public void readCustomData(int discriminator, @NotNull PacketBuffer buf) {
super.readCustomData(discriminator, buf);
if (discriminator == GregtechDataCodes.UPDATE_COVER_MODE) {
this.conveyorMode = buf.readEnumValue(ConveyorMode.class);
getCoverableView().scheduleRenderUpdate();
}
}
@Override
public void writeInitialSyncData(@NotNull PacketBuffer packetBuffer) {
super.writeInitialSyncData(packetBuffer);
packetBuffer.writeInt(transferRate);
packetBuffer.writeByte(conveyorMode.ordinal());
packetBuffer.writeByte(distributionMode.ordinal());
packetBuffer.writeByte(manualImportExportMode.ordinal());
getItemFilterContainer().writeInitialSyncData(packetBuffer);
}
@Override
public void readInitialSyncData(@NotNull PacketBuffer packetBuffer) {
super.readInitialSyncData(packetBuffer);
this.transferRate = packetBuffer.readInt();
this.conveyorMode = ConveyorMode.VALUES[packetBuffer.readByte()];
this.distributionMode = DistributionMode.VALUES[packetBuffer.readByte()];
this.manualImportExportMode = ManualImportExportMode.VALUES[packetBuffer.readByte()];
getItemFilterContainer().readInitialSyncData(packetBuffer);
}
@Override
public void writeToNBT(@NotNull NBTTagCompound tagCompound) {
super.writeToNBT(tagCompound);
tagCompound.setInteger("TransferRate", transferRate);
tagCompound.setInteger("ConveyorMode", conveyorMode.ordinal());
tagCompound.setInteger("DistributionMode", distributionMode.ordinal());
tagCompound.setBoolean("WorkingAllowed", isWorkingAllowed);
tagCompound.setInteger("ManualImportExportMode", manualImportExportMode.ordinal());
tagCompound.setTag("Filter", this.itemFilterContainer.serializeNBT());
}
@Override
public void readFromNBT(@NotNull NBTTagCompound tagCompound) {
super.readFromNBT(tagCompound);
this.transferRate = tagCompound.getInteger("TransferRate");
this.conveyorMode = ConveyorMode.VALUES[tagCompound.getInteger("ConveyorMode")];
this.distributionMode = DistributionMode.VALUES[tagCompound.getInteger("DistributionMode")];
this.isWorkingAllowed = tagCompound.getBoolean("WorkingAllowed");
this.manualImportExportMode = ManualImportExportMode.VALUES[tagCompound.getInteger("ManualImportExportMode")];
var filterTag = tagCompound.getCompoundTag("Filter");
if (filterTag.hasKey("IsBlacklist")) {
this.itemFilterContainer.handleLegacyNBT(filterTag);
} else {
this.itemFilterContainer.deserializeNBT(filterTag);
}
}
@Override
@SideOnly(Side.CLIENT)
protected @NotNull TextureAtlasSprite getPlateSprite() {
return Textures.VOLTAGE_CASINGS[this.tier].getSpriteOnSide(SimpleSidedCubeRenderer.RenderSide.SIDE);
}
public enum ConveyorMode implements IStringSerializable, IIOMode {
IMPORT("cover.conveyor.mode.import"),
EXPORT("cover.conveyor.mode.export");
public static final ConveyorMode[] VALUES = values();
public final String localeName;
ConveyorMode(String localeName) {
this.localeName = localeName;
}
@NotNull
@Override
public String getName() {
return localeName;
}
@Override
public boolean isImport() {
return this == IMPORT;
}
}
private class CoverableItemHandlerWrapper extends ItemHandlerDelegate {
public CoverableItemHandlerWrapper(IItemHandler delegate) {
super(delegate);
}
@NotNull
@Override
public ItemStack insertItem(int slot, @NotNull ItemStack stack, boolean simulate) {
if (conveyorMode == ConveyorMode.EXPORT && manualImportExportMode == ManualImportExportMode.DISABLED) {
return stack;
}
if (manualImportExportMode == ManualImportExportMode.FILTERED &&
!itemFilterContainer.test(stack)) {
return stack;
}
return super.insertItem(slot, stack, simulate);
}
@NotNull
@Override
public ItemStack extractItem(int slot, int amount, boolean simulate) {
if (conveyorMode == ConveyorMode.IMPORT && manualImportExportMode == ManualImportExportMode.DISABLED) {
return ItemStack.EMPTY;
}
if (manualImportExportMode == ManualImportExportMode.FILTERED) {
ItemStack result = super.extractItem(slot, amount, true);
if (result.isEmpty() || !itemFilterContainer.test(result)) {
return ItemStack.EMPTY;
}
return simulate ? result : super.extractItem(slot, amount, false);
}
return super.extractItem(slot, amount, simulate);
}
}
}