net.jselby.pc.network.packets.mcplay.PacketOutMapChunkBulk.java Source code

Java tutorial

Introduction

Here is the source code for net.jselby.pc.network.packets.mcplay.PacketOutMapChunkBulk.java

Source

/*
 * PoweredCube3
 * Copyright (C) 2014 James
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package net.jselby.pc.network.packets.mcplay;

import net.jselby.pc.network.OutgoingPacket;
import net.jselby.pc.network.PacketDefinitions;
import net.jselby.pc.network.StandardOutput;
import net.jselby.pc.network.clients.Client;
import net.jselby.pc.network.data.NetworkedChunk;
import net.jselby.pc.world.Chunk;
import org.apache.commons.lang.ArrayUtils;

import java.io.IOException;
import java.util.ArrayList;

/**
 * Sends many world chunks at once, for better compression results.
 * Similar to the PacketOutChunkData packet.
 *
 * @author j_selby
 */
public class PacketOutMapChunkBulk extends OutgoingPacket {
    public ArrayList<Chunk> chunks = new ArrayList<>();

    @Override
    public void write(Client cl, StandardOutput out) throws IOException {
        byte[] dataArray = new byte[0];
        boolean skylight = true;

        ArrayList<NetworkedChunk> networkedChunks = new ArrayList<>();

        for (Chunk chunk : chunks) {
            NetworkedChunk compiledChunk = new NetworkedChunk(chunk);
            networkedChunks.add(compiledChunk);
            dataArray = ArrayUtils.addAll(dataArray, compiledChunk.getData());
        }

        out.writeShort(chunks.size());
        out.writeInt(dataArray.length);
        out.writeBoolean(skylight);
        out.writeBytes(dataArray);

        for (NetworkedChunk chunk : networkedChunks) {
            out.writeInt(chunk.getX());
            out.writeInt(chunk.getZ());
            int bitmap = chunk.getPrimaryMap();
            out.writeByte((byte) (bitmap & 0xff));
            out.writeByte((byte) ((bitmap >> 8) & 0xff));

            chunk.clear();
        }

        // Clear this packet, remove unneeded data
        chunks.clear();
    }

    @Override
    public int getId() {
        return 0x26;
    }

    @Override
    public PacketDefinitions.State getState() {
        return PacketDefinitions.State.PLAY;
    }
}