def save(self): ''' Saves all waypoint information to the 'mcedit_waypoints.dat' file in the world directory ''' if DEBUG: current_frame = inspect.currentframe() outerframe = inspect.getouterframes(current_frame, 2)[1] print "Called by '" + str(outerframe[3]) + "()' in '" + str( outerframe[1].split("\\")[-1]) + "' at line " + str( outerframe[2]) del self.nbt_waypoints["Waypoints"] self.nbt_waypoints["Waypoints"] = nbt.TAG_List() for waypoint in self.waypoints.keys(): if waypoint.split()[0] == "Empty": continue way = nbt.TAG_Compound() way["Name"] = nbt.TAG_String(waypoint.split()[0]) way["Dimension"] = nbt.TAG_Int(self.waypoints[waypoint][5]) coords = nbt.TAG_List() coords.append(nbt.TAG_Float(self.waypoints[waypoint][0])) coords.append(nbt.TAG_Float(self.waypoints[waypoint][1])) coords.append(nbt.TAG_Float(self.waypoints[waypoint][2])) rot = nbt.TAG_List() rot.append(nbt.TAG_Float(self.waypoints[waypoint][3])) rot.append(nbt.TAG_Float(self.waypoints[waypoint][4])) way["Coordinates"] = coords way["Rotation"] = rot self.nbt_waypoints["Waypoints"].append(way) self.nbt_waypoints.save( os.path.join(self.worldDirectory, u"mcedit_waypoints.dat"))
def newPlayer(self): playerTag = nbt.TAG_Compound() playerTag['Air'] = nbt.TAG_Short(300) playerTag['AttackTime'] = nbt.TAG_Short(0) playerTag['DeathTime'] = nbt.TAG_Short(0) playerTag['Fire'] = nbt.TAG_Short(-20) playerTag['Health'] = nbt.TAG_Short(20) playerTag['HurtTime'] = nbt.TAG_Short(0) playerTag['Score'] = nbt.TAG_Int(0) playerTag['FallDistance'] = nbt.TAG_Float(0) playerTag['OnGround'] = nbt.TAG_Byte(0) playerTag["Inventory"] = nbt.TAG_List() playerTag['Motion'] = nbt.TAG_List([nbt.TAG_Double(0) for i in range(3)]) spawn = self.level.playerSpawnPosition() spawnX = spawn[0] spawnZ = spawn[2] blocks = [self.level.blockAt(spawnX, i, spawnZ) for i in range(self.level.Height)] i = self.level.Height done = False for index, b in enumerate(reversed(blocks)): if b != 0 and not done: i = index done = True spawnY = self.level.Height - i playerTag['Pos'] = nbt.TAG_List([nbt.TAG_Double([spawnX, spawnY, spawnZ][i]) for i in range(3)]) playerTag['Rotation'] = nbt.TAG_List([nbt.TAG_Float(0), nbt.TAG_Float(0)]) return playerTag
def saveLastPosition(self, mainViewport, dimension): ''' Saves the final position of the camera viewport when the world is closed or MCEdit is exited :param mainViewport: The reference to viewport object :param dimension: The dimension the camera viewport is currently in :type dimension: int ''' log.info('Saving last position.') if "LastPosition" in self.nbt_waypoints: del self.nbt_waypoints["LastPosition"] topTag = nbt.TAG_Compound() topTag["Dimension"] = nbt.TAG_Int(dimension) pos = nbt.TAG_List() pos.append(nbt.TAG_Float(mainViewport.cameraPosition[0])) pos.append(nbt.TAG_Float(mainViewport.cameraPosition[1])) pos.append(nbt.TAG_Float(mainViewport.cameraPosition[2])) topTag["Coordinates"] = pos rot = nbt.TAG_List() rot.append(nbt.TAG_Float(mainViewport.yaw)) rot.append(nbt.TAG_Float(mainViewport.pitch)) topTag["Rotation"] = rot self.nbt_waypoints["LastPosition"] = topTag self.save()
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 save(self): del self.nbt_waypoints["Waypoints"] self.nbt_waypoints["Waypoints"] = nbt.TAG_List() for waypoint in self.waypoints.keys(): way = nbt.TAG_Compound() way["Name"] = nbt.TAG_String(waypoint.split()[0]) way["Dimension"] = nbt.TAG_Int(self.waypoints[waypoint][5]) coords = nbt.TAG_List() coords.append(nbt.TAG_Float(self.waypoints[waypoint][0])) coords.append(nbt.TAG_Float(self.waypoints[waypoint][1])) coords.append(nbt.TAG_Float(self.waypoints[waypoint][2])) rot = nbt.TAG_List() rot.append(nbt.TAG_Float(self.waypoints[waypoint][3])) rot.append(nbt.TAG_Float(self.waypoints[waypoint][4])) way["Coordinates"] = coords way["Rotation"] = rot self.nbt_waypoints["Waypoints"].append(way) self.nbt_waypoints.save(os.path.join(self.worldDirectory, u"mcedit_waypoints.dat"))
def saveLastPosition(self, mainViewport, dimension): if "LastPosition" in self.nbt_waypoints: del self.nbt_waypoints["LastPosition"] topTag = nbt.TAG_Compound() topTag["Dimension"] = nbt.TAG_Int(dimension) pos = nbt.TAG_List() pos.append(nbt.TAG_Float(mainViewport.cameraPosition[0])) pos.append(nbt.TAG_Float(mainViewport.cameraPosition[1])) pos.append(nbt.TAG_Float(mainViewport.cameraPosition[2])) topTag["Coordinates"] = pos rot = nbt.TAG_List() rot.append(nbt.TAG_Float(mainViewport.yaw)) rot.append(nbt.TAG_Float(mainViewport.pitch)) topTag["Rotation"] = rot self.nbt_waypoints["LastPosition"] = topTag self.save()
def save(self): ''' Saves all waypoint information to the 'mcedit_waypoints.dat' file in the world directory ''' del self.nbt_waypoints["Waypoints"] self.nbt_waypoints["Waypoints"] = nbt.TAG_List() for waypoint in self.waypoints.keys(): if waypoint.split()[0] == "Empty": continue way = nbt.TAG_Compound() way["Name"] = nbt.TAG_String(waypoint.split()[0]) way["Dimension"] = nbt.TAG_Int(self.waypoints[waypoint][5]) coords = nbt.TAG_List() coords.append(nbt.TAG_Float(self.waypoints[waypoint][0])) coords.append(nbt.TAG_Float(self.waypoints[waypoint][1])) coords.append(nbt.TAG_Float(self.waypoints[waypoint][2])) rot = nbt.TAG_List() rot.append(nbt.TAG_Float(self.waypoints[waypoint][3])) rot.append(nbt.TAG_Float(self.waypoints[waypoint][4])) way["Coordinates"] = coords way["Rotation"] = rot self.nbt_waypoints["Waypoints"].append(way) self.nbt_waypoints.save( os.path.join(self.worldDirectory, u"mcedit_waypoints.dat"))
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'
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