cn.nukkit.utils.Binary Java Examples

The following examples show how to use cn.nukkit.utils.Binary. 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: AddPlayerPacket.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void encode() {
    this.reset();
    this.putUUID(this.uuid);
    this.putString(this.username);
    this.putEntityUniqueId(this.entityUniqueId);
    this.putEntityRuntimeId(this.entityRuntimeId);
    this.putVector3f(this.x, this.y, this.z);
    this.putVector3f(this.speedX, this.speedY, this.speedZ);
    this.putLFloat(this.pitch);
    this.putLFloat(this.yaw); //TODO headrot
    this.putLFloat(this.yaw);
    this.putSlot(this.item);

    this.put(Binary.writeMetadata(this.metadata));
}
 
Example #2
Source File: QueryRegenerateEvent.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public byte[] getShortQuery(byte[] buffer) {
    if (buffer == null) buffer = new byte[Character.MAX_VALUE];
    FastByteArrayOutputStream query = new FastByteArrayOutputStream(buffer);
    try {
        query.write(this.serverName.getBytes(StandardCharsets.UTF_8));
        query.write((byte) 0x00);
        query.write(this.gameType.getBytes(StandardCharsets.UTF_8));
        query.write((byte) 0x00);
        query.write(this.map.getBytes(StandardCharsets.UTF_8));
        query.write((byte) 0x00);
        query.write(String.valueOf(this.numPlayers).getBytes(StandardCharsets.UTF_8));
        query.write((byte) 0x00);
        query.write(String.valueOf(this.maxPlayers).getBytes(StandardCharsets.UTF_8));
        query.write((byte) 0x00);
        query.write(Binary.writeLShort(this.port));
        query.write(this.ip.getBytes(StandardCharsets.UTF_8));
        query.write((byte) 0x00);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return query.toByteArray();
}
 
Example #3
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 #4
Source File: ChunkRequestTask.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
public ChunkRequestTask(Level level, Chunk chunk) {
    this.timeStamp = chunk.getChanges();
    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 #5
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 #6
Source File: QueryRegenerateEvent.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public byte[] getShortQuery() {
    ByteBuffer query = ByteBuffer.allocate(65536);
    query.put(this.serverName.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(this.gameType.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(this.map.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(String.valueOf(this.numPlayers).getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(String.valueOf(this.maxPlayers).getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    query.put(Binary.writeLShort(this.port));
    query.put(this.ip.getBytes(StandardCharsets.UTF_8));
    query.put((byte) 0x00);
    return Arrays.copyOf(query.array(), query.position());
}
 
Example #7
Source File: AddPlayerPacket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void encode() {
    this.reset();
    this.putUUID(this.uuid);
    this.putString(this.username);
    this.putEntityUniqueId(this.entityUniqueId);
    this.putEntityRuntimeId(this.entityRuntimeId);
    this.putString(this.platformChatId);
    this.putVector3f(this.x, this.y, this.z);
    this.putVector3f(this.speedX, this.speedY, this.speedZ);
    this.putLFloat(this.pitch);
    this.putLFloat(this.yaw); //TODO headrot
    this.putLFloat(this.yaw);
    this.putSlot(this.item);
    this.put(Binary.writeMetadata(this.metadata));
    this.putUnsignedVarInt(0); //TODO: Adventure settings
    this.putUnsignedVarInt(0);
    this.putUnsignedVarInt(0);
    this.putUnsignedVarInt(0);
    this.putUnsignedVarInt(0);
    this.putLLong(entityUniqueId);
    this.putUnsignedVarInt(0); //TODO: Entity links
    this.putString(deviceId);
    this.putLInt(buildPlatform);
}
 
Example #8
Source File: AddPlayerPacket.java    From Nukkit with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void encode() {
    this.reset();
    this.putUUID(this.uuid);
    this.putString(this.username);
    this.putEntityUniqueId(this.entityUniqueId);
    this.putEntityRuntimeId(this.entityRuntimeId);
    this.putVector3f(this.x, this.y, this.z);
    this.putVector3f(this.speedX, this.speedY, this.speedZ);
    this.putLFloat(this.pitch);
    this.putLFloat(this.yaw); //TODO headrot
    this.putLFloat(this.yaw);
    this.putSlot(this.item);

    this.put(Binary.writeMetadata(this.metadata));
}
 
Example #9
Source File: LevelDB.java    From Jupiter with GNU General Public License v3.0 6 votes vote down vote up
public Chunk readChunk(int chunkX, int chunkZ) {
    byte[] data;
    if (!this.chunkExists(chunkX, chunkZ) || (data = this.db.get(TerrainKey.create(chunkX, chunkZ).toArray())) == null) {
        return null;
    }

    byte[] flags = this.db.get(FlagsKey.create(chunkX, chunkZ).toArray());
    if (flags == null) {
        flags = new byte[]{0x03};
    }

    return Chunk.fromBinary(
            Binary.appendBytes(
                    Binary.writeLInt(chunkX),
                    Binary.writeLInt(chunkZ),
                    data,
                    flags)
            , this);
}
 
Example #10
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 #11
Source File: QueryHandler.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
public static String getTokenString(String token, String salt) {
    try {
        return String.valueOf(Binary.readInt(Binary.subBytes(MessageDigest.getInstance("SHA-512").digest((salt + ":" + token).getBytes()), 7, 4)));
    } catch (NoSuchAlgorithmException e) {
        return String.valueOf(new Random().nextInt());
    }
}
 
Example #12
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 #13
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 #14
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 #15
Source File: DataPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void decode() {
    super.decode();
    this.seqNumber = this.getLTriad();

    while (!this.feof()) {
        byte[] data = Binary.subBytes(this.buffer, this.offset);
        EncapsulatedPacket packet = EncapsulatedPacket.fromBinary(data, false);
        this.offset += packet.getOffset();
        if (packet.buffer.length == 0) {
            break;
        }
        this.packets.add(packet);
    }
}
 
Example #16
Source File: AddItemEntityPacket.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void encode() {
    this.reset();
    this.putEntityUniqueId(this.entityUniqueId);
    this.putEntityRuntimeId(this.entityRuntimeId);
    this.putSlot(this.item);
    this.putVector3f(this.x, this.y, this.z);
    this.putVector3f(this.speedX, this.speedY, this.speedZ);
    this.put(Binary.writeMetadata(metadata));
    this.putBoolean(this.isFromFishing);
}
 
Example #17
Source File: ServerHandler.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void unblockAddress(String address) {
    byte[] buffer = Binary.appendBytes(
            RakNet.PACKET_UNBLOCK_ADDRESS,
            new byte[]{(byte) (address.length() & 0xff)},
            address.getBytes(StandardCharsets.UTF_8)
    );
    this.server.pushMainToThreadPacket(buffer);
}
 
Example #18
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 #19
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 #20
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 #21
Source File: ServerHandler.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void sendOption(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.pushMainToThreadPacket(buffer);
}
 
Example #22
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 #23
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 #24
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 #25
Source File: SynLibInterface.java    From SynapseAPI with GNU General Public License v3.0 5 votes vote down vote up
@Override
public Integer putPacket(Player player, DataPacket packet, boolean needACK, boolean immediate) {
    RedirectPacket pk = new RedirectPacket();
    pk.uuid = player.getUniqueId();
    pk.direct = immediate;
    pk.mcpeBuffer = packet instanceof BatchPacket ? Binary.appendBytes((byte) 0xfe, ((BatchPacket) packet).payload) : packet.getBuffer();

    this.synapseInterface.putPacket(pk);
    return 0;  //这个返回值在nk中并没有被用到
}
 
Example #26
Source File: SessionManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
protected void streamOpen(Session session) {
    String identifier = FastAppender.get(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 #27
Source File: SessionManager.java    From Jupiter 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 #28
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 #29
Source File: SessionManager.java    From Jupiter with GNU General Public License v3.0 5 votes vote down vote up
public void streamRAW(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.pushThreadToMainPacket(buffer);
}
 
Example #30
Source File: RakNetInterface.java    From Nukkit with GNU General Public License v3.0 5 votes vote down vote up
@Override
    public void handleEncapsulated(String identifier, EncapsulatedPacket packet, int flags) {
        if (this.players.containsKey(identifier)) {
            DataPacket pk = null;
            try {
                if (packet.buffer.length > 0) {
                    if (packet.buffer[0] == PING_DataPacket.ID) {
                        PING_DataPacket pingPacket = new PING_DataPacket();
                        pingPacket.buffer = packet.buffer;
                        pingPacket.decode();

                        this.networkLatency.put(identifier, (int) pingPacket.pingID);
                        return;
                    }

                    pk = this.getPacket(packet.buffer);
                    if (pk != null) {
                        pk.decode();
                        this.players.get(identifier).handleDataPacket(pk);
                    }
                }
            } catch (Exception e) {
                this.server.getLogger().logException(e);
                if (Nukkit.DEBUG > 1 && pk != null) {
                    MainLogger logger = this.server.getLogger();
//                    if (logger != null) {
                    logger.debug("Packet " + pk.getClass().getName() + " 0x" + Binary.bytesToHexString(packet.buffer));
                    //logger.logException(e);
//                    }
                }

                if (this.players.containsKey(identifier)) {
                    this.handler.blockAddress(this.players.get(identifier).getAddress(), 5);
                }
            }
        }
    }