Search in sources :

Example 1 with PacketUIWidgetUpdate

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

the class Widget method writeUpdateInfo.

/**
 * Writes data to be sent to client's {@link #readUpdateInfo}
 */
protected final void writeUpdateInfo(int id, Consumer<PacketBuffer> packetBufferWriter) {
    PacketBuffer packetBuffer = new PacketBuffer(Unpooled.buffer());
    packetBuffer.writeInt(id);
    packetBufferWriter.accept(packetBuffer);
    int widgetId = gui.guiWidgets.inverse().get(this);
    if (gui.entityPlayer instanceof EntityPlayerMP) {
        int currentWindowId = gui.entityPlayer.openContainer.windowId;
        PacketUIWidgetUpdate widgetUpdate = new PacketUIWidgetUpdate(currentWindowId, widgetId, packetBuffer);
        NetworkHandler.channel.sendTo(NetworkHandler.packet2proxy(widgetUpdate), (EntityPlayerMP) gui.entityPlayer);
    }
}
Also used : PacketUIWidgetUpdate(gregtech.api.net.PacketUIWidgetUpdate) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 2 with PacketUIWidgetUpdate

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

the class UIFactory method initClientUI.

@SideOnly(Side.CLIENT)
public final void initClientUI(PacketBuffer serializedHolder, int windowId, List<PacketUIWidgetUpdate> initialWidgetUpdates) {
    E holder = readHolderFromSyncData(serializedHolder);
    Minecraft minecraft = Minecraft.getMinecraft();
    EntityPlayerSP entityPlayer = minecraft.player;
    ModularUI uiTemplate = createUITemplate(holder, entityPlayer);
    uiTemplate.initWidgets();
    ModularUIGui modularUIGui = new ModularUIGui(uiTemplate);
    modularUIGui.inventorySlots.windowId = windowId;
    for (PacketUIWidgetUpdate packet : initialWidgetUpdates) {
        modularUIGui.handleWidgetUpdate(packet);
    }
    minecraft.addScheduledTask(() -> {
        minecraft.displayGuiScreen(modularUIGui);
        minecraft.player.openContainer.windowId = windowId;
    });
}
Also used : PacketUIWidgetUpdate(gregtech.api.net.PacketUIWidgetUpdate) EntityPlayerSP(net.minecraft.client.entity.EntityPlayerSP) Minecraft(net.minecraft.client.Minecraft) ModularUIGui(gregtech.api.gui.impl.ModularUIGui) SideOnly(net.minecraftforge.fml.relauncher.SideOnly)

Example 3 with PacketUIWidgetUpdate

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

the class ModularUIGui method updateScreen.

@Override
public void updateScreen() {
    super.updateScreen();
    PacketUIWidgetUpdate packet = queuingWidgetUpdates.poll();
    if (packet != null && packet.windowId == inventorySlots.windowId) {
        Widget<?> widget = modularUI.guiWidgets.get(packet.widgetId);
        int discriminator = packet.updateData.readInt();
        if (widget != null)
            widget.readUpdateInfo(discriminator, packet.updateData);
    }
    modularUI.guiWidgets.values().forEach(Widget::updateScreen);
}
Also used : PacketUIWidgetUpdate(gregtech.api.net.PacketUIWidgetUpdate) Widget(gregtech.api.gui.Widget)

Example 4 with PacketUIWidgetUpdate

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

the class ModularUIContainer method writeUpdateInfo.

@Override
public void writeUpdateInfo(Widget widget, int updateId, Consumer<PacketBuffer> payloadWriter) {
    int widgetId = modularUI.guiWidgets.inverse().get(widget);
    PacketBuffer packetBuffer = new PacketBuffer(Unpooled.buffer());
    packetBuffer.writeVarInt(updateId);
    payloadWriter.accept(packetBuffer);
    if (modularUI.entityPlayer instanceof EntityPlayerMP) {
        PacketUIWidgetUpdate widgetUpdate = new PacketUIWidgetUpdate(windowId, widgetId, packetBuffer);
        if (!accumulateWidgetUpdateData) {
            NetworkHandler.channel.sendTo(NetworkHandler.packet2proxy(widgetUpdate), (EntityPlayerMP) modularUI.entityPlayer);
        } else {
            accumulatedUpdates.add(widgetUpdate);
        }
    }
}
Also used : PacketUIWidgetUpdate(gregtech.api.net.PacketUIWidgetUpdate) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 5 with PacketUIWidgetUpdate

use of gregtech.api.net.PacketUIWidgetUpdate 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

PacketUIWidgetUpdate (gregtech.api.net.PacketUIWidgetUpdate)5 PacketBuffer (net.minecraft.network.PacketBuffer)3 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)2 Widget (gregtech.api.gui.Widget)1 ModularUIContainer (gregtech.api.gui.impl.ModularUIContainer)1 ModularUIGui (gregtech.api.gui.impl.ModularUIGui)1 PacketUIOpen (gregtech.api.net.PacketUIOpen)1 ArrayList (java.util.ArrayList)1 Minecraft (net.minecraft.client.Minecraft)1 EntityPlayerSP (net.minecraft.client.entity.EntityPlayerSP)1 FakePlayer (net.minecraftforge.common.util.FakePlayer)1 PlayerContainerEvent (net.minecraftforge.event.entity.player.PlayerContainerEvent)1 SideOnly (net.minecraftforge.fml.relauncher.SideOnly)1