-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegistryData.cpp
More file actions
76 lines (55 loc) · 2.37 KB
/
RegistryData.cpp
File metadata and controls
76 lines (55 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include "data/RegistryData.hpp"
#include "network/buffer.hpp"
#include <stdexcept>
#include <string>
#include <utility>
#include <vector>
RegistryData::RegistryData() : _registry_id(""), _entries() {}
RegistryData::RegistryData(const std::string& registryId) : _registry_id(registryId), _entries() {}
RegistryData::~RegistryData() = default;
RegistryData::RegistryData(const RegistryData& other) : _registry_id(other._registry_id), _entries(other._entries) {}
RegistryData& RegistryData::operator=(const RegistryData& other) {
if (this != &other) {
_registry_id = other._registry_id;
_entries = other._entries;
}
return *this;
}
RegistryData::RegistryData(RegistryData&& other) noexcept : _registry_id(std::move(other._registry_id)), _entries(std::move(other._entries)) {}
RegistryData& RegistryData::operator=(RegistryData&& other) noexcept {
if (this != &other) {
_registry_id = std::move(other._registry_id);
_entries = std::move(other._entries);
}
return *this;
}
const std::string& RegistryData::getRegistryId() const { return _registry_id; }
const std::vector<RegistryDataEntry>& RegistryData::getEntries() const { return _entries; }
size_t RegistryData::getEntryCount() const { return _entries.size(); }
void RegistryData::addEntry(const std::string& entryId, bool hasData, std::optional<std::shared_ptr<nbt::Tag>> data) {
_entries.emplace_back(entryId, hasData, data);
}
std::vector<uint8_t> RegistryData::serialize() const {
try {
Buffer buffer;
// Format MC 1.21.5: id + entries length + entries array
buffer.writeString(_registry_id);
buffer.writeVarInt(static_cast<int32_t>(_entries.size()));
for (const auto& entry : _entries) {
// Chaque entrée: key (string) + value optional (anonymousNbt)
buffer.writeString(entry.entry_id);
// Format "value optional": boolean présent + données NBT si présent
if (entry.has_data && entry.data.has_value()) {
buffer.writeBool(true); // Données présentes
buffer.writeBytes("{}"); // Données NBT (vide pour l'instant)
} else {
buffer.writeBool(false); // Pas de données NBT optionnelles
}
}
return buffer.getData();
} catch (const std::exception& e) {
throw std::runtime_error("Failed to serialize RegistryData: " + std::string(e.what()));
}
}
bool RegistryData::isEmpty() const { return _entries.empty(); }
void RegistryData::reserve(size_t capacity) { _entries.reserve(capacity); }