Example usage for io.netty.buffer ByteBuf readUnsignedShort

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

Introduction

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

Prototype

public abstract int readUnsignedShort();

Source Link

Document

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

Usage

From source file:at.yawk.accordion.codec.unsafe.EnumCodec.java

License:Mozilla Public License

@Override
public E decode(ByteBuf encoded) {
    return type.getEnumConstants()[encoded.readUnsignedShort()];
}

From source file:at.yawk.accordion.distributed.AbstractCollectionSynchronizer.java

License:Mozilla Public License

/**
 * Decode the entries encoded by #encode.
 *///from w ww .  j  a  va 2s . c  o  m
private Set<T> decode(ByteBuf message) {
    // ushort length
    int count = message.readUnsignedShort();
    // read individual entries
    Set<T> entries = new HashSet<>(count);
    for (int i = 0; i < count; i++) {
        entries.add(this.serializer.decode(message));
    }
    return entries;
}

From source file:at.yawk.accordion.netty.Framer.java

License:Mozilla Public License

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf msg, List<Object> out) throws Exception {
    if (msg.readableBytes() < 2) {
        // no (full) length header received yet, wait
        return;/* w  w  w.  jav  a  2 s . c  o m*/
    }

    // mark so we can reset here if the message isn't complete yet
    msg.markReaderIndex();
    int messageLength = msg.readUnsignedShort();
    if (messageLength > msg.readableBytes()) {
        // length header received but message not yet complete, wait
        msg.resetReaderIndex();
        return;
    }

    // message complete, queue for handling
    ByteBuf message = msg.readBytes(messageLength);
    out.add(message);
}

From source file:buildcraft.builders.TileArchitect.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf stream) {
    box.readData(stream);/*from   w  w  w .j av a 2  s .c  o m*/
    name = Utils.readUTF(stream);
    readConfiguration.readData(stream);
    int size = stream.readUnsignedShort();
    subLasers.clear();
    for (int i = 0; i < size; i++) {
        LaserData ld = new LaserData();
        ld.readData(stream);
        subLasers.add(ld);
    }
}

From source file:buildcraft.builders.TileBlueprintLibrary.java

License:Minecraft Mod Public

@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
    if (side.isClient()) {
        if ("requestSelectedBlueprint".equals(command)) {
            if (isOutputConsistent()) {
                if (selected > -1 && selected < currentPage.size()) {
                    // Work around 32k max limit on client->server
                    final BlueprintBase bpt = BuildCraftBuilders.clientDB.load(currentPage.get(selected));
                    final byte[] bptData = bpt.getData();
                    final int chunks = (bptData.length + CHUNK_SIZE - 1) / CHUNK_SIZE;

                    BuildCraftCore.instance
                            .sendToServer(new PacketCommand(this, "uploadServerBegin", new CommandWriter() {
                                public void write(ByteBuf data) {
                                    bpt.id.writeData(data);
                                    data.writeShort(chunks);
                                }/*w  ww  .j a v  a2  s. com*/
                            }));

                    for (int i = 0; i < chunks; i++) {
                        final int chunk = i;
                        final int start = CHUNK_SIZE * chunk;
                        final int length = Math.min(CHUNK_SIZE, bptData.length - start);
                        BuildCraftCore.instance
                                .sendToServer(new PacketCommand(this, "uploadServerChunk", new CommandWriter() {
                                    public void write(ByteBuf data) {
                                        data.writeShort(chunk);
                                        data.writeShort(length);
                                        data.writeBytes(bptData, start, length);
                                    }
                                }));
                    }

                    BuildCraftCore.instance.sendToServer(new PacketCommand(this, "uploadServerEnd", null));
                } else {
                    BuildCraftCore.instance
                            .sendToServer(new PacketCommand(this, "uploadNothingToServer", null));
                }
            }
        } else if ("downloadBlueprintToClient".equals(command)) {
            BlueprintId id = new BlueprintId();
            id.readData(stream);
            byte[] data = Utils.readByteArray(stream);

            try {
                NBTTagCompound nbt = CompressedStreamTools.func_152457_a(data, NBTSizeTracker.field_152451_a);
                BlueprintBase bpt = BlueprintBase.loadBluePrint(nbt);
                bpt.setData(data);
                bpt.id = id;

                BuildCraftBuilders.clientDB.add(bpt);
                setCurrentPage(BuildCraftBuilders.clientDB.getPage(pageId));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    } else if (side.isServer()) {
        if ("uploadNothingToServer".equals(command)) {
            setInventorySlotContents(3, getStackInSlot(2));
            setInventorySlotContents(2, null);

            downloadingPlayer = null;
        } else if ("uploadServerBegin".equals(command)) {
            blueprintDownloadId = new BlueprintId();
            blueprintDownloadId.readData(stream);
            blueprintDownload = new byte[CHUNK_SIZE * stream.readUnsignedShort()];
        } else if ("uploadServerChunk".equals(command)) {
            int start = stream.readUnsignedShort() * CHUNK_SIZE;
            int length = stream.readUnsignedShort();
            if (blueprintDownload != null) {
                stream.readBytes(blueprintDownload, start, length);
            } else {
                stream.skipBytes(length);
            }
        } else if ("uploadServerEnd".equals(command)) {
            try {
                NBTTagCompound nbt = CompressedStreamTools.func_152457_a(blueprintDownload,
                        NBTSizeTracker.field_152451_a);
                BlueprintBase bpt = BlueprintBase.loadBluePrint(nbt);
                bpt.setData(blueprintDownload);
                bpt.id = blueprintDownloadId;
                BuildCraftBuilders.serverDB.add(bpt);

                setInventorySlotContents(3, bpt.getStack());
                setInventorySlotContents(2, null);
            } catch (IOException e) {
                e.printStackTrace();
            }

            blueprintDownloadId = null;
            blueprintDownload = null;
            downloadingPlayer = null;
        }
    }
}

From source file:buildcraft.builders.TileBuilder.java

License:Minecraft Mod Public

@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
    super.receiveCommand(command, side, sender, stream);
    if (side.isClient()) {
        if ("clearItemRequirements".equals(command)) {
            requiredToBuild = null;// w w w . j  av  a2  s  .co m
        } else if ("setItemRequirements".equals(command)) {
            int size = stream.readUnsignedShort();
            requiredToBuild = new ArrayList<ItemStack>();
            for (int i = 0; i < size; i++) {
                ItemStack stack = Utils.readStack(stream);
                stack.stackSize = Math.min(999, stream.readUnsignedShort());
                requiredToBuild.add(stack);
            }
        }
    } else if (side.isServer()) {
        EntityPlayer player = (EntityPlayer) sender;
        if ("eraseFluidTank".equals(command)) {
            int id = stream.readInt();
            if (id < 0 || id >= fluidTanks.length) {
                return;
            }
            if (isUseableByPlayer(player) && player.getDistanceSq(xCoord, yCoord, zCoord) <= 64) {
                fluidTanks[id].setFluid(null);
                sendNetworkUpdate();
            }
        }
    }
}

From source file:buildcraft.builders.TileQuarry.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf stream) {
    super.readData(stream);
    box.readData(stream);/*from www. j a va  2 s .c o m*/
    targetX = stream.readInt();
    targetY = stream.readUnsignedShort();
    targetZ = stream.readInt();
    headPosX = stream.readDouble();
    headPosY = stream.readDouble();
    headPosZ = stream.readDouble();
    speed = stream.readFloat();
    headTrajectory = stream.readFloat();
    int flags = stream.readUnsignedByte();
    stage = Stage.values()[flags & 0x07];
    movingHorizontally = (flags & 0x10) != 0;
    movingVertically = (flags & 0x20) != 0;
    int newLedState = stream.readUnsignedByte();
    if (newLedState != ledState) {
        ledState = newLedState;
        worldObj.markBlockRangeForRenderUpdate(xCoord, yCoord, zCoord, xCoord, yCoord, zCoord);
    }

    createUtilsIfNeeded();

    if (arm != null) {
        arm.setHead(headPosX, headPosY, headPosZ);
        arm.updatePosition();
    }
}

From source file:buildcraft.builders.urbanism.TileUrbanist.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf stream) {
    frames.clear();/*from   w  ww  . j  av a 2s.  c o  m*/

    int size = stream.readUnsignedShort();
    for (int i = 0; i < size; i++) {
        AnchoredBox b = new AnchoredBox();
        b.readData(stream);
        frames.add(b);
    }
}

From source file:buildcraft.commander.ContainerZonePlan.java

License:Minecraft Mod Public

@Override
public void receiveCommand(String command, Side side, Object sender, ByteBuf stream) {
    if (side.isClient()) {
        if ("areaLoaded".equals(command)) {
            currentAreaSelection = new ZonePlan();
            currentAreaSelection.readData(stream);
            gui.refreshSelectedArea();//from  w  ww.j  a  v a2 s .c o  m
        } else if ("receiveImage".equals(command)) {
            int size = stream.readUnsignedMedium();

            for (int i = 0; i < size; ++i) {
                mapTexture.colorMap[i] = 0xFF000000
                        | MapColor.mapColorArray[stream.readUnsignedByte()].colorValue;
            }
        }
    } else if (side.isServer()) {
        if ("loadArea".equals(command)) {
            final int index = stream.readUnsignedByte();
            BuildCraftCore.instance.sendToPlayer((EntityPlayer) sender,
                    new PacketCommand(this, "areaLoaded", new CommandWriter() {
                        public void write(ByteBuf data) {
                            map.selectArea(index).writeData(data);
                        }
                    }));
        } else if ("saveArea".equals(command)) {
            final int index = stream.readUnsignedByte();
            ZonePlan plan = new ZonePlan();
            plan.readData(stream);
            map.setArea(index, plan);
        } else if ("computeMap".equals(command)) {
            computeMap(stream.readInt(), stream.readInt(), stream.readUnsignedShort(),
                    stream.readUnsignedShort(), stream.readUnsignedByte(), (EntityPlayer) sender);
        }
    }
}

From source file:buildcraft.core.builders.BuildingItem.java

License:Minecraft Mod Public

@Override
public void readData(ByteBuf stream) {
    origin = new Position();
    destination = new Position();
    origin.readData(stream);/*  w w w  .  jav a2s  .c o m*/
    destination.readData(stream);
    lifetime = stream.readDouble();
    stacksToDisplay.clear();
    int size = stream.readUnsignedShort();
    for (int i = 0; i < size; i++) {
        StackAtPosition e = new StackAtPosition();
        e.readData(stream);
        stacksToDisplay.add(e);
    }
}