def create_options_file(self) -> None: stream: object = nbt_be_binary_stream() tag: object = compound_tag("", [ compound_tag("Data", [ byte_tag("hardcore", 0), byte_tag("MapFeatures", 0), byte_tag("raining", 0), byte_tag("Difficulty", 0), byte_tag("thundering", 0), int_tag("GameType", 0), int_tag("generatorVersion", 1), int_tag("rainTime", 0), int_tag("SpawnX", 256), int_tag("SpawnY", 70), int_tag("SpawnZ", 256), int_tag("thunderTime", 0), int_tag("version", 19133), long_tag("LastPlayed", int(time.time() * 1000)), long_tag("RandomSeed", random.randint(0, sys.maxsize)), long_tag("SizeOnDisk", 0), long_tag("Time", 0), string_tag("generatorName", "default"), string_tag("LevelName", "world"), compound_tag("GameRules", []) ]) ]) stream.write_root_tag(tag) with open(os.path.join(self.world_dir, "level.dat"), "wb") as file: file.write(gzip.compress(stream.data)) file.close()
def get_player_option(self, uuid: str, name: str) -> object: with open(os.path.join(self.world_dir, f"players/{uuid}.dat"), "rb") as file: stream: object = nbt_be_binary_stream(gzip.decompress(file.read())) file.close() tag: object = stream.read_root_tag() return tag.get_tag(name).value
def nbt_deserialize(self, data: bytes) -> None: stream = nbt_be_binary_stream(data) root_tag: object = stream.read_root_tag() if not isinstance(root_tag, compound_tag): raise Exception("Invalid NBT data!") if not root_tag.has_tag("Level"): raise Exception("Level tag isnt present!") level_tag: object = root_tag.get_tag("Level") self.x: int = level_tag.get_tag("xPos").value self.z: int = level_tag.get_tag("zPos").value self.terrain_populated: bool = level_tag.get_tag( "TerrainPopulated").value > 0 if level_tag.has_tag("LightPopulated"): self.light_populated: bool = level_tag.get_tag( "LightPopulated").value > 0 else: self.light_populated: bool = False sections_tag: object = level_tag.get_tag("Sections") for section_tag in sections_tag.value: self.sections[section_tag.get_tag("Y").value] = section( section_tag.get_tag("Blocks").value, section_tag.get_tag("Data").value, section_tag.get_tag("BlockLight").value, section_tag.get_tag("SkyLight").value) if level_tag.has_tag("Biomes"): self.biomes: list = level_tag.get_tag("Biomes").value self.entities: list = level_tag.get_tag("Entities").value self.tile_entities: list = level_tag.get_tag("TileEntities").value self.height_map: list = level_tag.get_tag("HeightMap").value
def nbt_serialize(self) -> bytes: stream: object = nbt_be_binary_stream() self.recalculate_height_map() sections: list = list_tag("Sections", [], tag_ids.compound_tag) for i, sect in self.sections.items(): sections.value.append( compound_tag("", [ byte_array_tag("Blocks", sect.block_ids), byte_array_tag("Data", sect.data_entries), byte_array_tag("BlockLight", sect.block_light_entries), byte_array_tag("SkyLight", sect.sky_light_entries) ])) root_tag: object = compound_tag("", [ compound_tag("Level", [ int_tag("xPos", self.x), int_tag("zPos", self.z), long_tag("LastUpdate", 0), byte_tag("V", 1), long_tag("InhabitedTime", 0), byte_tag("TerrainPopulated", 1 if self.terrain_populated else 0), byte_tag("LightPopulated", 1 if self.light_populated else 0), sections, byte_array_tag("Biomes", self.biomes), list_tag("Entities", self.entities, tag_ids.compound_tag), list_tag("TileEntities", self.tile_entities, tag_ids.compound_tag), int_array_tag("HeightMap", self.height_map) ]), int_tag("DataVersion", 1343) ]) stream.write_root_tag(root_tag) return stream.data
def read_data(self, data: bytes) -> None: stream: object = nbt_be_binary_stream(data) tag: object = compound_tag() tag.read(stream) root_tag: object = tag.get_tag("") self.data_version: object = root_tag.get_tag("DataVersion").value level_tag: object = root_tag.get_tag("Level") self.x: int = level_tag.get_tag("xPos").value self.z: int = level_tag.get_tag("zPos").value self.data: list = level_tag
def set_player_option(self, uuid: str, name: str, value: object) -> None: with open(os.path.join(self.world_dir, f"players/{uuid}.dat"), "rb") as file: stream: object = nbt_be_binary_stream(gzip.decompress(file.read())) file.close() tag: object = stream.read_root_tag() if tag.has_tag(name): option_tag: object = tag.get_tag(name) option_tag.value = value tag.set_tag(option_tag) stream.buffer = b"" stream.pos = 0 stream.write_root_tag(tag) with open(os.path.join(self.world_dir, f"players/{uuid}.dat"), "wb") as file: file.write(gzip.compress(stream.data)) file.close()
def create_player_file(self, uuid: str) -> None: stream: object = nbt_be_binary_stream() tag: object = compound_tag("", [ byte_tag("OnGround", 1), byte_tag("Sleeping", 0), short_tag("Air", 300), short_tag("AttackTime", 0), short_tag("DeathTime", 0), short_tag("Fire", 0), short_tag("Health", 20), short_tag("HurtTime", 0), short_tag("SleepTimer", 0), int_tag("Dimension", 0), int_tag("foodLevel", 0), int_tag("foodTickTimer", 0), int_tag("playerGameType", self.get_world_gamemode()), int_tag("XpLevel", 0), int_tag("XpTotal", 0), float_tag("FallDistance", 0), float_tag("foodExhastionLevel", 0), float_tag("foodSaturationLevel", 0), float_tag("XpP", 0), compound_tag("Inventory", []), list_tag("Motion", [double_tag("", 0), double_tag("", 0), double_tag("", 0)], tag_ids.double_tag), list_tag("Pos", [ double_tag("", self.get_spawn_position().x), double_tag("", self.get_spawn_position().y), double_tag("", self.get_spawn_position().z) ], tag_ids.double_tag), list_tag("Rotation", [float_tag("", 0), float_tag("", 0), float_tag("", 0)], tag_ids.float_tag) ]) stream.write_root_tag(tag) with open(os.path.join(self.world_dir, f"players/{uuid}.dat"), "wb") as file: file.write(gzip.compress(stream.data)) file.close()
def set_option(self, name: str, value: object) -> None: with open(os.path.join(self.world_dir, "level.dat"), "rb") as file: stream: object = nbt_be_binary_stream(gzip.decompress(file.read())) file.close() tag: object = stream.read_root_tag() data_tag: bytes = tag.get_tag("Data") if data_tag.has_tag(name): option_tag: object = data_tag.get_tag(name) option_tag.value = value data_tag.set_tag(option_tag) tag.set_tag(data_tag) stream.buffer = b"" stream.pos = 0 stream.write_root_tag(tag) with open(os.path.join(self.world_dir, "level.dat"), "wb") as file: file.write(gzip.compress(stream.data)) file.close()
def get_option(self, name: str) -> object: with open(os.path.join(self.world_dir, "level.dat"), "rb") as file: stream: object = nbt_be_binary_stream(gzip.decompress(file.read())) file.close() tag: object = stream.read_root_tag() return tag.get_tag("Data").get_tag(name).value