Example usage for io.netty.buffer ByteBuf readShort

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

Introduction

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

Prototype

public abstract short readShort();

Source Link

Document

Gets a 16-bit short integer at the current readerIndex and increases the readerIndex by 2 in this buffer.

Usage

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

License:Minecraft Mod Public

@SuppressWarnings("rawtypes")
Object readClass(Object objI, ByteBuf data, SerializationContext context) throws IllegalArgumentException,
        IllegalAccessException, InstantiationException, ClassNotFoundException {

    Object obj = objI;// w  ww.  ja v  a2 s  . c  o  m

    // The data layout for an object is the following:
    // [boolean] does the object exist (e.g. non-null)
    // {false} exit
    // [int] what is the object real class?
    // {0} the same as the declared class
    // {1-x} a different one
    // [string] if the number is not yet registered, the name of the
    // class
    // [bytes] the actual contents

    int index = data.readByte();

    if (index != 0) {
        ClassMapping delegateMapping;

        if (context.idToClass.size() < index) {
            String className = Utils.readUTF(data);

            Class cls = Class.forName(className);

            delegateMapping = (ClassMapping) get(cls);

            context.idToClass.add(get(cls));
        } else {
            delegateMapping = (ClassMapping) context.idToClass.get(index - 1);
        }

        return delegateMapping.readClass(obj, data, context);
    }

    if (obj == null) {
        obj = mappedClass.newInstance();
    }

    for (Field f : shortFields) {
        f.setShort(obj, data.readShort());
    }

    for (Field f : intFields) {
        f.setInt(obj, data.readInt());
    }

    for (Field f : booleanFields) {
        f.setBoolean(obj, data.readBoolean());
    }

    for (Field f : enumFields) {
        f.set(obj, ((Class) f.getGenericType()).getEnumConstants()[data.readByte()]);
    }

    for (Field f : floatFields) {
        f.setFloat(obj, data.readFloat());
    }

    for (Field f : doubleFields) {
        f.setDouble(obj, data.readDouble());
    }

    for (FieldObject f : objectFields) {
        f.field.set(obj, f.mapping.read(data, f.field.get(obj), context));
    }

    return obj;
}

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

License:Minecraft Mod Public

private Object readArray(Object objI, ByteBuf data, SerializationContext context)
        throws IllegalArgumentException, IllegalAccessException, InstantiationException,
        ClassNotFoundException {/*from w  w  w. jav a 2s .  c om*/
    Object obj = objI;

    Class<? extends Object> cpt = mappedClass.getComponentType();

    int size = data.readInt();

    switch (cptType) {
    case Byte: {
        byte[] arr;

        if (obj == null) {
            arr = new byte[size];
        } else {
            arr = (byte[]) obj;
        }

        data.readBytes(arr);

        obj = arr;

        break;
    }
    case Float: {
        float[] arr;

        if (obj == null) {
            arr = new float[size];
        } else {
            arr = (float[]) obj;
        }

        for (int i = 0; i < arr.length; ++i) {
            arr[i] = data.readFloat();
        }

        obj = arr;

        break;
    }
    case Double: {
        double[] arr;

        if (obj == null) {
            arr = new double[size];
        } else {
            arr = (double[]) obj;
        }

        for (int i = 0; i < arr.length; ++i) {
            arr[i] = data.readDouble();
        }

        obj = arr;

        break;
    }
    case Short: {
        short[] arr;

        if (obj == null) {
            arr = new short[size];
        } else {
            arr = (short[]) obj;
        }

        for (int i = 0; i < arr.length; ++i) {
            arr[i] = data.readShort();
        }

        obj = arr;

        break;
    }
    case Int: {
        int[] arr;

        if (obj == null) {
            arr = new int[size];
        } else {
            arr = (int[]) obj;
        }

        for (int i = 0; i < arr.length; ++i) {
            arr[i] = data.readInt();
        }

        obj = arr;

        break;
    }
    case Boolean: {
        boolean[] arr;

        if (obj == null) {
            arr = new boolean[size];
        } else {
            arr = (boolean[]) obj;
        }

        for (int i = 0; i < arr.length; ++i) {
            arr[i] = data.readBoolean();
        }

        obj = arr;

        break;
    }
    case Object: {
        Object[] arr;

        if (obj == null) {
            arr = (Object[]) Array.newInstance(cpt, size);
        } else {
            arr = (Object[]) obj;
        }

        for (int i = 0; i < arr.length; ++i) {
            arr[i] = cptMapping.read(data, arr[i], context);
        }

        obj = arr;

        break;
    }
    }

    return obj;
}

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

License:Minecraft Mod Public

@Override
public Object read(ByteBuf data, Object o, SerializationContext context) throws IllegalArgumentException,
        IllegalAccessException, InstantiationException, ClassNotFoundException {

    if (!data.readBoolean()) {
        return null;
    } else {//from  www  . ja  v  a 2s .c  o  m
        int size = data.readShort();

        ArrayList list = new ArrayList();

        for (int i = 0; i < size; ++i) {
            Object val = anonymousSerializer.read(data, null, context);

            list.add(val);
        }

        return list;
    }
}

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

License:Minecraft Mod Public

@Override
public Object read(ByteBuf data, Object o, SerializationContext context) {
    if (!data.readBoolean()) {
        return null;
    } else {/*from   w w w. j  a v  a 2 s . com*/
        int id = data.readShort();
        int amount = data.readerIndex();

        NBTTagCompound nbt = null;

        if (data.readBoolean()) {
            nbt = Utils.readNBT(data);
            return new FluidStack(id, amount, nbt);
        } else {
            return new FluidStack(id, amount);
        }
    }
}

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

License:Minecraft Mod Public

@Override
public Object read(ByteBuf data, Object o, SerializationContext context) throws IllegalArgumentException,
        IllegalAccessException, InstantiationException, ClassNotFoundException {

    if (!data.readBoolean()) {
        return null;
    } else {// ww w.  j  a va2s  .  c  o  m
        int size = data.readShort();

        HashMap map = new HashMap();

        for (int i = 0; i < size; ++i) {
            Object key = anonymousSerializer.read(data, null, context);
            Object value = anonymousSerializer.read(data, null, context);

            map.put(key, value);
        }

        return map;
    }
}

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

License:Minecraft Mod Public

@Override
public Object read(ByteBuf data, Object o, SerializationContext context) throws IllegalArgumentException,
        IllegalAccessException, InstantiationException, ClassNotFoundException {
    if (!data.readBoolean()) {
        return null;
    } else {/*w  w  w.  ja  v a2s. co  m*/
        int size = data.readShort();

        HashSet<Object> set = new HashSet<Object>();

        for (int i = 0; i < size; ++i) {
            Object value = anonymousSerializer.read(data, null, context);

            set.add(value);
        }

        return set;
    }
}

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

License:Minecraft Mod Public

@Override
public Object read(ByteBuf data, Object o, SerializationContext context) throws IllegalArgumentException,
        IllegalAccessException, InstantiationException, ClassNotFoundException {

    if (!data.readBoolean()) {
        return null;
    } else {//from  www. ja v  a2s  .  com
        int size = data.readShort();

        LinkedList list = new LinkedList();

        for (int i = 0; i < size; ++i) {
            Object val = anonymousSerializer.read(data, null, context);

            list.add(val);
        }

        return list;
    }
}

From source file:buildcraft.factory.network.PacketHandlerFactory.java

License:Minecraft Mod Public

private void onRefinerySelect(EntityPlayer playerEntity, PacketUpdate packet) throws IOException {

    TileRefinery tile = getRefinery(playerEntity.worldObj, packet.posX, packet.posY, packet.posZ);
    if (tile == null || packet.payload == null) {
        return;//from   ww w  .  j  a v a  2 s  . c om
    }

    ByteBuf stream = packet.payload.stream;

    tile.setFilter(stream.readByte(), FluidRegistry.getFluid(stream.readShort()));
}

From source file:buildcraft.robotics.EntityRobot.java

License:Minecraft Mod Public

@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
    if (side.isClient()) {
        if ("clientSetItemInUse".equals(command)) {
            itemInUse = NetworkUtils.readStack(stream);
        } else if ("clientSetInventory".equals(command)) {
            int slot = stream.readUnsignedShort();
            inv[slot] = NetworkUtils.readStack(stream);
        } else if ("initialize".equals(command)) {
            itemInUse = NetworkUtils.readStack(stream);
            itemActive = stream.readBoolean();
        } else if ("setItemActive".equals(command)) {
            itemActive = stream.readBoolean();
            itemActiveStage = 0;//w  ww . j  a v  a 2  s.c o  m
            lastUpdateTime = new Date().getTime();

            if (!itemActive) {
                setSteamDirection(0, -1, 0);
            }
        } else if ("setSteamDirection".equals(command)) {
            setSteamDirection(stream.readInt(), stream.readShort(), stream.readInt());
        } else if ("syncWearables".equals(command)) {
            wearables.clear();

            int amount = stream.readUnsignedByte();
            while (amount > 0) {
                wearables.add(NetworkUtils.readStack(stream));
                amount--;
            }
        }
    } else if (side.isServer()) {
        EntityPlayer p = (EntityPlayer) sender;
        if ("requestInitialization".equals(command)) {
            BuildCraftCore.instance.sendToPlayer(p, new PacketCommand(this, "initialize", new CommandWriter() {
                public void write(ByteBuf data) {
                    NetworkUtils.writeStack(data, itemInUse);
                    data.writeBoolean(itemActive);
                }
            }));

            for (int i = 0; i < inv.length; ++i) {
                final int j = i;
                BuildCraftCore.instance.sendToPlayer(p,
                        new PacketCommand(this, "clientSetInventory", new CommandWriter() {
                            public void write(ByteBuf data) {
                                data.writeShort(j);
                                NetworkUtils.writeStack(data, inv[j]);
                            }
                        }));
            }

            if (currentDockingStation != null) {
                setSteamDirection(currentDockingStation.side.offsetX, currentDockingStation.side.offsetY,
                        currentDockingStation.side.offsetZ);
            } else {
                setSteamDirection(0, -1, 0);
            }
        }
    }
}

From source file:buildcraft.robotics.TileZonePlan.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf stream) {
    progress = stream.readShort();
    mapName = NetworkUtils.readUTF(stream);
    stream.readBytes(previewColors, 0, 80);
    previewColorsPushed = false;/* w w  w  .j  a v  a2 s.c om*/
}