Search in sources :

Example 1 with ModularUIContainer

use of gregtech.api.gui.impl.ModularUIContainer in project GregTech by GregTechCE.

the class NetworkHandler method init.

public static void init() {
    channel = NetworkRegistry.INSTANCE.newEventDrivenChannel(GTValues.MODID);
    channel.register(new NetworkHandler());
    PacketEncoder<PacketUIWidgetUpdate> widgetUpdateEncoder = (packet, buf) -> {
        buf.writeVarInt(packet.updateData.readableBytes());
        buf.writeBytes(packet.updateData);
        buf.writeVarInt(packet.windowId);
        buf.writeVarInt(packet.widgetId);
    };
    PacketDecoder<PacketUIWidgetUpdate> widgetUpdateDecoder = (buf) -> {
        ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
        ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
        directSliceBuffer.release();
        return new PacketUIWidgetUpdate(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
    };
    registerPacket(1, PacketUIOpen.class, new PacketCodec<>((packet, buf) -> {
        buf.writeVarInt(packet.serializedHolder.readableBytes());
        buf.writeBytes(packet.serializedHolder);
        buf.writeVarInt(packet.uiFactoryId);
        buf.writeVarInt(packet.windowId);
        buf.writeVarInt(packet.initialWidgetUpdates.size());
        for (PacketUIWidgetUpdate widgetUpdate : packet.initialWidgetUpdates) {
            widgetUpdateEncoder.encode(widgetUpdate, buf);
        }
    }, (buf) -> {
        ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
        ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
        directSliceBuffer.release();
        int uiFactoryId = buf.readVarInt();
        int windowId = buf.readVarInt();
        ArrayList<PacketUIWidgetUpdate> initialWidgetUpdates = new ArrayList<>();
        int initialWidgetUpdatesCount = buf.readVarInt();
        for (int i = 0; i < initialWidgetUpdatesCount; i++) {
            initialWidgetUpdates.add(widgetUpdateDecoder.decode(buf));
        }
        return new PacketUIOpen(uiFactoryId, new PacketBuffer(copiedDataBuffer), windowId, initialWidgetUpdates);
    }));
    registerPacket(2, PacketUIWidgetUpdate.class, new PacketCodec<>((packet, buf) -> {
        buf.writeVarInt(packet.updateData.readableBytes());
        buf.writeBytes(packet.updateData);
        buf.writeVarInt(packet.windowId);
        buf.writeVarInt(packet.widgetId);
    }, (buf) -> {
        ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
        ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
        directSliceBuffer.release();
        return new PacketUIWidgetUpdate(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
    }));
    registerPacket(3, PacketUIClientAction.class, new PacketCodec<>((packet, buf) -> {
        buf.writeVarInt(packet.updateData.readableBytes());
        buf.writeBytes(packet.updateData);
        buf.writeVarInt(packet.windowId);
        buf.writeVarInt(packet.widgetId);
    }, (buf) -> {
        ByteBuf directSliceBuffer = buf.readBytes(buf.readVarInt());
        ByteBuf copiedDataBuffer = Unpooled.copiedBuffer(directSliceBuffer);
        directSliceBuffer.release();
        return new PacketUIClientAction(buf.readVarInt(), buf.readVarInt(), new PacketBuffer(copiedDataBuffer));
    }));
    registerPacket(4, PacketBlockParticle.class, new PacketCodec<>((packet, buf) -> {
        buf.writeBlockPos(packet.blockPos);
        buf.writeFloat((float) packet.entityPos.x);
        buf.writeFloat((float) packet.entityPos.y);
        buf.writeFloat((float) packet.entityPos.z);
        buf.writeVarInt(packet.particlesAmount);
    }, (buf) -> new PacketBlockParticle(buf.readBlockPos(), new Vector3(buf.readFloat(), buf.readFloat(), buf.readFloat()), buf.readVarInt())));
    registerPacket(5, PacketClipboard.class, new PacketCodec<>((packet, buf) -> {
        buf.writeString(packet.text);
    }, (buf) -> new PacketClipboard(buf.readString(32767))));
    registerServerExecutor(PacketUIClientAction.class, (packet, handler) -> {
        Container openContainer = handler.player.openContainer;
        if (openContainer instanceof ModularUIContainer && openContainer.windowId == packet.windowId) {
            ModularUI modularUI = ((ModularUIContainer) openContainer).getModularUI();
            PacketBuffer buffer = packet.updateData;
            modularUI.guiWidgets.get(packet.widgetId).handleClientAction(buffer.readVarInt(), buffer);
        }
    });
    if (FMLCommonHandler.instance().getSide().isClient()) {
        initClient();
    }
}
Also used : FMLNetworkEvent(net.minecraftforge.fml.common.network.FMLNetworkEvent) Vector3(codechicken.lib.vec.Vector3) ModularUIGui(gregtech.api.gui.impl.ModularUIGui) FMLEventChannel(net.minecraftforge.fml.common.network.FMLEventChannel) NetHandlerPlayServer(net.minecraft.network.NetHandlerPlayServer) FMLCommonHandler(net.minecraftforge.fml.common.FMLCommonHandler) HashMap(java.util.HashMap) IntIdentityHashBiMap(net.minecraft.util.IntIdentityHashBiMap) ICustomParticleBlock(gregtech.api.block.ICustomParticleBlock) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Minecraft(net.minecraft.client.Minecraft) Side(net.minecraftforge.fml.relauncher.Side) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) IThreadListener(net.minecraft.util.IThreadListener) SideOnly(net.minecraftforge.fml.relauncher.SideOnly) ModularUIContainer(gregtech.api.gui.impl.ModularUIContainer) GTLog(gregtech.api.util.GTLog) World(net.minecraft.world.World) INetHandler(net.minecraft.network.INetHandler) BlockPos(net.minecraft.util.math.BlockPos) ClipboardUtil(gregtech.api.util.ClipboardUtil) IBlockState(net.minecraft.block.state.IBlockState) GuiScreen(net.minecraft.client.gui.GuiScreen) GTValues(gregtech.api.GTValues) ParticleManager(net.minecraft.client.particle.ParticleManager) NetworkRegistry(net.minecraftforge.fml.common.network.NetworkRegistry) NetHandlerPlayClient(net.minecraft.client.network.NetHandlerPlayClient) TargetPoint(net.minecraftforge.fml.common.network.NetworkRegistry.TargetPoint) SubscribeEvent(net.minecraftforge.fml.common.eventhandler.SubscribeEvent) ModularUI(gregtech.api.gui.ModularUI) PacketBuffer(net.minecraft.network.PacketBuffer) UIFactory(gregtech.api.gui.UIFactory) Container(net.minecraft.inventory.Container) ArrayList(java.util.ArrayList) Vector3(codechicken.lib.vec.Vector3) ByteBuf(io.netty.buffer.ByteBuf) ModularUIContainer(gregtech.api.gui.impl.ModularUIContainer) Container(net.minecraft.inventory.Container) ModularUIContainer(gregtech.api.gui.impl.ModularUIContainer) ModularUI(gregtech.api.gui.ModularUI) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 2 with ModularUIContainer

use of gregtech.api.gui.impl.ModularUIContainer in project GregTech by GregTechCE.

the class GTUtility method findPlayersUsing.

public static List<EntityPlayerMP> findPlayersUsing(MetaTileEntity metaTileEntity, double radius) {
    ArrayList<EntityPlayerMP> result = new ArrayList<>();
    AxisAlignedBB box = new AxisAlignedBB(metaTileEntity.getPos()).expand(radius, radius, radius).expand(-radius, -radius, -radius);
    List<EntityPlayerMP> entities = metaTileEntity.getWorld().getEntitiesWithinAABB(EntityPlayerMP.class, box);
    for (EntityPlayerMP player : entities) {
        if (player.openContainer instanceof ModularUIContainer) {
            ModularUI modularUI = ((ModularUIContainer) player.openContainer).getModularUI();
            if (modularUI.holder instanceof MetaTileEntityHolder && ((MetaTileEntityHolder) modularUI.holder).getMetaTileEntity() == metaTileEntity) {
                result.add(player);
            }
        }
    }
    return result;
}
Also used : AxisAlignedBB(net.minecraft.util.math.AxisAlignedBB) MetaTileEntityHolder(gregtech.api.metatileentity.MetaTileEntityHolder) ModularUIContainer(gregtech.api.gui.impl.ModularUIContainer) ModularUI(gregtech.api.gui.ModularUI) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP)

Example 3 with ModularUIContainer

use of gregtech.api.gui.impl.ModularUIContainer 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)3 ModularUI (gregtech.api.gui.ModularUI)2 ArrayList (java.util.ArrayList)2 PacketBuffer (net.minecraft.network.PacketBuffer)2 Vector3 (codechicken.lib.vec.Vector3)1 GTValues (gregtech.api.GTValues)1 ICustomParticleBlock (gregtech.api.block.ICustomParticleBlock)1 UIFactory (gregtech.api.gui.UIFactory)1 ModularUIGui (gregtech.api.gui.impl.ModularUIGui)1 MetaTileEntityHolder (gregtech.api.metatileentity.MetaTileEntityHolder)1 PacketUIOpen (gregtech.api.net.PacketUIOpen)1 PacketUIWidgetUpdate (gregtech.api.net.PacketUIWidgetUpdate)1 ClipboardUtil (gregtech.api.util.ClipboardUtil)1 GTLog (gregtech.api.util.GTLog)1 ByteBuf (io.netty.buffer.ByteBuf)1 Unpooled (io.netty.buffer.Unpooled)1 HashMap (java.util.HashMap)1 IBlockState (net.minecraft.block.state.IBlockState)1 Minecraft (net.minecraft.client.Minecraft)1 GuiScreen (net.minecraft.client.gui.GuiScreen)1