Search in sources :

Example 6 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class CraftingDataPacket method encode.

@Override
public void encode() {
    this.reset();
    this.putUnsignedVarInt(entries.size());
    BinaryStream writer = new BinaryStream();
    for (Object entry : entries) {
        int entryType = writeEntry(entry, writer);
        if (entryType >= 0) {
            this.putVarInt(entryType);
            this.put(writer.getBuffer());
        } else {
            this.putVarInt(-1);
        }
        writer.reset();
    }
    this.putBoolean(cleanRecipes);
}
Also used : BinaryStream(cn.nukkit.utils.BinaryStream)

Example 7 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class AvailableCommandsPacket method encode.

@Override
public void encode() {
    this.reset();
    BinaryStream commandsStream = new BinaryStream();
    this.commands.forEach((name, versions) -> {
        if (name.equals("help"))
            return;
        ArrayList<String> aliases = new ArrayList<>();
        aliases.addAll(Arrays.asList(versions.versions.get(0).aliases));
        aliases.add(name);
        for (String alias : aliases) {
            commandsStream.putString(alias);
            commandsStream.putString(versions.versions.get(0).description);
            commandsStream.putByte((byte) 0);
            commandsStream.putByte((byte) 0);
            commandsStream.putLInt(-1);
            commandsStream.putUnsignedVarInt(versions.versions.get(0).overloads.size());
            for (CommandOverload overload : versions.versions.get(0).overloads.values()) {
                commandsStream.putUnsignedVarInt(overload.input.parameters.length);
                for (CommandParameter parameter : overload.input.parameters) {
                    commandsStream.putString(parameter.name);
                    commandsStream.putLInt(ARG_FLAG_VALID | getFlag(parameter.type));
                    commandsStream.putBoolean(parameter.optional);
                }
            }
        }
        aliasCommands += (aliases.size() - 1);
    });
    this.putUnsignedVarInt(0);
    this.putUnsignedVarInt(0);
    this.putUnsignedVarInt(0);
    this.putUnsignedVarInt(this.commands.size() + aliasCommands);
    this.put(commandsStream.getBuffer());
}
Also used : CommandParameter(cn.nukkit.command.data.CommandParameter) CommandOverload(cn.nukkit.command.data.CommandOverload) ArrayList(java.util.ArrayList) BinaryStream(cn.nukkit.utils.BinaryStream)

Example 8 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class CraftingManager method getMultiItemHash.

private String getMultiItemHash(Collection<Item> items) {
    BinaryStream stream = new BinaryStream();
    for (Item item : items) {
        stream.putUnsignedVarInt(item.getId());
        stream.putVarInt(item.getDamage());
    }
    return new String(stream.getByteArray());
}
Also used : Item(cn.nukkit.item.Item) BinaryStream(cn.nukkit.utils.BinaryStream)

Example 9 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class Session method handleSplit.

private void handleSplit(EncapsulatedPacket packet) throws Exception {
    if (packet.splitCount >= MAX_SPLIT_SIZE || packet.splitIndex >= MAX_SPLIT_SIZE || packet.splitIndex < 0) {
        return;
    }
    if (!this.splitPackets.containsKey(packet.splitID)) {
        if (this.splitPackets.size() >= MAX_SPLIT_COUNT) {
            return;
        }
        this.splitPackets.put(packet.splitID, new HashMap<Integer, EncapsulatedPacket>() {

            {
                put(packet.splitIndex, packet);
            }
        });
    } else {
        this.splitPackets.get(packet.splitID).put(packet.splitIndex, packet);
    }
    if (this.splitPackets.get(packet.splitID).size() == packet.splitCount) {
        EncapsulatedPacket pk = new EncapsulatedPacket();
        BinaryStream stream = new BinaryStream();
        for (int i = 0; i < packet.splitCount; i++) {
            stream.put(this.splitPackets.get(packet.splitID).get(i).buffer);
        }
        pk.buffer = stream.getBuffer();
        pk.length = pk.buffer.length;
        this.splitPackets.remove(packet.splitID);
        this.handleEncapsulatedPacketRoute(pk);
    }
}
Also used : BinaryStream(cn.nukkit.utils.BinaryStream) EncapsulatedPacket(cn.nukkit.raknet.protocol.EncapsulatedPacket)

Example 10 with BinaryStream

use of cn.nukkit.utils.BinaryStream in project Nukkit by Nukkit.

the class AcknowledgePacket method encode.

@Override
public void encode() {
    super.encode();
    int count = this.packets.size();
    int[] packets = new int[count];
    int index = 0;
    for (int i : this.packets.values()) {
        packets[index++] = i;
    }
    short records = 0;
    BinaryStream payload = new BinaryStream();
    if (count > 0) {
        int pointer = 1;
        int start = packets[0];
        int last = packets[0];
        while (pointer < count) {
            int current = packets[pointer++];
            int diff = current - last;
            if (diff == 1) {
                last = current;
            } else if (diff > 1) {
                if (start == last) {
                    payload.putByte((byte) 0x01);
                    payload.put(Binary.writeLTriad(start));
                    start = last = current;
                } else {
                    payload.putByte((byte) 0x00);
                    payload.put(Binary.writeLTriad(start));
                    payload.put(Binary.writeLTriad(last));
                    start = last = current;
                }
                ++records;
            }
        }
        if (start == last) {
            payload.putByte((byte) 0x01);
            payload.put(Binary.writeLTriad(start));
        } else {
            payload.putByte((byte) 0x00);
            payload.put(Binary.writeLTriad(start));
            payload.put(Binary.writeLTriad(last));
        }
        ++records;
    }
    this.putShort(records);
    this.buffer = Binary.appendBytes(this.buffer, payload.getBuffer());
}
Also used : BinaryStream(cn.nukkit.utils.BinaryStream)

Aggregations

BinaryStream (cn.nukkit.utils.BinaryStream)13 CompoundTag (cn.nukkit.nbt.tag.CompoundTag)5 BlockEntity (cn.nukkit.blockentity.BlockEntity)4 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 BaseFullChunk (cn.nukkit.level.format.generic.BaseFullChunk)3 Player (cn.nukkit.Player)2 BlockEntitySpawnable (cn.nukkit.blockentity.BlockEntitySpawnable)2 Entity (cn.nukkit.entity.Entity)2 ChunkException (cn.nukkit.utils.ChunkException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 CommandOverload (cn.nukkit.command.data.CommandOverload)1 CommandParameter (cn.nukkit.command.data.CommandParameter)1 Item (cn.nukkit.item.Item)1 FullChunk (cn.nukkit.level.format.FullChunk)1 LevelProvider (cn.nukkit.level.format.LevelProvider)1 EntitiesKey (cn.nukkit.level.format.leveldb.key.EntitiesKey)1 ExtraDataKey (cn.nukkit.level.format.leveldb.key.ExtraDataKey)1 TilesKey (cn.nukkit.level.format.leveldb.key.TilesKey)1 NBTInputStream (cn.nukkit.nbt.stream.NBTInputStream)1