Example usage for io.netty.buffer ByteBuf writeShort

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

Introduction

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

Prototype

public abstract ByteBuf writeShort(int value);

Source Link

Document

Sets the specified 16-bit short integer at the current writerIndex and increases the writerIndex by 2 in this buffer.

Usage

From source file:buildcraft.core.lib.network.PacketTileUpdate.java

License:Minecraft Mod Public

@Override
public void writeIdentificationData(ByteBuf data) {
    data.writeInt(posX);
    data.writeShort(posY);
    data.writeInt(posZ);
}

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

License:Minecraft Mod Public

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

    try {//from  w ww  . j  a v  a2  s . c  o  m
        byte[] compressed = CompressedStreamTools.compress(nbttagcompound);
        data.writeShort(compressed.length);
        data.writeBytes(compressed);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

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);//from   w  w  w . ja  v a 2 s  .co m
    data.writeInt(tile.yCoord);
    data.writeInt(tile.zCoord);

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

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 {//  www .j av a  2 s.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 a2 s  . co  m*/

    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  w ww  . j a v  a2s  . co  m
    } 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);/*www.ja  v  a  2 s  .  com*/

        data.writeBytes(arr);

        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;
    }
    }
}

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

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {

    ArrayList list = (ArrayList) o;

    if (o == null) {
        data.writeBoolean(false);//from w  w w.  jav  a  2  s . c  o  m
    } else {
        data.writeBoolean(true);
        data.writeShort(list.size());

        for (Object val : list) {
            anonymousSerializer.write(data, val, context);
        }
    }
}

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

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context) {
    FluidStack stack = (FluidStack) o;/*from   w  w  w  . j a v  a2  s.com*/

    if (stack == null) {
        data.writeBoolean(false);
    } else {
        data.writeShort(stack.getFluid().getID());
        data.writeInt(stack.amount);

        if (stack.tag == null) {
            data.writeBoolean(false);
        } else {
            data.writeBoolean(true);
            Utils.writeNBT(data, stack.tag);
        }
    }

}

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

License:Minecraft Mod Public

@Override
public void write(ByteBuf data, Object o, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException {

    HashMap map = (HashMap) o;

    if (o == null) {
        data.writeBoolean(false);//from  ww  w  .  ja va2 s.  c o m
    } else {
        data.writeBoolean(true);
        data.writeShort(map.size());

        Set<Map.Entry> s = map.entrySet();

        for (Map.Entry e : s) {
            anonymousSerializer.write(data, e.getKey(), context);
            anonymousSerializer.write(data, e.getValue(), context);
        }
    }
}