def convert(tag): out = None if (tag.type is parse.TAG_Byte): out = nbt.TAG_Byte(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_Byte_Array): out = nbt.TAG_Byte_Array(value=fromstring(tag.data), name=tag.name) elif (tag.type is parse.TAG_Double): out = nbt.TAG_Double(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_Float): out = nbt.TAG_Float(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_Int): out = nbt.TAG_Int(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_Int_Array): out = nbt.TAG_Int_Array(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_Long): out = nbt.TAG_Long(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_Short): out = nbt.TAG_Short(value=tag.data, name=tag.name) elif (tag.type is parse.TAG_String): out = nbt.TAG_String(value=tag.data, name=tag.name) # Recursives elif (tag.type is parse.TAG_Compound): out = nbt.TAG_Compound(name=tag.name) for item in tag.data: temp = convert(item) if (temp is not None): out[temp.name] = temp elif (tag.type is parse.TAG_List): out = nbt.TAG_List(name=tag.name) for item in tag.data[1]: temp = convert(dummyNBTyaml(tag.data[0], item)) if (temp is not None): out.append(temp) return out
def parse_nbt(node, params={}): """Recursivly build nbt datastructure from xml.""" if 'name' in node.attrib: name = node.attrib['name'] else: name = '' if node.text and node.text != '': text = replace_params(node.text, params) else: text = '' if node.tag == 'Compound': values = [] # list of other tags for child in node: values.append(parse_nbt(child, params)) return nbt.TAG_Compound(values, name) if node.tag == 'List': values = [] # list of other tags for child in node: values.append(parse_nbt(child, params)) return nbt.TAG_List(values, name) elif node.tag == 'String': return nbt.TAG_String(text, name) elif node.tag == 'Int': return nbt.TAG_Int(text, name) elif node.tag == 'Byte': return nbt.TAG_Byte(text, name) elif node.tag == 'Short': return nbt.TAG_Short(text, name) elif node.tag == 'Long': return nbt.TAG_Long(text, name) elif node.tag == 'Float': return nbt.TAG_Float(text, name) elif node.tag == 'Double': return nbt.TAG_Double(text, name) else: raise 'Unsupported'
myfile.write("Saved %d chunks.\n" % numchunks) myfile.close() with open(tf, "a") as myfile: myfile.write("Creating world %s\n" % worlddir) myfile.close() world = mclevel.MCInfdevOldLevel(worlddir, create=True) from pymclevel.nbt import TAG_Int, TAG_String, TAG_Byte_Array # Set Data tags # tags = [ TAG_Int(0, "MapFeatures"), TAG_String("flat", "generatorName"), TAG_String("0", "generatorOptions"), nbt.TAG_Long(name='DayTime', value=4000) ] for tag in tags: world.root_tag['Data'].add(tag) # Set Game rules # rule = nbt.TAG_Compound(name='GameRules', value=[ TAG_String("false", "doDaylightCycle"), TAG_String("false", "doMobSpawning") ]) world.root_tag['Data'].add(rule) # The code tracks the peak [x,y,z] # this is a placeholder peak = [10, 255, 10]
def testCreate(): "Create an indev level." # 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]) # You can also create and name a tag before adding it to the compound. spawn = nbt.TAG_List((nbt.TAG_Short(100), nbt.TAG_Short(45), nbt.TAG_Short(55))) spawn.name = "Spawn" mapTag = nbt.TAG_Compound() mapTag.add(spawn) mapTag.name = "Map" level.add(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