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:org.spout.vanilla.protocol.codec.world.block.BlockActionCodec.java

License:Open Source License

@Override
public BlockActionMessage decode(ByteBuf buffer) throws IOException {
    int x = buffer.readInt();
    int y = buffer.readUnsignedShort();
    int z = buffer.readInt();
    byte firstByte = buffer.readByte();
    byte secondByte = buffer.readByte();
    short blockId = buffer.readShort();
    return new BlockActionMessage(x, y, z, blockId, firstByte, secondByte, NullRepositionManager.getInstance());
}

From source file:org.spout.vanilla.protocol.codec.world.block.BlockChangeCodec.java

License:Open Source License

@Override
public BlockChangeMessage decode(ByteBuf buffer) throws IOException {
    int x = buffer.readInt();
    int y = buffer.readUnsignedByte();
    int z = buffer.readInt();
    short type = buffer.readShort();
    int metadata = buffer.readUnsignedByte();
    return new BlockChangeMessage(x, y, z, type, metadata, NullRepositionManager.getInstance());
}

From source file:org.spout.vanilla.protocol.codec.world.block.SignCodec.java

License:Open Source License

@Override
public SignMessage decode(ByteBuf buffer) throws IOException {
    int x = buffer.readInt();
    int y = buffer.readShort();
    int z = buffer.readInt();
    String[] message = new String[4];
    for (int i = 0; i < message.length; i++) {
        String line = VanillaByteBufUtils.readString(buffer);
        if (line == null) {
            line = "";
        }/* w w w. jav  a2  s . co  m*/
        message[i] = line;
    }
    return new SignMessage(x, y, z, message, NullRepositionManager.getInstance());
}

From source file:org.spout.vanilla.protocol.codec.world.chunk.ChunkBulkCodec.java

License:Open Source License

@Override
public ChunkBulkMessage decode(ByteBuf buffer) throws IOException {
    int length = buffer.readShort() & 0xFFFF;

    int compressed = buffer.readInt();

    byte[] compressedDataFlat = new byte[compressed];

    buffer.readBytes(compressedDataFlat);

    int[] x = new int[length];
    int[] z = new int[length];
    byte[][][] data = new byte[length][][];
    boolean[][] hasAdd = new boolean[length][];
    byte[][] biomeData = new byte[length][];

    for (int i = 0; i < length; i++) {
        x[i] = buffer.readInt();//from  www  .  j  a v  a 2 s . c o m
        z[i] = buffer.readInt();
        short primaryBitmap = buffer.readShort();
        hasAdd[i] = shortToBooleanArray(buffer.readShort());
        data[i] = shortToByteByteArray(primaryBitmap, hasAdd[i]);
        biomeData[i] = new byte[Chunk.BLOCKS.AREA];
    }

    int flatLength = 0;

    for (int i = 0; i < length; i++) {
        byte[][] columnData = data[i];
        for (int j = 0; j < columnData.length; j++) {
            flatLength += columnData[j].length;
        }
        flatLength += biomeData[i].length;
    }

    byte[] uncompressedDataFlat = new byte[flatLength];

    Inflater inflater = new Inflater();
    inflater.setInput(compressedDataFlat);
    inflater.getRemaining();
    try {
        int uncompressed = inflater.inflate(uncompressedDataFlat);
        if (uncompressed == 0) {
            throw new IOException("Not all bytes uncompressed.");
        } else if (uncompressed != uncompressedDataFlat.length) {
            throw new IOException("Wrong length for compressed data");
        }
    } catch (DataFormatException e) {
        e.printStackTrace();
        throw new IOException("Bad compressed data.");
    } finally {
        inflater.end();
    }

    int pos = 0;
    for (int i = 0; i < length; i++) {
        byte[][] columnData = data[i];
        for (int j = 0; j < columnData.length; j++) {
            int l = columnData[j].length;
            System.arraycopy(uncompressedDataFlat, pos, columnData[j], 0, l);
            pos += l;
        }
        int l = biomeData[i].length;
        System.arraycopy(uncompressedDataFlat, pos, biomeData[i], 0, l);
        pos += l;
    }

    if (pos != flatLength) {
        throw new IllegalStateException("Flat data length miscalculated");
    }

    return new ChunkBulkMessage(x, z, hasAdd, data, biomeData, NullRepositionManager.getInstance());
}

From source file:org.spout.vanilla.protocol.codec.world.chunk.ChunkDataCodec.java

License:Open Source License

@Override
public ChunkDataMessage decode(ByteBuf buffer) throws IOException {
    int x = buffer.readInt();
    int z = buffer.readInt();
    boolean contiguous = buffer.readByte() == 1;

    short primaryBitMap = buffer.readShort();
    short addBitMap = buffer.readShort();
    int compressedSize = buffer.readInt();
    byte[] compressedData = new byte[compressedSize];
    buffer.readBytes(compressedData);//from w  ww .  ja  v  a2 s . com

    boolean[] hasAdditionalData = new boolean[MAX_SECTIONS];
    byte[][] data = new byte[MAX_SECTIONS][];

    int size = 0;
    for (int i = 0; i < MAX_SECTIONS; ++i) {
        if ((primaryBitMap & 1 << i) != 0) { // This chunk exists! Let's initialize the data for it.
            int sectionSize = Chunk.BLOCKS.HALF_VOLUME * 5;
            if ((addBitMap & 1 << i) != 0) {
                hasAdditionalData[i] = true;
                sectionSize += Chunk.BLOCKS.HALF_VOLUME;
            }

            data[i] = new byte[sectionSize];
            size += sectionSize;
        }
    }

    if (contiguous) {
        size += Chunk.BLOCKS.AREA;
    }

    byte[] uncompressedData = new byte[size];

    Inflater inflater = new Inflater();
    inflater.setInput(compressedData);
    inflater.getRemaining();
    int index = 0;
    try {
        while (index < uncompressedData.length) {
            int uncompressed = inflater.inflate(uncompressedData, index, uncompressedData.length - index);
            index += uncompressed;
            if (uncompressed == 0) {
                break;
            }
        }
        if (index != uncompressedData.length) {
            throw new IOException("Not all bytes uncompressed.");
        }
    } catch (DataFormatException e) {
        e.printStackTrace();
        throw new IOException("Bad compressed data.");
    } finally {
        inflater.end();
    }

    size = 0;
    // TODO - fix this total hack
    size = readSectionData(uncompressedData, size, data, 0, 4096);
    size = readSectionData(uncompressedData, size, data, 2 * 2048, 2048);
    size = readSectionData(uncompressedData, size, data, 3 * 2048, 2048);
    size = readSectionData(uncompressedData, size, data, 4 * 2048, 2048);

    /*size = 0;
    for (byte[] sectionData : data) {
       if (sectionData != null && sectionData.length + size < uncompressedData.length) {
    System.arraycopy(uncompressedData, size, sectionData, 0, sectionData.length);
    size += sectionData.length;
       }
    }*/
    byte[] biomeData = new byte[Chunk.BLOCKS.AREA];

    if (contiguous) {
        System.arraycopy(uncompressedData, size, biomeData, 0, biomeData.length);
        size += biomeData.length;
    }

    return new ChunkDataMessage(x, z, contiguous, hasAdditionalData, data, biomeData, null,
            NullRepositionManager.getInstance());
}

From source file:org.spout.vanilla.protocol.VanillaByteBufUtils.java

License:Open Source License

/**
 * Reads a list of parameters from the buffer.
 *
 * @param buf The buffer.//from  ww w .j a va  2s . c  o  m
 * @return The parameters.
 */
public static List<Parameter<?>> readParameters(ByteBuf buf) throws IOException {
    List<Parameter<?>> parameters = new ArrayList<Parameter<?>>();

    for (int b = buf.readUnsignedByte(); b != 127; b = buf.readUnsignedByte()) {
        int type = (b & 0xE0) >> 5;
        int index = b & 0x1F;

        switch (type) {
        case Parameter.TYPE_BYTE:
            parameters.add(new Parameter<Byte>(type, index, buf.readByte()));
            break;
        case Parameter.TYPE_SHORT:
            parameters.add(new Parameter<Short>(type, index, buf.readShort()));
            break;
        case Parameter.TYPE_INT:
            parameters.add(new Parameter<Integer>(type, index, buf.readInt()));
            break;
        case Parameter.TYPE_FLOAT:
            parameters.add(new Parameter<Float>(type, index, buf.readFloat()));
            break;
        case Parameter.TYPE_STRING:
            parameters.add(new Parameter<String>(type, index, readString(buf)));
            break;
        case Parameter.TYPE_ITEM:
            parameters.add(new Parameter<ItemStack>(type, index, readItemStack(buf)));
            break;
        }
    }

    return parameters;
}

From source file:org.spout.vanilla.protocol.VanillaByteBufUtils.java

License:Open Source License

public static ItemStack readItemStack(ByteBuf buffer) throws IOException {
    short id = buffer.readShort();
    if (id < 0) {
        return null;
    } else {/*from  ww  w. j  a  v  a2s.com*/
        Material material = VanillaMaterials.getMaterial(id);
        if (material == null) {
            throw new IOException("Unknown material with id of " + id);
        }
        int count = buffer.readUnsignedByte();
        int damage = buffer.readUnsignedShort();
        CompoundMap nbtData = readCompound(buffer);
        return new ItemStack(material, damage, count).setNBTData(nbtData);
    }
}

From source file:org.spoutcraft.client.network.codec.play.ChunkDataBulkCodec.java

License:MIT License

@Override
public ChunkDataBulkMessage decode(ByteBuf buf) throws IOException {
    final short columnCount = buf.readShort();
    final int compressedDataLength = buf.readInt();
    final boolean hasSkyLight = buf.readBoolean();
    final byte[] compressedData = new byte[compressedDataLength];
    buf.readBytes(compressedData, 0, compressedData.length);
    final int[] columnXs = new int[columnCount];
    final int[] columnZs = new int[columnCount];
    final short[] primaryBitMaps = new short[columnCount];
    final short[] additionalDataBitMaps = new short[columnCount];
    for (int i = 0; i < columnCount; i++) {
        columnXs[i] = buf.readInt();//w ww.  ja v a  2s .  c o  m
        columnZs[i] = buf.readInt();
        primaryBitMaps[i] = (short) buf.readUnsignedShort();
        additionalDataBitMaps[i] = (short) buf.readUnsignedShort();
    }
    return new ChunkDataBulkMessage(columnCount, compressedDataLength, hasSkyLight, compressedData, columnXs,
            columnZs, primaryBitMaps, additionalDataBitMaps);
}

From source file:org.spoutcraft.mod.protocol.codec.AddFileCodec.java

License:MIT License

@Override
public AddFileMessage decode(Spoutcraft game, ByteBuf buffer) throws IOException {
    if (game.getSide().isServer()) {
        throw new IOException("Server is not allowed to receive files!");
    }//from ww w  .  j  a va  2s.  c  o  m
    final String addonIdentifier = BufferUtil.readUTF8(buffer);
    final String fname = BufferUtil.readUTF8(buffer);
    final short part = buffer.readShort();
    final short partCount = buffer.readShort();
    final byte[] data = new byte[buffer.readableBytes()];
    buffer.readBytes(data);
    return new AddFileMessage(addonIdentifier, fname, part, partCount, data);
}

From source file:org.starmod.net.object.field.type.NetworkShort.java

License:MIT License

@Override
public void decode(ByteBuf buf) {
    setValue(buf.readShort());
}