use of gregtech.api.gui.INativeWidget in project GregTech by GregTechCE.
the class ModularUIContainer method notifyWidgetChange.
// WARNING! WIDGET CHANGES SHOULD BE *STRICTLY* SYNCHRONIZED BETWEEN SERVER AND CLIENT,
// OTHERWISE ID MISMATCH CAN HAPPEN BETWEEN ASSIGNED SLOTS!
@Override
public void notifyWidgetChange() {
List<INativeWidget> nativeWidgets = modularUI.guiWidgets.values().stream().flatMap(widget -> widget.getNativeWidgets().stream()).collect(Collectors.toList());
Set<INativeWidget> removedWidgets = new HashSet<>(slotMap.values());
removedWidgets.removeAll(nativeWidgets);
if (!removedWidgets.isEmpty()) {
for (INativeWidget removedWidget : removedWidgets) {
Slot slotHandle = removedWidget.getHandle();
this.slotMap.remove(slotHandle);
// replace removed slot with empty placeholder to avoid list index shift
EmptySlotPlaceholder emptySlotPlaceholder = new EmptySlotPlaceholder();
emptySlotPlaceholder.slotNumber = slotHandle.slotNumber;
this.inventorySlots.set(slotHandle.slotNumber, emptySlotPlaceholder);
this.inventoryItemStacks.set(slotHandle.slotNumber, ItemStack.EMPTY);
}
}
Set<INativeWidget> addedWidgets = new HashSet<>(nativeWidgets);
addedWidgets.removeAll(slotMap.values());
if (!addedWidgets.isEmpty()) {
int[] emptySlotIndexes = inventorySlots.stream().filter(it -> it instanceof EmptySlotPlaceholder).mapToInt(slot -> slot.slotNumber).toArray();
int currentIndex = 0;
for (INativeWidget addedWidget : addedWidgets) {
Slot slotHandle = addedWidget.getHandle();
// add or replace empty slot in inventory
this.slotMap.put(slotHandle, addedWidget);
if (currentIndex < emptySlotIndexes.length) {
int slotIndex = emptySlotIndexes[currentIndex++];
slotHandle.slotNumber = slotIndex;
this.inventorySlots.set(slotIndex, slotHandle);
this.inventoryItemStacks.set(slotIndex, ItemStack.EMPTY);
} else {
slotHandle.slotNumber = this.inventorySlots.size();
this.inventorySlots.add(slotHandle);
this.inventoryItemStacks.add(ItemStack.EMPTY);
}
}
}
}
Aggregations