def _createMetadataTag(self, random_seed=None): """ Create a level.dat for a newly created world or a world found with damaged level.dat/.dat_old (xxx repair in WorldEditor?) :param random_seed: :type random_seed: :return: :rtype: """ metadataTag = nbt.TAG_Compound() metadataTag["Data"] = nbt.TAG_Compound() metadataTag["Data"]["SpawnX"] = nbt.TAG_Int(0) metadataTag["Data"]["SpawnY"] = nbt.TAG_Int(2) metadataTag["Data"]["SpawnZ"] = nbt.TAG_Int(0) last_played = long(time.time() * 1000) if random_seed is None: random_seed = long(random.random() * 0xffffffffffffffffL) - 0x8000000000000000L metadataTag["Data"]['version'] = nbt.TAG_Int(VERSION_ANVIL) self.metadata = AnvilWorldMetadata(metadataTag) self.metadata.LastPlayed = long(last_played) self.metadata.RandomSeed = long(random_seed) self.metadata.SizeOnDisk = 0 self.metadata.Time = 1 self.metadata.LevelName = os.path.basename(self.filename)
def TileEntities(self): chestTag = nbt.TAG_Compound() chestTag["id"] = nbt.TAG_String("Chest") chestTag["Items"] = nbt.TAG_List(self.rootTag["Inventory"]) chestTag["x"] = nbt.TAG_Int(0) chestTag["y"] = nbt.TAG_Int(0) chestTag["z"] = nbt.TAG_Int(0) return nbt.TAG_List([chestTag], name="TileEntities")
def _create(self): chunkTag = nbt.TAG_Compound() chunkTag.name = "" levelTag = nbt.TAG_Compound() chunkTag["Level"] = levelTag levelTag["HeightMap"] = nbt.TAG_Int_Array(numpy.zeros((16, 16), 'uint32').newbyteorder()) levelTag["TerrainPopulated"] = nbt.TAG_Byte(1) levelTag["xPos"] = nbt.TAG_Int(self.cx) levelTag["zPos"] = nbt.TAG_Int(self.cz) levelTag["LastUpdate"] = nbt.TAG_Long(0) levelTag["Entities"] = nbt.TAG_List() levelTag["TileEntities"] = nbt.TAG_List() self.rootTag = chunkTag self.dirty = True
def saveToFile(self, filename): super(ZipSchematic, self).saveChanges() schematicDat = nbt.TAG_Compound() schematicDat.name = "Mega Schematic" schematicDat["Width"] = nbt.TAG_Int(self.size[0]) schematicDat["Height"] = nbt.TAG_Int(self.size[1]) schematicDat["Length"] = nbt.TAG_Int(self.size[2]) schematicDat["Materials"] = nbt.TAG_String(self.blocktypes.name) schematicDat.save(self.worldFolder.getFilePath("schematic.dat")) basedir = self.worldFolder.filename assert os.path.isdir(basedir) with closing(zipfile.ZipFile(filename, "w", zipfile.ZIP_STORED)) as z: for root, dirs, files in os.walk(basedir): # NOTE: ignore empty directories for fn in files: absfn = os.path.join(root, fn) zfn = absfn[len(basedir) + len(os.sep):] # XXX: relative path z.write(absfn, zfn)
def created_nbt(): # The root of an NBT file is always a TAG_Compound. level = nbt.TAG_Compound(name="MinecraftLevel") # Subtags of a TAG_Compound are automatically named when you use the [] operator. level["About"] = nbt.TAG_Compound() level["About"]["Author"] = nbt.TAG_String("codewarrior") level["About"]["CreatedOn"] = nbt.TAG_Long(time.time()) level["Environment"] = nbt.TAG_Compound() level["Environment"]["SkyBrightness"] = nbt.TAG_Byte(16) level["Environment"]["SurroundingWaterHeight"] = nbt.TAG_Short(32) level["Environment"]["FogColor"] = nbt.TAG_Int(0xcccccc) entity = nbt.TAG_Compound() entity["id"] = nbt.TAG_String("Creeper") entity["Pos"] = nbt.TAG_List( [nbt.TAG_Float(d) for d in (32.5, 64.0, 33.3)]) level["Entities"] = nbt.TAG_List([entity]) spawn = nbt.TAG_List( (nbt.TAG_Short(100), nbt.TAG_Short(45), nbt.TAG_Short(55))) mapTag = nbt.TAG_Compound() mapTag["Spawn"] = spawn level["Map"] = mapTag mapTag2 = nbt.TAG_Compound([spawn]) mapTag2.name = "Map" # I think it looks more familiar with [] syntax. l, w, h = 128, 128, 128 mapTag["Height"] = nbt.TAG_Short(h) # y dimension mapTag["Length"] = nbt.TAG_Short(l) # z dimension mapTag["Width"] = nbt.TAG_Short(w) # x dimension # Byte arrays are stored as numpy.uint8 arrays. mapTag["Blocks"] = nbt.TAG_Byte_Array() mapTag["Blocks"].value = numpy.zeros( l * w * h, dtype=numpy.uint8) # create lots of air! # The blocks array is indexed (y,z,x) for indev levels, so reshape the blocks mapTag["Blocks"].value.shape = (h, l, w) # Replace the bottom layer of the indev level with wood mapTag["Blocks"].value[0, :, :] = 5 # This is a great way to learn the power of numpy array slicing and indexing. mapTag["Data"] = nbt.TAG_Byte_Array() mapTag["Data"].value = numpy.zeros(l * w * h, dtype=numpy.uint8) # Save a few more tag types for completeness level["ShortArray"] = nbt.TAG_Short_Array( numpy.zeros((16, 16), dtype='uint16')) level["IntArray"] = nbt.TAG_Int_Array(numpy.zeros((16, 16), dtype='uint32')) level["Float"] = nbt.TAG_Float(0.3) return level
def testList(): tag = nbt.TAG_List() tag.append(nbt.TAG_Int(258)) del tag[0]
def Position(self, pos): for a, p in zip('xyz', pos): self.rootTag[a] = nbt.TAG_Int(p)
def Spawn(self, pos): for name, val in zip(("SpawnX", "SpawnY", "SpawnZ"), pos): self.rootTag[name] = nbt.TAG_Int(val)
def setWorldSpawnPosition(self, pos): for name, val in zip(("SpawnX", "SpawnY", "SpawnZ"), pos): self.rootTag[name] = nbt.TAG_Int(val)
def exportStructure(filename, dim, selection, author=None, excludedBlocks=None): """ Parameters ---------- filename : unicode dim : mceditlib.worldeditor.WorldEditorDimension selection : mceditlib.selection.SelectionBox Returns ------- """ excludedBlocks = set(excludedBlocks or []) rootTag = nbt.TAG_Compound() rootTag['author'] = nbt.TAG_String(author or "Anonymous") rootTag['version'] = nbt.TAG_Int(1) rootTag['size'] = nbt.TAG_List([nbt.TAG_Int(s) for s in selection.size]) entities = rootTag['entities'] = nbt.TAG_List(list_type=nbt.ID_COMPOUND) blocks = rootTag['blocks'] = nbt.TAG_List(list_type=nbt.ID_COMPOUND) palette = rootTag['palette'] = nbt.TAG_List(list_type=nbt.ID_COMPOUND) ox, oy, oz = selection.origin paletteIDs = {} for x, y, z in selection.positions: block = dim.getBlock(x, y, z) if block in excludedBlocks: continue paletteIdx = paletteIDs.get(block.nameAndState, None) if paletteIdx is None: paletteTag = nbt.TAG_Compound() paletteTag['Name'] = nbt.TAG_String(block.internalName) if len(block.stateDict): paletteTag['Properties'] = nbt.TAG_Compound() for k, v in block.stateDict.iteritems(): paletteTag['Properties'][k] = nbt.TAG_String(v) paletteIdx = paletteIDs[block.nameAndState] = len(palette) palette.append(paletteTag) blockTag = nbt.TAG_Compound() blockTag['state'] = nbt.TAG_Int(paletteIdx) blockTag['pos'] = nbt.TAG_List( [nbt.TAG_Int(a) for a in x - ox, y - oy, z - oz]) tileEntity = dim.getTileEntity((x, y, z)) if tileEntity: tileEntity = tileEntity.copyWithOffset(-selection.origin) blockTag['nbt'] = tileEntity.rootTag blocks.append(blockTag) for entity in dim.getEntities(selection): entity = entity.copyWithOffset(-selection.origin) entityTag = nbt.TAG_Compound() entityTag['pos'] = nbt.TAG_List( [nbt.TAG_Double(a) for a in entity.Position]) entityTag['blockPos'] = nbt.TAG_List( [nbt.TAG_Int(int(floor(a))) for a in entity.Position]) entityTag['nbt'] = entity.rootTag entities.append(entityTag) rootTag.save(filename)