Search in sources :

Example 1 with INativeWidget

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);
            }
        }
    }
}
Also used : INativeWidget(gregtech.api.gui.INativeWidget) WidgetUIAccess(gregtech.api.gui.widgets.WidgetUIAccess) java.util(java.util) PacketUIWidgetUpdate(gregtech.api.net.PacketUIWidgetUpdate) Collectors(java.util.stream.Collectors) Unpooled(io.netty.buffer.Unpooled) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) Consumer(java.util.function.Consumer) ItemStack(net.minecraft.item.ItemStack) Widget(gregtech.api.gui.Widget) PacketUIClientAction(gregtech.api.net.PacketUIClientAction) EntityPlayer(net.minecraft.entity.player.EntityPlayer) NetworkHandler(gregtech.api.net.NetworkHandler) ModularUI(gregtech.api.gui.ModularUI) PerTickIntCounter(gregtech.api.util.PerTickIntCounter) net.minecraft.inventory(net.minecraft.inventory) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) GTUtility(gregtech.api.util.GTUtility) PacketBuffer(net.minecraft.network.PacketBuffer) SPacketSetSlot(net.minecraft.network.play.server.SPacketSetSlot) INativeWidget(gregtech.api.gui.INativeWidget)

Aggregations

INativeWidget (gregtech.api.gui.INativeWidget)1 ModularUI (gregtech.api.gui.ModularUI)1 Widget (gregtech.api.gui.Widget)1 WidgetUIAccess (gregtech.api.gui.widgets.WidgetUIAccess)1 NetworkHandler (gregtech.api.net.NetworkHandler)1 PacketUIClientAction (gregtech.api.net.PacketUIClientAction)1 PacketUIWidgetUpdate (gregtech.api.net.PacketUIWidgetUpdate)1 GTUtility (gregtech.api.util.GTUtility)1 PerTickIntCounter (gregtech.api.util.PerTickIntCounter)1 Unpooled (io.netty.buffer.Unpooled)1 java.util (java.util)1 Consumer (java.util.function.Consumer)1 Collectors (java.util.stream.Collectors)1 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)1 EntityPlayer (net.minecraft.entity.player.EntityPlayer)1 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)1 net.minecraft.inventory (net.minecraft.inventory)1 ItemStack (net.minecraft.item.ItemStack)1 PacketBuffer (net.minecraft.network.PacketBuffer)1 SPacketSetSlot (net.minecraft.network.play.server.SPacketSetSlot)1