Ejemplo n.º 1
0
 def add_data_to_tag(self, tag):
     tag.tags.append(TAG_String(name='id', value=self.i_id))
     tag.tags.append(TAG_Short(name='Damage', value=int(self.damage)))
     tag.tags.append(TAG_Byte(name='Count', value=1))
     subtag = TAG_Compound()
     subtag.name = 'tag'
     did_append = False
     if len(self.enchants) > 0:
         enchant = TAG_List(name='ench', type=TAG_Compound)
         for ench in self.enchants:
             enchant.tags.append(ench.getNBTTag())
         subtag.tags.append(enchant)
         did_append = True
     if len(self.attributes) > 0:
         attributes = TAG_List(name='AttributeModifiers', type=TAG_Compound)
         for attribute in self.attributes:
             attributes.tags.append(attribute.getNBTTag())
         subtag.tags.append(attributes)
         did_append = True
     if self.name is not None or len(self.lore) > 0:
         display = TAG_Compound()
         display.name = 'display'
         if self.name is not None:
             display.tags.append(TAG_String(name='Name', value=self.name))
         if len(self.lore) > 0:
             lore_tag = TAG_List(name='Lore', type=TAG_String)
             for lore in self.lore:
                 lore_tag.tags.append(TAG_String(value=lore))
             display.tags.append(lore_tag)
         subtag.tags.append(display)
         did_append = True
     if did_append:
         tag.tags.append(subtag)
Ejemplo n.º 2
0
def generate_level():
    level = NBTFile() # Blank NBT
    level.name = "Data"
    level.tags.extend([
        TAG_Long(name="Time", value=1),
        TAG_Long(name="LastPlayed", value=int(time.time())),
        TAG_Int(name="SpawnX", value=0),
        TAG_Int(name="SpawnY", value=2),
        TAG_Int(name="SpawnZ", value=0),
        TAG_Long(name="SizeOnDisk", value=0),
        TAG_Long(name="RandomSeed", value=random.randrange(1,9999999999)),
        TAG_Int(name="version", value=19132),
        TAG_String(name="LevelName", value="Testing")
    ])
    
    player = TAG_Compound()
    player.name = "Player"
    player.tags.extend([
        TAG_Int(name="Score", value=0),
        TAG_Int(name="Dimension", value=0)
    ])
    inventory = TAG_Compound()
    inventory.name = "Inventory"
    player.tags.append(inventory)
    level.tags.append(player)
    
    return level
Ejemplo n.º 3
0
def pack_nbt(s):
    """
    Pack a native Python data structure into an NBT tag. Only the following
    structures and types are supported:

     * int
     * float
     * str
     * unicode
     * dict

    Additionally, arbitrary iterables are supported.

    Packing is not lossless. In order to avoid data loss, TAG_Long and
    TAG_Double are preferred over the less precise numerical formats.

    Lists and tuples may become dicts on unpacking if they were not homogenous
    during packing, as a side-effect of NBT's format. Nothing can be done
    about this.

    Only strings are supported as keys for dicts and other mapping types. If
    your keys are not strings, they will be coerced. (Resistance is futile.)
    """

    if isinstance(s, int):
        return TAG_Long(s)
    elif isinstance(s, float):
        return TAG_Double(s)
    elif isinstance(s, (str, unicode)):
        return TAG_String(s)
    elif isinstance(s, dict):
        tag = TAG_Compound()
        for k, v in s:
            v = pack_nbt(v)
            v.name = str(k)
            tag.tags.append(v)
        return tag
    elif hasattr(s, "__iter__"):
        # We arrive at a slight quandry. NBT lists must be homogenous, unlike
        # Python lists. NBT compounds work, but require unique names for every
        # entry. On the plus side, this technique should work for arbitrary
        # iterables as well.
        tags = [pack_nbt(i) for i in s]
        t = type(tags[0])
        # If we're homogenous...
        if all(t == type(i) for i in tags):
            tag = TAG_List(type=t)
            tag.tags = tags
        else:
            tag = TAG_Compound()
            for i, item in enumerate(tags):
                item.name = str(i)
            tag.tags = tags
        return tag
    else:
        raise ValueError("Couldn't serialise type %s!" % type(s))
Ejemplo n.º 4
0
 def __init__(self, playerName, xpos, ypos, zpos):
         self.filename = "%s.dat" % (playerName)
         self.player = NBTFile()
         motion = TAG_List(name = "Motion", type = TAG_Double)
         motion.insert(0, TAG_Double(value = 0))
         motion.insert(1, TAG_Double(value = 0))
         motion.insert(2, TAG_Double(value = 0))
         pos = TAG_List(name = "Pos", type = TAG_Double)
         pos.insert(0, TAG_Double(value = xpos))
         pos.insert(1, TAG_Double(value = ypos))
         pos.insert(2, TAG_Double(value = zpos))
         rotation = TAG_List(name = "Rotation", type = TAG_Float)
         rotation.insert(0, TAG_Float(value = 0))
         rotation.insert(1, TAG_Float(value = 0))
         abilities = TAG_Compound()
         abilities.name = "abilities"
         abilities.tags.extend([
                 TAG_Byte(name = "flying", value = 0),
                 TAG_Byte(name = "instabuild", value = 0),
                 TAG_Byte(name = "invulnerable", value = 0),
                 TAG_Byte(name = "mayfly", value = 0)
         ])
         self.player.tags.extend([
                 TAG_Byte(name = "OnGround", value = 1),
                 TAG_Byte(name = "Sleeping", value = 0),
                 TAG_Short(name = "Air", value = 300),
                 TAG_Short(name = "AttackTime", value = 0),
                 TAG_Short(name = "DeathTime", value = 0),
                 TAG_Short(name = "Fire", value = -20),
                 TAG_Short(name = "Health", value = 20),
                 TAG_Short(name = "HurtTime", value = 0),
                 TAG_Short(name = "SleepTimer", value = 0),
                 TAG_Int(name = "Dimension", value = 0),
                 TAG_Int(name = "foodLevel", value = 20),
                 TAG_Int(name = "foodTickTimer", value = 0),
                 TAG_Int(name = "playerGameType", value = 0),
                 TAG_Int(name = "SpawnX", value = xpos),
                 TAG_Int(name = "SpawnY", value = ypos),
                 TAG_Int(name = "SpawnZ", value = zpos),
                 TAG_Int(name = "XpLevel", value = 0),
                 TAG_Int(name = "XpTotal", value = 0),
                 TAG_Float(name = "fallDistance", value = 0),
                 TAG_Float(name = "foodExhaustionLevel", value = 0),
                 TAG_Float(name = "foodSaturationLevel", value = 5),
                 TAG_Float(name = "XpP", value = 0),
                 TAG_List(name = "Inventory", type = TAG_Compound),
                 motion,
                 pos,
                 rotation,
                 abilities
         ])
Ejemplo n.º 5
0
 def process_entity(self, entity, randomitems):
         slot = 0
         updated = False
         items = items_from_nbt(entity["Items"])
         if sum(items.values()) == 0:
                 print("empty chest found")
                 itemlist = TAG_List(type=TAG_Compound)
                 for randomitem in randomitems:
                         if slot < 27:
                                 if random.uniform(0,100) < float(randomitem[3]):
                                         item = TAG_Compound()
                                         try:
                                                 quantity = int(randomitem[2])
                                         except ValueError:
                                                 try:
                                                         lowrand, highrand = randomitem[2].split('-')
                                                         quantity = random.randrange(int(lowrand), int(highrand) + 1)
                                                 except ValueError:
                                                         print("ERROR: Invalid quantity range.  Defaulting to 1.")
                                                         line = ''
                                                         for section in randomitem:
                                                                 line += ' ' + section
                                                         print(line)
                                                         quantity = 1
                                         if quantity > 127:
                                                 quantity = 127
                                         elif quantity < 1:
                                                 quantity = 1
                                         item.tags.extend([
                                                 TAG_Byte(name="Count", value=quantity),
                                                 TAG_Byte(name="Slot", value=slot),
                                                 TAG_Short(name="Damage", value=int(randomitem[1])),
                                                 TAG_Short(name="id", value=int(randomitem[0]))
                                         ])
                                         if len(randomitem) > 4:
                                                 enchants = TAG_List(name="ench", type=TAG_Compound)
                                                 count = 4
                                                 try:
                                                         while len(randomitem) > count:
                                                                 enchant = TAG_Compound()
                                                                 enchant.tags.extend([
                                                                         TAG_Short(name="id", value=int(randomitem[count])),
                                                                         TAG_Short(name="lvl", value=int(randomitem[count+1]))
                                                                 ])
                                                                 enchants.insert((count-4)/2,enchant)
                                                                 count += 2
                                                         tag = TAG_Compound()
                                                         tag.tags.extend([enchants])
                                                         tag.name = "tag"
                                                         item.tags.extend([tag])
                                                 except IndexError:
                                                         print("ERROR: invalid enchant data")
                                                         line = ''
                                                         for section in randomitem:
                                                                 line += ' ' + section
                                                         print(line)
                                         itemlist.insert(slot,item)
                                         slot += 1
                                         updated = True
                 if updated:
                         entity["Items"] = itemlist
         return updated