Search in sources :

Example 1 with PacketUIOpen

use of gregtech.api.net.PacketUIOpen in project GregTech by GregTechCE.

the class UIFactory method openUI.

public final void openUI(E holder, EntityPlayerMP player) {
    if (player instanceof FakePlayer) {
        return;
    }
    ModularUI uiTemplate = createUITemplate(holder, player);
    uiTemplate.initWidgets();
    player.getNextWindowId();
    player.closeContainer();
    int currentWindowId = player.currentWindowId;
    PacketBuffer serializedHolder = new PacketBuffer(Unpooled.buffer());
    writeHolderToSyncData(serializedHolder, holder);
    int uiFactoryId = FACTORY_REGISTRY.getIDForObject(this);
    ModularUIContainer container = new ModularUIContainer(uiTemplate);
    container.windowId = currentWindowId;
    // accumulate all initial updates of widgets in open packet
    container.accumulateWidgetUpdateData = true;
    uiTemplate.guiWidgets.values().forEach(Widget::detectAndSendChanges);
    container.accumulateWidgetUpdateData = false;
    ArrayList<PacketUIWidgetUpdate> updateData = new ArrayList<>(container.accumulatedUpdates);
    container.accumulatedUpdates.clear();
    PacketUIOpen packet = new PacketUIOpen(uiFactoryId, serializedHolder, currentWindowId, updateData);
    NetworkHandler.channel.sendTo(NetworkHandler.packet2proxy(packet), player);
    container.addListener(player);
    player.openContainer = container;
    // and fire forge event only in the end
    MinecraftForge.EVENT_BUS.post(new PlayerContainerEvent.Open(player, container));
}
Also used : PacketUIWidgetUpdate(gregtech.api.net.PacketUIWidgetUpdate) ModularUIContainer(gregtech.api.gui.impl.ModularUIContainer) ArrayList(java.util.ArrayList) PacketUIOpen(gregtech.api.net.PacketUIOpen) PlayerContainerEvent(net.minecraftforge.event.entity.player.PlayerContainerEvent) PacketBuffer(net.minecraft.network.PacketBuffer) FakePlayer(net.minecraftforge.common.util.FakePlayer)

Aggregations

ModularUIContainer (gregtech.api.gui.impl.ModularUIContainer)1 PacketUIOpen (gregtech.api.net.PacketUIOpen)1 PacketUIWidgetUpdate (gregtech.api.net.PacketUIWidgetUpdate)1 ArrayList (java.util.ArrayList)1 PacketBuffer (net.minecraft.network.PacketBuffer)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1 PlayerContainerEvent (net.minecraftforge.event.entity.player.PlayerContainerEvent)1