Search in sources :

Example 51 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project RFToolsDimensions by McJty.

the class PacketSyncDimensionInfo method toBytes.

@Override
public void toBytes(ByteBuf buf) {
    buf.writeInt(dimensions.size());
    for (Map.Entry<Integer, DimensionDescriptor> me : dimensions.entrySet()) {
        buf.writeInt(me.getKey());
        NBTTagCompound tagCompound = new NBTTagCompound();
        me.getValue().writeToNBT(tagCompound);
        PacketBuffer buffer = new PacketBuffer(buf);
        buffer.writeNBTTagCompoundToBuffer(tagCompound);
    }
    buf.writeInt(dimensionInformation.size());
    for (Map.Entry<Integer, DimensionInformation> me : dimensionInformation.entrySet()) {
        buf.writeInt(me.getKey());
        DimensionInformation dimInfo = me.getValue();
        NetworkTools.writeString(buf, dimInfo.getName());
        dimInfo.toBytes(buf);
    }
}
Also used : DimensionDescriptor(mcjty.rftoolsdim.dimensions.description.DimensionDescriptor) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) HashMap(java.util.HashMap) Map(java.util.Map) DimensionInformation(mcjty.rftoolsdim.dimensions.DimensionInformation) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 52 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.

the class NetworkHandler method packet2proxy.

@SuppressWarnings("unchecked")
public static FMLProxyPacket packet2proxy(Packet packet) {
    PacketCodec<Packet> codec = (PacketCodec<Packet>) codecMap.get(packet.getClass());
    PacketBuffer buf = new PacketBuffer(Unpooled.buffer());
    buf.writeVarInt(packetMap.getId(packet.getClass()));
    codec.encoder.encode(packet, buf);
    return new FMLProxyPacket(buf, GTValues.MODID);
}
Also used : FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) FMLProxyPacket(net.minecraftforge.fml.common.network.internal.FMLProxyPacket) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 53 with PacketBuffer

use of net.minecraft.network.PacketBuffer 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 54 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.

the class SyncedTileEntityBase method onDataPacket.

@Override
public void onDataPacket(NetworkManager net, SPacketUpdateTileEntity pkt) {
    NBTTagCompound updateTag = pkt.getNbtCompound();
    NBTTagList tagList = updateTag.getTagList("d", NBT.TAG_COMPOUND);
    for (int i = 0; i < tagList.tagCount(); i++) {
        NBTTagCompound entryTag = tagList.getCompoundTagAt(i);
        int discriminator = entryTag.getInteger("i");
        byte[] updateData = entryTag.getByteArray("d");
        ByteBuf backedBuffer = Unpooled.copiedBuffer(updateData);
        receiveCustomData(discriminator, new PacketBuffer(backedBuffer));
    }
}
Also used : NBTTagList(net.minecraft.nbt.NBTTagList) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) ByteBuf(io.netty.buffer.ByteBuf) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 55 with PacketBuffer

use of net.minecraft.network.PacketBuffer in project GregTech by GregTechCE.

the class SyncedTileEntityBase method writeCustomData.

public void writeCustomData(int discriminator, Consumer<PacketBuffer> dataWriter) {
    ByteBuf backedBuffer = Unpooled.buffer();
    dataWriter.accept(new PacketBuffer(backedBuffer));
    byte[] updateData = Arrays.copyOfRange(backedBuffer.array(), 0, backedBuffer.writerIndex());
    updateEntries.add(new UpdateEntry(discriminator, updateData));
    @SuppressWarnings("deprecation") IBlockState blockState = getBlockType().getStateFromMeta(getBlockMetadata());
    world.notifyBlockUpdate(getPos(), blockState, blockState, 0);
}
Also used : IBlockState(net.minecraft.block.state.IBlockState) ByteBuf(io.netty.buffer.ByteBuf) PacketBuffer(net.minecraft.network.PacketBuffer)

Aggregations

PacketBuffer (net.minecraft.network.PacketBuffer)105 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)20 IOException (java.io.IOException)14 FMLProxyPacket (net.minecraftforge.fml.common.network.internal.FMLProxyPacket)14 ByteBuf (io.netty.buffer.ByteBuf)11 SPacketCustomPayload (net.minecraft.network.play.server.SPacketCustomPayload)7 BlockPos (net.minecraft.util.math.BlockPos)7 HashMap (java.util.HashMap)4 DimensionInformation (mcjty.rftoolsdim.dimensions.DimensionInformation)4 DimensionDescriptor (mcjty.rftoolsdim.dimensions.description.DimensionDescriptor)4 EntityPlayerMP (net.minecraft.entity.player.EntityPlayerMP)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 NetHandlerPlayServer (net.minecraft.network.NetHandlerPlayServer)3 Test (org.junit.Test)3 PacketBufferBC (buildcraft.lib.net.PacketBufferBC)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 PacketCustomTileData (gregtech.api.net.PacketCustomTileData)2 PacketUIWidgetUpdate (gregtech.api.net.PacketUIWidgetUpdate)2 AbstractData (hellfirepvp.fracture.common.data.AbstractData)2