Lodestone.Minecraft.Java  1.2.6
Loading...
Searching...
No Matches
lodestone::minecraft::java::indev::McLevelNbtLevelIO Class Reference

#include <McLevelLevelIo.h>

Inheritance diagram for lodestone::minecraft::java::indev::McLevelNbtLevelIO:
Collaboration diagram for lodestone::minecraft::java::indev::McLevelNbtLevelIO:

Public Member Functions

std::unique_ptr< level::Level > read (const common::conversion::io::options::OptionPresets::CommonNbtReadOptions &options) const override
 Creates a Level from a mclevel Map compound tag.
void write (level::Level *l, const common::conversion::io::options::OptionPresets::NbtOutputWriteOptions< const conversion::io::options::versioned::VersionedOptions > &options) const override
 Writes a level to a given NBT compound tag.
Public Member Functions inherited from lodestone::conversion::io::IObjectIO
constexpr IObjectIO ()=default
 ~IObjectIO () override=default
Public Member Functions inherited from lodestone::conversion::io::types::ReadableAdapter
virtual ~ReadableAdapter ()=default
Public Member Functions inherited from lodestone::conversion::io::types::WritableAdapter
virtual ~WritableAdapter ()=default
Public Member Functions inherited from lodestone::minecraft::common::conversion::io::NbtIO< I, std::unique_ptr< level::Level >, level::Level *, RO, WO >
nbt::tag_compound tagFromWritten (W obj, WO &options) const
void writeToNbtStreamWriter (W obj, const std::string &key, nbt::io::stream_writer &writer, WO &options) const
Public Member Functions inherited from lodestone::conversion::io::AbstractObjectIO< I, R, W, RO, const options::OptionPresets::NbtOutputWriteOptions< WO >, void >
 AbstractObjectIO ()=default
pro::proxy< ReallyAny > baseRead (void *options) const override
void baseWrite (void *obj, void *options) const override
Public Member Functions inherited from lodestone::conversion::io::types::Readable< R, RO >
virtual ~Readable ()=default
virtual R read (RO &options) const =0
Public Member Functions inherited from lodestone::conversion::io::types::Writable< W, WO, void >
virtual ~Writable ()=default
virtual WR write (W obj, WO &options) const =0
Public Member Functions inherited from lodestone::conversion::registry::RegistryIdentifierRelations< conversion::registry::RegistryIdentifierRelation<&conversion::identifiers::LEVEL_IO, const McLevelLevelIo, &identifiers::MCLEVEL_LEVEL_IO, conversion::registry::LevelIORegistry > >
T * getAsByRelation (const common::registry::Identifier *identifier) const
io::IObjectIO * getByRelation () const

Additional Inherited Members

Static Public Member Functions inherited from lodestone::common::registry::Identifiable< I >
static constexpr const Identifier * getIdentifier ()

Member Function Documentation

◆ read()

std::unique_ptr< level::Level > lodestone::minecraft::java::indev::McLevelNbtLevelIO::read ( const common::conversion::io::options::OptionPresets::CommonNbtReadOptions & options) const
override

Creates a Level from a mclevel Map compound tag.

53 {
54
55 auto lvl = std::make_unique<level::Level>(level::Level());
56 auto root = options.input;
57
58 // Environment tag
59 auto environment = root["Environment"].get_as<nbt::tag_compound>();
60 const int16_t surroundingGroundHeight = environment[
61 "SurroundingGroundHeight"].get_as<nbt::tag_short>().get();
62 const int16_t timeOfDay = environment["TimeOfDay"].get_as<
63 nbt::tag_short>().get();
64 const int16_t cloudHeight = environment["CloudHeight"].get_as<
65 nbt::tag_short>().get();
66 const int32_t cloudColor = environment["CloudColor"].get_as<
67 nbt::tag_int>().get();
68 const int8_t skyBrightness = environment["SkyBrightness"].get_as<
69 nbt::tag_byte>().get();
70 const int32_t skyColor = environment["SkyColor"].get_as<nbt::tag_int>().
71 get();
72 const int32_t fogColor = environment["FogColor"].get_as<nbt::tag_int>().
73 get();
74 const int16_t surroundingWaterHeight = environment[
75 "SurroundingWaterHeight"].get_as<nbt::tag_short>().get();
76 const int8_t surroundingGroundType = environment["SurroundingGroundType"].
77 get_as<nbt::tag_byte>().get();
78 const int8_t surroundingWaterType = environment["SurroundingWaterType"].
79 get_as<nbt::tag_byte>().get();
80
81 // Map tag
82 auto map = root["Map"].get_as<nbt::tag_compound>();
83 const int16_t length = map["Length"].get_as<nbt::tag_short>().get();
84 const int16_t width = map["Width"].get_as<nbt::tag_short>().get();
85 const int16_t height = map["Height"].get_as<nbt::tag_short>().get();
86
87 const std::vector<int8_t> blocks = map["Blocks"].get_as<
88 nbt::tag_byte_array>().get();
89 const std::vector<int8_t> data = map["Data"].get_as<
90 nbt::tag_byte_array>().get();
91
92 const std::unique_ptr<conversion::block::version::BlockIO> io =
93 LodestoneJava::getInstance()->m_blockIo.getIo(options.version);
94
95 for (int y = 0; y < height; y++) {
96 for (int z = 0; z < length; z++) {
97 for (int x = 0; x < width; x++) {
98 const size_t idx = INDEX_YZX(x, y, z, width, length);
99
100 const uint8_t bb = blocks[idx];
101
102#ifdef USE_RISKY_OPTIMIZATIONS
103 if (bb == 0) // since air is id 0
104 continue; // this skips us having to convert the block
105#endif
106
107 const int8_t metadata = data[idx] & 0x0F;
108 const int8_t lighting = data[idx] & 0xF0;
109
110 level::block::instance::BlockInstance b =
111 io->convertBlockToInternal(
112 conversion::block::data::
113 NumericBlockData(
114 bb,
115 0));
116 // leave as zero until block states are implemented
117
119 lvl->setBlockCreate(std::move(b), x, y, z, height);
120 }
121
122 if (level::chunk::Chunk *ch = lvl->getChunk(level::coords::ChunkCoordinates::fromBlockCoordinates(x, z))) {
123 if (level::chunk::section::Section *s = ch->getSectionFromBlockY(y)) {
125
126 // TODO its late so don't want to bother with this yet
127 // uint8_t brightness = (skyBrightness * 15);
128 // if (brightness != 0) {
129 // brightness /= 100;
130 // }
131 //
132 // size_t h = ch->getHeightAt(CHUNK_LOCAL_IDX(x, width), CHUNK_LOCAL_IDX(z, length));
133 //
134 // for (size_t i = height; i > h; i--) {
135 // ch->setSkyLight(CHUNK_LOCAL_IDX(x, width), i, CHUNK_LOCAL_IDX(z, length), brightness);
136 // }
137 }
138 }
139 }
140 }
141 }
142
143 // TODO: TileEntities tag
144
145 // About tag
146 auto about = root["About"].get_as<nbt::tag_compound>();
147 const std::string author = about["Author"].get_as<nbt::tag_string>().
148 get();
149 const int64_t createdOn = about["CreatedOn"].get_as<nbt::tag_long>().
150 get();
151 lvl->setCreationTime(createdOn);
152 const std::string name = about["Name"].get_as<nbt::tag_string>().get();
153
154 // TODO: Entities tag
155
156 return lvl;
157 }
#define INDEX_YZX(x, y, z, w, d)
std::unique_ptr< BlockIO > getIo(uint32_t version)
static constexpr ChunkCoordinates fromBlockCoordinates(const signed_size_t blockX, const signed_size_t blockZ)
static constexpr int blockToLocalChunkZ(const signed_size_t blockZ)
static constexpr int blockToLocalChunkX(const signed_size_t blockX)
static constexpr int blockToLocalSectionY(const signed_size_t blockX)
lodestone::conversion::block::version::VersionedBlockIO m_blockIo
Definition LodestoneJava.h:39
static LodestoneJava * getInstance()
Definition LodestoneJava.cpp:89

◆ write()

void lodestone::minecraft::java::indev::McLevelNbtLevelIO::write ( level::Level * l,
const common::conversion::io::options::OptionPresets::NbtOutputWriteOptions< const conversion::io::options::versioned::VersionedOptions > & options ) const
override

Writes a level to a given NBT compound tag.

163 {
164 auto &root = options.output;
165
166 //region Environment
167 auto environment = nbt::tag_compound();
168
169 const auto cloudColor = l->getPropertyOr("CloudColor", 0xFFFFFF);
170 const auto cloudHeight = l->getPropertyOr("CloudHeight", static_cast<int16_t>(66));
171 const auto fogColor = l->getPropertyOr("FogColor", 0xFFFFFF);
172 const auto skyBrightness = l->getPropertyOr("SkyBrightness", static_cast<int8_t>(0xF));
173 const auto skyColor = l->getPropertyOr("SkyColor", 0x99CCFF);
174 const auto surroundingGroundHeight = l->getPropertyOr("SurroundingGroundHeight", static_cast<int16_t>(2));
175 const auto surroundingGroundType = l->getPropertyOr("SurroundingGroundType", static_cast<int8_t>(2));
176 const auto surroundingWaterHeight = l->getPropertyOr("SurroundingWaterHeight", static_cast<int16_t>(32));
177 const auto surroundingWaterType = l->getPropertyOr("SurroundingWaterType", static_cast<int8_t>(8));
178 const auto timeOfDay = l->getPropertyOr("TimeOfDay", static_cast<int16_t>(16));
179 environment["CloudColor"] = cloudColor->getValue();
180 environment["CloudHeight"] = cloudHeight->getValue();
181 environment["FogColor"] = fogColor->getValue();
182 environment["SkyBrightness"] = skyBrightness->getValue();
183 environment["SkyColor"] = skyColor->getValue();
184 environment["SurroundingGroundHeight"] = surroundingGroundHeight->getValue();
185 environment["SurroundingGroundType"] = surroundingGroundType->getValue();
186 environment["SurroundingWaterHeight"] = surroundingWaterHeight->getValue();
187 environment["SurroundingWaterType"] = surroundingWaterType->getValue();
188 environment["TimeOfDay"] = timeOfDay->getValue();
189
190 //region Map
191 auto map = nbt::tag_compound();
192
193 const auto spawnPos = l->getSpawnPos();
194 const auto spawn = nbt::tag_list({
195 static_cast<int16_t>(spawnPos.x),
196 static_cast<int16_t>(spawnPos.y),
197 static_cast<int16_t>(spawnPos.z)
198 });
199 map.emplace<nbt::tag_list>("Spawn", spawn);
200
201 const auto levelBounds = l->getBlockBounds();
202 const auto height = levelBounds.getHeight();
203 const auto width = levelBounds.getWidth();
204 const auto length = levelBounds.getLength();
205 map["Height"] = nbt::tag_short(height);
206 map["Width"] = nbt::tag_short(width);
207 map["Length"] = nbt::tag_short(length);
208
209 auto blocks = std::vector<int8_t>(height * width * length);
210 auto metadata = std::vector<int8_t>(height * width * length);
211
212 const std::unique_ptr<conversion::block::version::BlockIO> bio =
213 LodestoneJava::getInstance()->m_blockIo.getIo(options.version);
214
215 for (int y = 0; y < height; y++) {
216 for (int z = 0; z < length; z++) {
217 for (int x = 0; x < width; x++) {
218 const size_t idx = INDEX_YZX(x, y, z, width, length);
219
220 const auto bb = l->getBlock(x, y, z);
221
222 if (bb.getBlock() != level::block::BlockRegistry::s_defaultBlock) {
223 uint8_t blockId = 0;
224 uint8_t data = 0;
225
226 if (const conversion::block::data::NumericBlockData *bl =
227 bio->convertBlockFromInternal(&bb)->as<conversion::block::data::NumericBlockData>()) {
228 blockId = bl->getId();
229 data = bl->getData();
230 }
231
232 blocks[idx] = blockId;
233 metadata[idx] = data;
234 }
235 }
236 }
237 }
238
239 map["Blocks"] = nbt::tag_byte_array(std::move(blocks));
240 map["Data"] = nbt::tag_byte_array(std::move(metadata));
241
242 //region About
243 auto about = nbt::tag_compound();
244
245 const auto author = l->getPropertyOr("Author", "Player");
246 const auto createdOn = l->getPropertyOr("CreatedOn", static_cast<int64_t>(0L));
247 const auto name = l->getPropertyOr("Name", "A Nice World");
248 about["Author"] = author->getValue();
249 about["CreatedOn"] = createdOn->getValue();
250 about["Name"] = name->getValue();
251
252 // TODO: Write entity data
253 auto entities = nbt::tag_list(nbt::tag_type::Compound);
254
255 // TODO: Write tile entity data
256 auto tileEntities = nbt::tag_list(nbt::tag_type::Compound);
257
258 root.emplace<nbt::tag_compound>("Environment", environment);
259 root.emplace<nbt::tag_compound>("Map", map);
260 root.emplace<nbt::tag_compound>("About", about);
261 root.emplace<nbt::tag_list>("Entities", entities);
262 root.emplace<nbt::tag_list>("TileEntities", tileEntities);
263 }

The documentation for this class was generated from the following files: