Search in sources :

Example 6 with PacketBuffer

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

the class ICustomDataTile method onBeingWatched.

default void onBeingWatched(EntityPlayerMP watcher) {
    TileEntity tileEntity = (TileEntity) this;
    PacketBuffer buffer = new PacketBuffer(Unpooled.buffer());
    buffer.writeBoolean(true);
    writeInitialSyncData(buffer);
    PacketCustomTileData packet = new PacketCustomTileData(tileEntity.getPos(), buffer);
    NetworkHandler.channel.sendTo(packet.toFMLPacket(), watcher);
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) PacketCustomTileData(gregtech.api.net.PacketCustomTileData) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 7 with PacketBuffer

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

the class ICustomDataTile method writeCustomData.

default void writeCustomData(int discriminator, Consumer<PacketBuffer> dataWriter) {
    // unsafe
    TileEntity tileEntity = (TileEntity) this;
    WorldServer worldServer = (WorldServer) tileEntity.getWorld();
    BlockPos pos = tileEntity.getPos();
    PlayerChunkMapEntry chunkLoaderEntry = worldServer.getPlayerChunkMap().getEntry(pos.getX() / 16, pos.getZ() / 16);
    // nobody is watching chunk, so...
    if (chunkLoaderEntry == null)
        return;
    PacketBuffer backedData = new PacketBuffer(Unpooled.buffer());
    backedData.writeBoolean(false);
    backedData.writeInt(discriminator);
    dataWriter.accept(backedData);
    PacketCustomTileData packet = new PacketCustomTileData(pos, backedData);
    for (EntityPlayerMP watcher : chunkLoaderEntry.players) {
        NetworkHandler.channel.sendTo(packet.toFMLPacket(), watcher);
    }
}
Also used : TileEntity(net.minecraft.tileentity.TileEntity) PacketCustomTileData(gregtech.api.net.PacketCustomTileData) WorldServer(net.minecraft.world.WorldServer) BlockPos(net.minecraft.util.math.BlockPos) PlayerChunkMapEntry(net.minecraft.server.management.PlayerChunkMapEntry) EntityPlayerMP(net.minecraft.entity.player.EntityPlayerMP) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 8 with PacketBuffer

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

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

the class DimensionSyncPacket method consumePacket.

public void consumePacket(ByteBuf data) {
    int size = data.readInt();
    for (int i = 0; i < size; i++) {
        int id = data.readInt();
        PacketBuffer buffer = new PacketBuffer(data);
        NBTTagCompound tagCompound;
        try {
            tagCompound = buffer.readCompoundTag();
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        DimensionDescriptor descriptor = new DimensionDescriptor(tagCompound);
        dimensions.put(id, descriptor);
    }
    size = data.readInt();
    for (int i = 0; i < size; i++) {
        int id = data.readInt();
        String name = NetworkTools.readString(data);
        DimensionInformation dimInfo = new DimensionInformation(name, dimensions.get(id), data);
        dimensionInformation.put(id, dimInfo);
    }
}
Also used : DimensionDescriptor(mcjty.rftoolsdim.dimensions.description.DimensionDescriptor) NBTTagCompound(net.minecraft.nbt.NBTTagCompound) IOException(java.io.IOException) DimensionInformation(mcjty.rftoolsdim.dimensions.DimensionInformation) PacketBuffer(net.minecraft.network.PacketBuffer)

Example 10 with PacketBuffer

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

the class DimensionSyncPacket method getData.

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

Aggregations

PacketBuffer (net.minecraft.network.PacketBuffer)103 NBTTagCompound (net.minecraft.nbt.NBTTagCompound)18 FMLProxyPacket (net.minecraftforge.fml.common.network.internal.FMLProxyPacket)14 IOException (java.io.IOException)13 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