示例#1
0
 def _loadPalette(self, data):
   palletLen, data = struct.unpack("<I", data[:4])[0], data[4:]
   dr = nbt.DataReader(data)
   palette = []
   for _ in range(palletLen):
     block = nbt.decode(dr)
     palette.append(block)
   return palette, data[dr.idx:]
示例#2
0
def unpack(bbuff, data_type):
    if data_type in data_types:
        format = data_types[data_type]
        return unpack_real(bbuff, format[0], format[1])

    if data_type == "string8":
        length = unpack(bbuff, "short")
        return bbuff.recv(length)
    if data_type == "string16":
        length = unpack(bbuff, "short")
        return bbuff.recv(2 * length).decode("utf-16be")
    if data_type == "slot":
        o = {}
        o["id"] = unpack(bbuff, "short")
        if o["id"] > 0:
            o["amount"] = unpack(bbuff, "byte")
            o["damage"] = unpack(bbuff, "short")
            if o["id"] in enchantable:
                length = unpack(bbuff, "short")
                if length > 0:
                    ench = bbuff.recv(length)
                    try:
                        ench = nbt.decompress(ench)
                        ench = nbt.decode(StringIO.StringIO(ench))
                        o["enchantments"] = ench
                    except:
                        o["enchantment_data"] = ench
        return o
    if data_type == "metadata":
        metadata = []
        x = unpack(bbuff, "ubyte")
        while x != 127:
            key = x & 0x1F  # Lower 5 bits
            ty = x >> 5  # Upper 3 bits
            if ty == 0:
                val = unpack(bbuff, "byte")
            if ty == 1:
                val = unpack(bbuff, "short")
            if ty == 2:
                val = unpack(bbuff, "int")
            if ty == 3:
                val = unpack(bbuff, "float")
            if ty == 4:
                val = unpack(bbuff, "string16")
            if ty == 5:
                val = {}
                val["id"] = unpack(bbuff, "short")
                val["count"] = unpack(bbuff, "byte")
                val["damage"] = unpack(bbuff, "short")
            if ty == 6:
                val = []
                for i in range(3):
                    val.append(unpack(bbuff, "int"))
            metadata.append((key, (ty, val)))
            x = unpack(bbuff, "byte")
        return metadata
示例#3
0
 def _loadEntities(self, db):
   try:
     data = ldb.get(db, self.keyBase + b"2")
   except KeyError:
     return []
   data = nbt.DataReader(data)
   entities = []
   while not data.finished():
     entities.append(nbt.decode(data))
   return entities
示例#4
0
def unpack(bbuff, data_type):
    if data_type in data_types:
        format = data_types[data_type]
        return unpack_real(bbuff, format[0], format[1])
    
    if data_type == "string8":
        length = unpack(bbuff, 'short')
        return bbuff.recv(length)
    if data_type == "string16":
        length = unpack(bbuff, 'short')
        return bbuff.recv(2*length).decode('utf-16be')
    if data_type == "slot":
        o = {}
        o["id"] = unpack(bbuff, 'short')
        if o["id"] > -1:
            o["amount"] = unpack(bbuff, 'byte')
            o["damage"] = unpack(bbuff, 'short')
            if o['id'] > -1:
                length = unpack(bbuff, 'short')
                if length > 0:
                    ench = bbuff.recv(length)
                    try: 
                        ench = nbt.decompress(ench)
                        ench = nbt.decode(StringIO.StringIO(ench))
                        o["enchantments"] = ench
                    except:
                        o["enchantment_data"] = ench
        return o
    if data_type == "metadata":
        metadata = []
        x = unpack(bbuff, 'ubyte')
        while x != 127:
            val = None
            key = x & 0x1F # Lower 5 bits
            ty  = x >> 5   # Upper 3 bits
            if ty == 0: val = unpack(bbuff, 'byte') 
            if ty == 1: val = unpack(bbuff, 'short') 
            if ty == 2: val = unpack(bbuff, 'int') 
            if ty == 3: val = unpack(bbuff, 'float') 
            if ty == 4: val = unpack(bbuff, 'string16')
            if ty == 5:
                val = {}
                val["id"]     = unpack(bbuff, 'short')
                val["count"]  = unpack(bbuff, 'byte')
                val["damage"] = unpack(bbuff, 'short')
            if ty == 6:
                val = []
                for i in range(3):
                    val.append(unpack(bbuff, 'int'))
            assert val is not None, "unknown val:" + str(ty)
            metadata.append((key, (ty, val)))
            x = unpack(bbuff, 'ubyte')
        return metadata
示例#5
0
 def _loadTileEntities(self, db):
   try:
     data = ldb.get(db, self.keyBase + b"1")
   except KeyError:
     return
   data = nbt.DataReader(data)
   while not data.finished():
     nbtData = nbt.decode(data)
     x = nbtData.pop("x").payload # We add back theses with the correct value on save, they are important.
     y = nbtData.pop("y").payload
     z = nbtData.pop("z").payload
     block = self.getBlock(x % 16, y, z % 16)
     if not block:
       print("Warning: Cannot apply nbt to block at {} {} {} since it does not exist.".format(x, y, z))
       continue
     block.nbt = nbtData