Java Code Examples for cn.nukkit.utils.Binary#appendBytes()

The following examples show how to use cn.nukkit.utils.Binary#appendBytes() . 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: ChunkRequestTask.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public ChunkRequestTask(Level level, Chunk chunk) {
    this.levelId = level.getId();
    this.chunk = chunk.toFastBinary();
    this.chunkX = chunk.getX();
    this.chunkZ = chunk.getZ();

    byte[] buffer = new byte[0];

    for (BlockEntity blockEntity : chunk.getBlockEntities().values()) {
        if (blockEntity instanceof BlockEntitySpawnable) {
            try {
                buffer = Binary.appendBytes(buffer, NBTIO.write(((BlockEntitySpawnable) blockEntity).getSpawnCompound(), ByteOrder.BIG_ENDIAN, true));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }

        }
    }

    this.blockEntities = buffer;
}
 
Example 2
Source File: SessionManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void streamInvalid(String identifier) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_INVALID_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 3
Source File: SessionManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void streamClose(String identifier, String reason) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_CLOSE_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (reason.length() & 0xff)},
            reason.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 4
Source File: SessionManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void streamOpen(Session session) {
    String identifier = session.getAddress() + ":" + session.getPort();
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_OPEN_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (session.getAddress().length() & 0xff)},
            session.getAddress().getBytes(StandardCharsets.UTF_8),
            Binary.writeShort(session.getPort()),
            Binary.writeLong(session.getID())
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 5
Source File: ServerHandler.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void closeSession(String identifier, String reason) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_CLOSE_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (reason.length() & 0xff)},
            reason.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 6
Source File: ServerHandler.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void blockAddress(String address, int timeout) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_BLOCK_ADDRESS,
            new byte[]{(byte) (address.length() & 0xff)},
            address.getBytes(StandardCharsets.UTF_8),
            Binary.writeInt(timeout)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 7
Source File: ServerHandler.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void blockAddress(String address, int timeout) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_BLOCK_ADDRESS,
            new byte[]{(byte) (address.length() & 0xff)},
            address.getBytes(StandardCharsets.UTF_8),
            Binary.writeInt(timeout)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 8
Source File: SessionManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void streamOpen(Session session) {
    String identifier = session.getAddress() + ":" + session.getPort();
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_OPEN_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (session.getAddress().length() & 0xff)},
            session.getAddress().getBytes(StandardCharsets.UTF_8),
            Binary.writeShort(session.getPort()),
            Binary.writeLong(session.getID())
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 9
Source File: ServerHandler.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void sendRaw(String address, int port, byte[] payload) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_RAW,
            new byte[]{(byte) (address.length() & 0xff)},
            address.getBytes(StandardCharsets.UTF_8),
            Binary.writeShort(port),
            payload
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 10
Source File: ServerHandler.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void sendEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_ENCAPSULATED,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (flags & 0xff)},
            packet.toBinary(true)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 11
Source File: SessionManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected void streamOption(String name, String value) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_SET_OPTION,
            new byte[]{(byte) (name.length() & 0xff)},
            name.getBytes(StandardCharsets.UTF_8),
            value.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 12
Source File: ServerHandler.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void closeSession(String identifier, String reason) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_CLOSE_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (reason.length() & 0xff)},
            reason.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 13
Source File: SessionManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void streamOption(String name, String value) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_SET_OPTION,
            new byte[]{(byte) (name.length() & 0xff)},
            name.getBytes(StandardCharsets.UTF_8),
            value.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 14
Source File: SessionManager.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
protected void streamACK(String identifier, int identifierACK) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_ACK_NOTIFICATION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            Binary.writeInt(identifierACK)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 15
Source File: SessionManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected void streamClose(String identifier, String reason) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_CLOSE_SESSION,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (reason.length() & 0xff)},
            reason.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 16
Source File: ServerHandler.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public void sendEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_ENCAPSULATED,
            new byte[]{(byte) (identifier.length() & 0xff)},
            identifier.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (flags & 0xff)},
            packet.toBinary(true)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example 17
Source File: SessionManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void streamEncapsulated(Session session, EncapsulatedPacket packet, int flags) {
    String id = FastAppender.get(session.getAddress(), ":", session.getPort());
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_ENCAPSULATED,
            new byte[]{(byte) (id.length() & 0xff)},
            id.getBytes(StandardCharsets.UTF_8),
            new byte[]{(byte) (flags & 0xff)},
            packet.toBinary(true)
    );
    this.server.pushThreadToMainPacket(buffer);
}
 
Example 18
Source File: Packet.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
protected void put(byte[] b) {
    this.buffer = Binary.appendBytes(this.buffer, b);
}
 
Example 19
Source File: Packet.java    From Jupiter with GNU General Public License v3.0 4 votes vote down vote up
protected void put(byte[] b) {
    this.buffer = Binary.appendBytes(this.buffer, b);
}
 
Example 20
Source File: Chunk.java    From Nukkit with GNU General Public License v3.0 4 votes vote down vote up
public byte[] toBinary(boolean saveExtra) {
    try {
        LevelProvider provider = this.getProvider();

        if (saveExtra && provider instanceof LevelDB) {

            List<CompoundTag> entities = new ArrayList<>();

            for (Entity entity : this.getEntities().values()) {
                if (!(entity instanceof Player) && !entity.closed) {
                    entity.saveNBT();
                    entities.add(entity.namedTag);
                }
            }

            EntitiesKey entitiesKey = EntitiesKey.create(this.getX(), this.getZ());
            if (!entities.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(entitiesKey.toArray(), NBTIO.write(entities));
            } else {
                ((LevelDB) provider).getDatabase().delete(entitiesKey.toArray());
            }

            List<CompoundTag> tiles = new ArrayList<>();

            for (BlockEntity blockEntity : this.getBlockEntities().values()) {
                if (!blockEntity.closed) {
                    blockEntity.saveNBT();
                    entities.add(blockEntity.namedTag);
                }
            }

            TilesKey tilesKey = TilesKey.create(this.getX(), this.getZ());
            if (!tiles.isEmpty()) {
                ((LevelDB) provider).getDatabase().put(tilesKey.toArray(), NBTIO.write(tiles));
            } else {
                ((LevelDB) provider).getDatabase().delete(tilesKey.toArray());
            }

            ExtraDataKey extraDataKey = ExtraDataKey.create(this.getX(), this.getZ());
            if (!this.getBlockExtraDataArray().isEmpty()) {
                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));
                }
                ((LevelDB) provider).getDatabase().put(extraDataKey.toArray(), extraData.getBuffer());
            } else {
                ((LevelDB) provider).getDatabase().delete(extraDataKey.toArray());
            }

        }

        byte[] heightMap = this.getHeightMapArray();

        byte[] biomeColors = new byte[this.getBiomeColorArray().length * 4];
        for (int i = 0; i < this.getBiomeColorArray().length; i++) {
            byte[] bytes = Binary.writeInt(this.getBiomeColorArray()[i]);
            biomeColors[i * 4] = bytes[0];
            biomeColors[i * 4 + 1] = bytes[1];
            biomeColors[i * 4 + 2] = bytes[2];
            biomeColors[i * 4 + 3] = bytes[3];
        }

        return Binary.appendBytes(
                Binary.writeLInt(this.getX()),
                Binary.writeLInt(this.getZ()),
                this.getBlockIdArray(),
                this.getBlockDataArray(),
                this.getBlockSkyLightArray(),
                this.getBlockLightArray(),
                heightMap,
                biomeColors,
                new byte[]{(byte) (((this.isLightPopulated ? 0x04 : 0) | (this.isPopulated() ? 0x02 : 0) | (this.isGenerated() ? 0x01 : 0)) & 0xff)}
        );
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}