Example usage for io.netty.buffer ByteBuf writeInt

List of usage examples for io.netty.buffer ByteBuf writeInt

Introduction

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

Prototype

public abstract ByteBuf writeInt(int value);

Source Link

Document

Sets the specified 32-bit integer at the current writerIndex and increases the writerIndex by 4 in this buffer.

Usage

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

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    data.writeInt(id);
    data.writeInt(contents.readableBytes());
    data.writeBytes(contents);
}

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

License:Minecraft Mod Public

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

    data.writeInt(entity.getEntityId());
}

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

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    // 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);/*  w w w  .  j  a  va2  s  .c  o m*/
    data.writeInt(tile.zCoord);

    data.writeInt(id);
    data.writeBoolean(moreDataToCome);
    data.writeBytes(contents);
}

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

License:Minecraft Mod Public

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

    data.writeInt(slot);
    Utils.writeStack(data, stack);
}

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);
    }//  w w w  . j  a  va  2  s .com

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

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

License:Minecraft Mod Public

@Override
public void writeData(ByteBuf data) {
    data.writeByte(packetId);/* w  w  w .  j  a  v a  2 s.c  om*/
    data.writeInt(posX);
    data.writeInt(posY);
    data.writeInt(posZ);

    if (payload != null) {
        payload.writeData(data);
    } else {
        data.writeByte(0);
    }
}

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 w w w.  j a va 2s  .  co  m
        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 void writeParameters(String method, ByteBuf data, Object... actuals)
        throws IOException, IllegalArgumentException, IllegalAccessException {
    if (!methodsMap.containsKey(method)) {
        throw new RuntimeException(method + " is not a callable method of " + getClass().getName());
    }/* ww w  . j a  v  a  2 s  . c om*/

    int methodIndex = methodsMap.get(method);
    MethodMapping m = methods[methodIndex];
    Class[] formals = m.parameters;

    int expectedParameters = m.hasInfo ? formals.length - 1 : formals.length;

    if (expectedParameters != actuals.length) {
        // We accept formals + 1 as an argument, in order to support the
        // special last argument RPCMessageInfo

        throw new RuntimeException(getClass().getName() + "." + method + " expects " + m.parameters.length
                + " parameters, not " + actuals.length);
    }

    data.writeShort(methodIndex);

    SerializationContext context = new SerializationContext();

    for (int i = 0; i < actuals.length; ++i) {
        if (int.class.equals(formals[i])) {
            data.writeInt((Integer) actuals[i]);
        } else if (float.class.equals(formals[i])) {
            data.writeFloat((Float) actuals[i]);
        } else if (char.class.equals(formals[i])) {
            data.writeChar((Character) actuals[i]);
        } else {
            m.mappings[i].write(data, actuals[i], context);
        }
    }
}

From source file:buildcraft.core.network.serializers.ClassMapping.java

License:Minecraft Mod Public

@SuppressWarnings("rawtypes")
void writeClass(Object obj, ByteBuf data, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {

    Class realClass = obj.getClass();

    if (realClass.equals(this.mappedClass)) {
        data.writeByte(0);//from www  .j ava2s .c  om
    } else {
        ClassMapping delegateMapping;

        if (context.classToId.containsKey(realClass.getCanonicalName())) {
            int index = context.classToId.get(realClass.getCanonicalName()) + 1;
            data.writeByte(index);
            delegateMapping = (ClassMapping) context.idToClass.get(index - 1);
        } else {
            int index = context.classToId.size() + 1;
            delegateMapping = (ClassMapping) get(realClass);

            data.writeByte(index);
            Utils.writeUTF(data, realClass.getCanonicalName());
            context.classToId.put(realClass.getCanonicalName(), context.classToId.size());
            context.idToClass.add(delegateMapping);
        }

        delegateMapping.writeClass(obj, data, context);

        return;
    }

    for (Field f : shortFields) {
        data.writeShort(f.getShort(obj));
    }

    for (Field f : intFields) {
        data.writeInt(f.getInt(obj));
    }

    for (Field f : booleanFields) {
        data.writeBoolean(f.getBoolean(obj));
    }

    for (Field f : enumFields) {
        data.writeByte(((Enum) f.get(obj)).ordinal());
    }

    for (Field f : floatFields) {
        data.writeFloat(f.getFloat(obj));
    }

    for (Field f : doubleFields) {
        data.writeDouble(f.getDouble(obj));
    }

    for (FieldObject f : objectFields) {
        Object cpt = f.field.get(obj);
        f.mapping.write(data, cpt, context);
    }
}

From source file:buildcraft.core.network.serializers.ClassMapping.java

License:Minecraft Mod Public

private void writeArray(Object obj, ByteBuf data, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {
    Class<? extends Object> cpt = mappedClass.getComponentType();

    switch (cptType) {
    case Byte: {
        byte[] arr = (byte[]) obj;
        data.writeInt(arr.length);

        data.writeBytes(arr);//from ww w .  j a  v a2  s. com

        break;
    }
    case Float: {
        float[] arr = (float[]) obj;
        data.writeInt(arr.length);

        for (float element : arr) {
            data.writeFloat(element);
        }

        break;
    }
    case Double: {
        double[] arr = (double[]) obj;
        data.writeInt(arr.length);

        for (double element : arr) {
            data.writeDouble(element);
        }

        break;
    }
    case Short: {
        short[] arr = (short[]) obj;
        data.writeInt(arr.length);

        for (short element : arr) {
            data.writeShort(element);
        }

        break;
    }
    case Int: {
        int[] arr = (int[]) obj;
        data.writeInt(arr.length);

        for (int element : arr) {
            data.writeInt(element);
        }

        break;
    }
    case Boolean: {
        boolean[] arr = (boolean[]) obj;
        data.writeInt(arr.length);

        for (boolean element : arr) {
            data.writeBoolean(element);
        }

        break;
    }
    case Object: {
        Object[] arr = (Object[]) obj;
        data.writeInt(arr.length);

        for (Object element : arr) {
            cptMapping.write(data, element, context);
        }

        break;
    }
    }
}