Example usage for io.netty.buffer Unpooled buffer

List of usage examples for io.netty.buffer Unpooled buffer

Introduction

In this page you can find the example usage for io.netty.buffer Unpooled buffer.

Prototype

public static ByteBuf buffer() 

Source Link

Document

Creates a new big-endian Java heap buffer with reasonably small initial capacity, which expands its capacity boundlessly on demand.

Usage

From source file:buildcraft.core.lib.utils.Utils.java

License:Minecraft Mod Public

/**
 * This subprogram transforms a packet into a FML packet to be send in the
 * minecraft default packet mechanism. This always use BC-CORE as a
 * channel, and as a result, should use discriminators declared there.
 *
 * WARNING! The implementation of this subprogram relies on the internal
 * behavior of #FMLIndexedMessageToMessageCodec (in particular the encode
 * member). It is probably opening a maintenance issue and should be
 * replaced eventually by some more solid mechanism.
 *///from w ww. j ava  2  s . co  m
public static FMLProxyPacket toPacket(Packet packet, int discriminator) {
    ByteBuf buf = Unpooled.buffer();

    buf.writeByte((byte) discriminator);
    packet.writeData(buf);

    return new FMLProxyPacket(buf, DefaultProps.NET_CHANNEL_NAME + "-CORE");
}

From source file:buildcraft.core.network.PacketRPC.java

License:Minecraft Mod Public

public ArrayList<PacketRPC> breakIntoSmallerPackets(int maxSize) {
    ArrayList<PacketRPC> messages = new ArrayList<PacketRPC>();

    if (contents.readableBytes() < maxSize) {
        messages.add(this);
        return messages;
    }//w w  w .j  a  va  2 s.c  o  m

    int start = 0;

    while (true) {
        ByteBuf subContents = contents
                .readBytes(contents.readableBytes() > maxSize ? maxSize : contents.readableBytes());

        PacketRPCPart subPacket = new PacketRPCPart();
        subPacket.id = id;
        subPacket.contents = subContents;

        messages.add(subPacket);

        start += maxSize;

        if (contents.readableBytes() == 0) {
            break;
        }
    }

    contents = Unpooled.buffer();

    messages.add(this);

    return messages;
}

From source file:buildcraft.core.network.PacketRPCTile.java

License:Minecraft Mod Public

public void call(EntityPlayer sender) {
    World world = null;/*from w w w.j av a 2 s . c  o m*/

    if (!sender.worldObj.isRemote) {
        // if this is a server, then get the world

        world = DimensionManager.getProvider(dimId).worldObj;
    } else if (sender.worldObj.provider.dimensionId == dimId) {
        // if the player is on this world, then synchronize things

        world = sender.worldObj;
    }

    TileEntity localTile = world.getTileEntity(x, y, z);

    setTile(localTile);

    RPCMessageInfo info = new RPCMessageInfo();
    info.sender = sender;

    ByteBuf previousData = bufferedPackets.get(id);
    bufferedPackets.remove(id);

    ByteBuf completeData;

    if (previousData != null) {
        completeData = previousData.writeBytes(contents);
    } else {
        completeData = Unpooled.buffer();
        completeData.writeBytes(contents);
    }

    if (!moreDataToCome) {
        RPCHandler.receiveRPC(localTile, info, completeData);
    } else {
        bufferedPackets.put(id, completeData);
    }
}

From source file:buildcraft.core.network.PacketTileState.java

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    super.writeData(data);

    ByteBuf tmpState = Unpooled.buffer();

    tmpState.writeByte(stateList.size());
    for (StateWithId stateWithId : stateList) {
        tmpState.writeByte(stateWithId.stateId);
        stateWithId.state.writeData(tmpState);
    }//  ww  w .  ja v a  2s.c o  m

    data.writeInt(tmpState.readableBytes());
    data.writeBytes(tmpState.readBytes(tmpState.readableBytes()));
}

From source file:buildcraft.core.network.PacketTileState.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf data) {
    super.readData(data);

    state = Unpooled.buffer();
    int length = data.readInt();
    state.writeBytes(data.readBytes(length));
}

From source file:buildcraft.core.network.RPCHandler.java

License:Minecraft Mod Public

private PacketRPCPipe createRCPPacket(Pipe pipe, String method, Object... actuals) {
    ByteBuf data = Unpooled.buffer();

    try {/*from  ww  w  .ja v  a  2s  .  c  om*/
        TileEntity tile = pipe.container;

        // In order to save space on message, we assuming dimensions ids
        // small. Maybe worth using a varint instead
        data.writeShort(tile.getWorldObj().provider.dimensionId);
        data.writeInt(tile.xCoord);
        data.writeInt(tile.yCoord);
        data.writeInt(tile.zCoord);

        writeParameters(method, data, actuals);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    byte[] bytes = new byte[data.readableBytes()];
    data.readBytes(bytes);

    return new PacketRPCPipe(bytes);
}

From source file:buildcraft.core.network.RPCHandler.java

License:Minecraft Mod Public

private PacketRPCTile createRCPPacket(TileEntity tile, String method, Object... actuals) {
    ByteBuf data = Unpooled.buffer();

    try {/*from ww  w  .j a v  a2  s .  co m*/
        writeParameters(method, data, actuals);
    } catch (IOException e) {
        e.printStackTrace();
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    } catch (IllegalAccessException e) {
        e.printStackTrace();
    }

    byte[] bytes = new byte[data.readableBytes()];
    data.readBytes(bytes);

    return new PacketRPCTile(tile, bytes);
}

From source file:buildcraft.core.utils.Utils.java

License:Minecraft Mod Public

/**
 * This subprogram transforms a packet into a FML packet to be send in the
 * minecraft default packet mechanism. This always use BC-CORE as a
 * channel, and as a result, should use discriminators declared there.
 *
 * WARNING! The implementation of this subprogram relies on the internal
 * behavior of #FMLIndexedMessageToMessageCodec (in particular the encode
 * member). It is probably opening a maintenance issue and should be
 * replaced eventually by some more solid mechanism.
 *//*from  w w  w  . j a  va  2s  . c o m*/
public static FMLProxyPacket toPacket(BuildCraftPacket packet, int discriminator) {
    ByteBuf buf = Unpooled.buffer();

    buf.writeByte((byte) discriminator);
    packet.writeData(buf);

    return new FMLProxyPacket(buf, DefaultProps.NET_CHANNEL_NAME + "-CORE");
}

From source file:buildcraft.lib.tile.TileBC_Neptune.java

License:Mozilla Public License

public final MessageUpdateTile createMessage(int id, IPayloadWriter writer) {
    PacketBufferBC buffer = new PacketBufferBC(Unpooled.buffer());
    buffer.writeShort(id);//from  w w w  .j  a  va  2 s. c o  m
    writer.write(buffer);
    return new MessageUpdateTile(getPos(), buffer);
}

From source file:buildcraft.lib.tile.TileBC_Neptune.java

License:Mozilla Public License

@Override
public NBTTagCompound getUpdateTag() {
    ByteBuf buf = Unpooled.buffer();
    buf.writeShort(NET_RENDER_DATA);/* w  w w.j a  v a 2 s .  com*/
    writePayload(NET_RENDER_DATA, new PacketBufferBC(buf), world.isRemote ? Side.CLIENT : Side.SERVER);
    byte[] bytes = new byte[buf.readableBytes()];
    buf.readBytes(bytes);

    NBTTagCompound nbt = super.getUpdateTag();
    nbt.setByteArray("d", bytes);
    return nbt;
}