LeviLamina
Loading...
Searching...
No Matches
RuntimePacket.h
1#pragma once
2
3#include <memory>
4#include <string>
5
6#include "ll/api/network/packet/Packet.h"
7
8#include "mc/network/Packet.h"
9
10namespace ll::network {
11
12class RuntimePacket final : public ::Packet {
13 friend class ll::network::Packet;
14
15 std::unique_ptr<ll::network::Packet> ownedPacket;
17 PacketRuntimeId runtimeId{};
18
19 RuntimePacket(ll::network::Packet const& packet) : packet(packet) {
20 runtimeId = packet.getRuntimeId();
21 mReliability = packet.mReliability;
22 mPriority = packet.mPriority;
23 mCompressible = packet.mCompressible;
24 mSenderSubId = packet.mSenderSubId;
25 }
26
27public:
28 RuntimePacket() = default;
29
30 RuntimePacket(std::unique_ptr<ll::network::Packet> packet)
31 : ownedPacket(std::move(packet)),
32 packet(ownedPacket.get()) {
33 if (packet) {
34 runtimeId = packet->getRuntimeId();
35 mReliability = packet->mReliability;
36 mPriority = packet->mPriority;
37 mCompressible = packet->mCompressible;
38 mSenderSubId = packet->mSenderSubId;
39 }
40 }
41
42 [[nodiscard]] constexpr PacketRuntimeId getRuntimeId() const { return runtimeId; }
43
44 [[nodiscard]] bool isOwned() const { return ownedPacket != nullptr; }
45 [[nodiscard]] constexpr bool hasPacket() const { return packet.has_value(); }
46
47 [[nodiscard]] constexpr optional_ref<ll::network::Packet const> getPacket() const { return packet; }
48
49 [[nodiscard]] constexpr optional_ref<ll::network::Packet> getOwnedPacket() const { return ownedPacket.get(); }
50
51 [[nodiscard]] constexpr void movePacket(std::unique_ptr<ll::network::Packet> newPacket) {
52 ownedPacket = std::move(newPacket);
53 packet = ownedPacket.get();
54 }
55
56 [[nodiscard]] constexpr std::unique_ptr<ll::network::Packet> releasePacket() {
57 packet = nullptr;
58 return std::move(ownedPacket);
59 }
60
61private:
62 [[nodiscard]] MinecraftPacketIds getId() const override;
63 [[nodiscard]] std::string getName() const override;
64 void write(BinaryStream&) const override;
66};
67
68}; // namespace ll::network
Definition Result.h:9
Definition BinaryStream.h:11
Definition ReadOnlyBinaryStream.h:8
Definition Packet.h:28
Definition optional_ref.h:10