cn.nukkit.utils.BinaryStream Java Examples

The following examples show how to use cn.nukkit.utils.BinaryStream. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
private static int writeShapedRecipe(ShapedRecipe recipe, BinaryStream stream) {
    stream.putVarInt(recipe.getWidth());
    stream.putVarInt(recipe.getHeight());

    for (int z = 0; z < recipe.getHeight(); ++z) {
        for (int x = 0; x < recipe.getWidth(); ++x) {
            stream.putSlot(recipe.getIngredient(x, z));
        }
    }

    stream.putUnsignedVarInt(1);
    stream.putSlot(recipe.getResult());

    stream.putUUID(recipe.getId());

    return CraftingDataPacket.ENTRY_SHAPED;
}
 
Example #2
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #3
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] toFastBinary() {
    BinaryStream stream = new BinaryStream(new byte[65536]);
    stream.put(Binary.writeInt(this.getX()));
    stream.put(Binary.writeInt(this.getZ()));
    stream.put(this.getBlockIdArray());
    stream.put(this.getBlockDataArray());
    stream.put(this.getBlockSkyLightArray());
    stream.put(this.getBlockLightArray());
    stream.put(this.getHeightMapArray());
    for (int color : this.getBiomeColorArray()) {
        stream.put(Binary.writeInt(color));
    }
    stream.putByte((byte) ((this.isLightPopulated() ? 1 << 2 : 0) + (this.isPopulated() ? 1 << 2 : 0) + (this.isGenerated() ? 1 : 0)));
    return stream.getBuffer();
}
 
Example #4
Source File: TextPacket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void decode() {
    this.type = (byte) getByte();
    this.isLocalized = this.getBoolean() || type == TYPE_TRANSLATION;
    switch (type) {
        case TYPE_CHAT:
        case TYPE_WHISPER:
        case TYPE_ANNOUNCEMENT:
            this.source = this.getString();
        case TYPE_RAW:
        case TYPE_TIP:
        case TYPE_SYSTEM:
        case TYPE_JSON:
            this.message = this.getString();
            break;

        case TYPE_TRANSLATION:
        case TYPE_POPUP:
        case TYPE_JUKEBOX_POPUP:
            this.message = this.getString();
            this.parameters = this.getArray(String.class, BinaryStream::getString);
    }
    this.xboxUserId = this.getString();
    this.platformChatId = this.getString();
}
 
Example #5
Source File: CraftingDataPacket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@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);
}
 
Example #6
Source File: Chunk.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public byte[] toFastBinary() {
    BinaryStream stream = new BinaryStream(new byte[65536]);
    stream.put(Binary.writeInt(this.x));
    stream.put(Binary.writeInt(this.z));
    stream.put(this.getBlockIdArray());
    stream.put(this.getBlockDataArray());
    stream.put(this.getBlockSkyLightArray());
    stream.put(this.getBlockLightArray());
    for (int height : this.getHeightMapArray()) {
        stream.putByte((byte) height);
    }
    for (int color : this.getBiomeColorArray()) {
        stream.put(Binary.writeInt(color));
    }
    stream.putByte((byte) ((this.isLightPopulated() ? 1 << 2 : 0) + (this.isPopulated() ? 1 << 2 : 0) + (this.isGenerated() ? 1 : 0)));
    return stream.getBuffer();
}
 
Example #7
Source File: CraftingDataPacket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
private static int writeShapedRecipe(ShapedRecipe recipe, BinaryStream stream) {
    stream.putVarInt(recipe.getWidth());
    stream.putVarInt(recipe.getHeight());

    for (int z = 0; z < recipe.getHeight(); ++z) {
        for (int x = 0; x < recipe.getWidth(); ++x) {
            stream.putSlot(recipe.getIngredient(x, z));
        }
    }

    stream.putUnsignedVarInt(1);
    stream.putSlot(recipe.getResult());

    stream.putUUID(recipe.getId());

    return CraftingDataPacket.ENTRY_SHAPED;
}
 
Example #8
Source File: EmptyChunkSection.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void writeTo(BinaryStream stream) {
    stream.putByte((byte) 8);
    stream.putByte((byte) 2);
    EMPTY_STORAGE.writeTo(stream);
    EMPTY_STORAGE.writeTo(stream);
}
 
Example #9
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private static int writeShapelessRecipe(ShapelessRecipe recipe, BinaryStream stream) {
    stream.putUnsignedVarInt(recipe.getIngredientCount());

    for (Item item : recipe.getIngredientList()) {
        stream.putSlot(item);
    }

    stream.putUnsignedVarInt(1);
    stream.putSlot(recipe.getResult());
    stream.putUUID(recipe.getId());

    return CraftingDataPacket.ENTRY_SHAPELESS;
}
 
Example #10
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public byte[] toFastBinary() {
    BinaryStream stream = new BinaryStream(new byte[65536]);
    stream.put(Binary.writeInt(this.getX()));
    stream.put(Binary.writeInt(this.getZ()));
    stream.put(this.getBlockIdArray());
    stream.put(this.getBlockDataArray());
    stream.put(this.getBlockSkyLightArray());
    stream.put(this.getBlockLightArray());
    stream.put(this.getHeightMapArray());
    stream.put(this.getBiomeIdArray());
    stream.putByte((byte) ((this.isLightPopulated() ? 1 << 2 : 0) + (this.isPopulated() ? 1 << 2 : 0) + (this.isGenerated() ? 1 : 0)));
    return stream.getBuffer();
}
 
Example #11
Source File: AvailableCommandsPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example #12
Source File: CraftingDataPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private static int writeEnchantList(EnchantmentList list, BinaryStream stream) {
    stream.putByte((byte) list.getSize());
    for (int i = 0; i < list.getSize(); ++i) {
        EnchantmentEntry entry = list.getSlot(i);
        stream.putUnsignedVarInt(entry.getCost());
        stream.putUnsignedVarInt(entry.getEnchantments().length);
        for (Enchantment enchantment : entry.getEnchantments()) {
            stream.putUnsignedVarInt(enchantment.getId());
            stream.putUnsignedVarInt(enchantment.getLevel());
        }
        stream.putString(entry.getRandomName());
    }
    return CraftingDataPacket.ENTRY_ENCHANT_LIST;
}
 
Example #13
Source File: CraftingDataPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private static int writeFurnaceRecipe(FurnaceRecipe recipe, BinaryStream stream) {
    if (recipe.getInput().hasMeta()) { //Data recipe
        stream.putVarInt(recipe.getInput().getId());
        stream.putVarInt(recipe.getInput().getDamage());
        stream.putSlot(recipe.getResult());

        return CraftingDataPacket.ENTRY_FURNACE_DATA;
    } else {
        stream.putVarInt(recipe.getInput().getId());
        stream.putSlot(recipe.getResult());

        return CraftingDataPacket.ENTRY_FURNACE;
    }
}
 
Example #14
Source File: BlockStorage.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void writeTo(BinaryStream stream) {
    PalettedBlockStorage storage = new PalettedBlockStorage();
    for (int i = 0; i < SECTION_SIZE; i++) {
        storage.setBlock(i, GlobalBlockPalette.getOrCreateRuntimeId(blockIds[i] & 0xff, blockData.get(i)));
    }
    storage.writeTo(stream);
}
 
Example #15
Source File: PalettedBlockStorage.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void writeTo(BinaryStream stream) {
    stream.putByte((byte) getPaletteHeader(bitArray.getVersion(), true));

    for (int word : bitArray.getWords()) {
        stream.putLInt(word);
    }

    stream.putVarInt(palette.size());
    palette.forEach((IntConsumer) stream::putVarInt);
}
 
Example #16
Source File: Session.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #17
Source File: CraftingManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private static UUID getMultiItemHash(Collection<Item> items) {
    BinaryStream stream = new BinaryStream();
    for (Item item : items) {
        stream.putVarInt(getFullItemHash(item));
    }
    return UUID.nameUUIDFromBytes(stream.getBuffer());
}
 
Example #18
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Writing")
@Test
void testWrite() throws IOException {
	BinaryStream bs = new BinaryStream();
	VarInt.writeUnsignedVarInt(bs, 237356812);
	VarInt.writeVarInt(bs, 0xea3eca71);
	VarInt.writeUnsignedVarLong(bs, 0x1234567812345678L);
	VarInt.writeVarLong(bs, 0xea3eca710becececL);
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(bs)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(bs)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(bs)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(bs))
	);
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	VarInt.writeUnsignedVarInt(os, 237356812);
	VarInt.writeVarInt(os, 0xea3eca71);
	VarInt.writeUnsignedVarLong(os, 0x1234567812345678L);
	VarInt.writeVarLong(os, 0xea3eca710becececL);
	VarInt.writeVarInt(os, 0x7FFFFFFF);
	VarInt.writeVarInt(os, -1);
	ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(is)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(is)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(is)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(is)),
			() -> assertEquals(0x7FFFFFFF, VarInt.readVarInt(is)),
			() -> assertEquals(-1, VarInt.readVarInt(is))
	);
}
 
Example #19
Source File: CraftingDataPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private static int writeShapelessRecipe(ShapelessRecipe recipe, BinaryStream stream) {
    stream.putUnsignedVarInt(recipe.getIngredientCount());

    for (Item item : recipe.getIngredientList()) {
        stream.putSlot(item);
    }

    stream.putUnsignedVarInt(1);
    stream.putSlot(recipe.getResult());
    stream.putUUID(recipe.getId());

    return CraftingDataPacket.ENTRY_SHAPELESS;
}
 
Example #20
Source File: CraftingDataPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
private static int writeEntry(Object entry, BinaryStream stream) {
    if (entry instanceof ShapelessRecipe) {
        return writeShapelessRecipe(((ShapelessRecipe) entry), stream);
    } else if (entry instanceof ShapedRecipe) {
        return writeShapedRecipe(((ShapedRecipe) entry), stream);
    } else if (entry instanceof FurnaceRecipe) {
        return writeFurnaceRecipe(((FurnaceRecipe) entry), stream);
    } else if (entry instanceof EnchantmentList) {
        return writeEnchantList(((EnchantmentList) entry), stream);
    }
    return -1;
}
 
Example #21
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private static int writeFurnaceRecipe(FurnaceRecipe recipe, BinaryStream stream) {
    if (recipe.getInput().hasMeta()) { //Data recipe
        stream.putVarInt(recipe.getInput().getId());
        stream.putVarInt(recipe.getInput().getDamage());
        stream.putSlot(recipe.getResult());

        return CraftingDataPacket.ENTRY_FURNACE_DATA;
    } else {
        stream.putVarInt(recipe.getInput().getId());
        stream.putSlot(recipe.getResult());

        return CraftingDataPacket.ENTRY_FURNACE;
    }
}
 
Example #22
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private static int writeEnchantList(EnchantmentList list, BinaryStream stream) {
    stream.putByte((byte) list.getSize());
    for (int i = 0; i < list.getSize(); ++i) {
        EnchantmentEntry entry = list.getSlot(i);
        stream.putUnsignedVarInt(entry.getCost());
        stream.putUnsignedVarInt(entry.getEnchantments().length);
        for (Enchantment enchantment : entry.getEnchantments()) {
            stream.putUnsignedVarInt(enchantment.getId());
            stream.putUnsignedVarInt(enchantment.getLevel());
        }
        stream.putString(entry.getRandomName());
    }
    return CraftingDataPacket.ENTRY_ENCHANT_LIST;
}
 
Example #23
Source File: AvailableCommandsPacket.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
@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());
}
 
Example #24
Source File: CraftingDataPacket.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
private static int writeEntry(Object entry, BinaryStream stream) {
    if (entry instanceof ShapelessRecipe) {
        return writeShapelessRecipe(((ShapelessRecipe) entry), stream);
    } else if (entry instanceof ShapedRecipe) {
        return writeShapedRecipe(((ShapedRecipe) entry), stream);
    } else if (entry instanceof FurnaceRecipe) {
        return writeFurnaceRecipe(((FurnaceRecipe) entry), stream);
    } else if (entry instanceof EnchantmentList) {
        return writeEnchantList(((EnchantmentList) entry), stream);
    }
    return -1;
}
 
Example #25
Source File: VarIntTest.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@DisplayName("Writing")
@Test
void testWrite() throws IOException {
	BinaryStream bs = new BinaryStream();
	VarInt.writeUnsignedVarInt(bs, 237356812);
	VarInt.writeVarInt(bs, 0xea3eca71);
	VarInt.writeUnsignedVarLong(bs, 0x1234567812345678L);
	VarInt.writeVarLong(bs, 0xea3eca710becececL);
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(bs)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(bs)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(bs)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(bs))
	);
	ByteArrayOutputStream os = new ByteArrayOutputStream();
	VarInt.writeUnsignedVarInt(os, 237356812);
	VarInt.writeVarInt(os, 0xea3eca71);
	VarInt.writeUnsignedVarLong(os, 0x1234567812345678L);
	VarInt.writeVarLong(os, 0xea3eca710becececL);
	ByteArrayInputStream is = new ByteArrayInputStream(os.toByteArray());
	assertAll(
			() -> assertEquals(237356812, VarInt.readUnsignedVarInt(is)),
			() -> assertEquals(0xea3eca71, VarInt.readVarInt(is)),
			() -> assertEquals(0x1234567812345678L, VarInt.readUnsignedVarLong(is)),
			() -> assertEquals(0xea3eca710becececL, VarInt.readVarLong(is))
	);
}
 
Example #26
Source File: CraftingEventPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void decode() {
    this.windowId = this.getByte();
    this.type = (int) this.getUnsignedVarInt();
    this.id = this.getUUID();

    this.input = this.getArray(Item.class, BinaryStream::getSlot);
    this.output = this.getArray(Item.class, BinaryStream::getSlot);
}
 
Example #27
Source File: Session.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #28
Source File: CraftingManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
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());
}
 
Example #29
Source File: Session.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
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);
    }
}
 
Example #30
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
@Override
public byte[] toBinary() {
    CompoundTag nbt = this.getNBT().copy();

    nbt.putInt("xPos", this.getX());
    nbt.putInt("zPos", this.getZ());

    if (this.isGenerated()) {
        nbt.putByteArray("Blocks", this.getBlockIdArray());
        nbt.putByteArray("Data", this.getBlockDataArray());
        nbt.putByteArray("SkyLight", this.getBlockSkyLightArray());
        nbt.putByteArray("BlockLight", this.getBlockLightArray());
        nbt.putIntArray("BiomeColors", this.getBiomeColorArray());

        int[] heightInts = new int[256];
        byte[] heightBytes = this.getHeightMapArray();
        for (int i = 0; i < heightInts.length; i++) {
            heightInts[i] = heightBytes[i] & 0xFF;
        }
        nbt.putIntArray("HeightMap", heightInts);
    }


    ArrayList<CompoundTag> entities = new ArrayList<>();
    for (Entity entity : this.getEntities().values()) {
        if (!(entity instanceof Player) && !entity.closed) {
            entity.saveNBT();
            entities.add(entity.namedTag);
        }
    }
    ListTag<CompoundTag> entityListTag = new ListTag<>("Entities");
    entityListTag.setAll(entities);
    nbt.putList(entityListTag);

    ArrayList<CompoundTag> tiles = new ArrayList<>();
    for (BlockEntity blockEntity : this.getBlockEntities().values()) {
        blockEntity.saveNBT();
        tiles.add(blockEntity.namedTag);
    }
    ListTag<CompoundTag> tileListTag = new ListTag<>("TileEntities");
    tileListTag.setAll(tiles);
    nbt.putList(tileListTag);

    BinaryStream extraData = new BinaryStream();
    Map<Integer, Integer> extraDataArray = this.getBlockExtraDataArray();
    extraData.putInt(extraDataArray.size());
    for (Integer key : extraDataArray.keySet()) {
        extraData.putInt(key);
        extraData.putShort(extraDataArray.get(key));
    }

    nbt.putByteArray("ExtraData", extraData.getBuffer());

    CompoundTag chunk = new CompoundTag("");
    chunk.putCompound("Level", nbt);

    try {
        return Zlib.deflate(NBTIO.write(chunk, ByteOrder.BIG_ENDIAN), RegionLoader.COMPRESSION_LEVEL);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}