Esempio n. 1
0
    def encode(self) -> bytes:
        out = Buffer.pack_string(self.team_name) + Buffer.pack("b", self.mode)

        if self.mode == 0:  # create team
            out += (Buffer.pack_chat(Chat(self.data["team_display_name"])) +
                    Buffer.pack("b", self.data["friendly_flags"]) +
                    Buffer.pack_string(self.data["name_tag_visibility"]) +
                    Buffer.pack_string(self.data["collision_rule"]) +
                    Buffer.pack_varint(self.data["team_color"]) +
                    Buffer.pack_chat(Chat(self.data["team_prefix"])) +
                    Buffer.pack_chat(Chat(self.data["team_suffix"])) +
                    Buffer.pack_varint(len(self.data["entities"])) + b"".join(
                        [Buffer.pack_string(e)
                         for e in self.data["entities"]]))
        elif self.mode == 2:  # update team info
            out += (Buffer.pack_chat(Chat(self.data["team_display_name"])) +
                    Buffer.pack("b", self.data["friendly_flags"]) +
                    Buffer.pack_string(self.data["name_tag_visibility"]) +
                    Buffer.pack_string(self.data["collision_rule"]) +
                    Buffer.pack_varint(self.data["team_color"]) +
                    Buffer.pack_chat(Chat(self.data["team_prefix"])) +
                    Buffer.pack_chat(Chat(self.data["team_suffix"])))
        elif self.mode == 3:  # add entities to team
            out += Buffer.pack_varint(len(self.data["entities"])) + b"".join(
                [Buffer.pack_string(e) for e in self.data["entities"]])
        elif self.mode == 4:  # remove entities from team
            out += Buffer.pack_varint(len(self.data["entities"])) + b"".join(
                [Buffer.pack_string(e) for e in self.data["entities"]])

        return out
Esempio n. 2
0
    def encode(self) -> bytes:
        out = Buffer.pack_varint(len(self.nodes))

        for node in self.nodes:
            pass  # idek yet

        return out + Buffer.pack_varint(0)
Esempio n. 3
0
    def encode(self) -> bytes:
        out = Buffer.pack_varint(len(self.stats))

        for entry in self.stats:
            out += b"".join([Buffer.pack_varint(e) for e in entry])

        return out
Esempio n. 4
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_position(self.x, self.y, self.z)
         + Buffer.pack_varint(self.block)
         + Buffer.pack_varint(self.status)
         + Buffer.pack("?", self.successful)
     )
Esempio n. 5
0
    def encode(self) -> bytes:
        out = (
            Buffer.pack_varint(self.map_id)
            + Buffer.pack("b", self.scale)
            + Buffer.pack("?", self.tracking_pos)
            + Buffer.pack("?", self.locked)
            + Buffer.pack_varint(len(self.icons))
        )

        for icon in self.icons:
            out += Buffer.pack_varint(icon["type"]) + Buffer.pack("b", icon["x"]) + Buffer.pack("b", icon["z"])

            display_name = icon.get("display_name")

            if display_name is not None:
                out += Buffer.pack("?", True) + Buffer.pack_chat(Chat(display_name))
            else:
                out += Buffer.pack("?", False)

        out += (
            Buffer.pack("B", self.cols)
            + Buffer.pack_optional((lambda x: Buffer.pack("B", x)), self.rows)
            + Buffer.pack_optional((lambda x: Buffer.pack("b", x)), self.x)
            + Buffer.pack_optional((lambda z: Buffer.pack("b", z)), self.z)
        )

        if self.data is not None:
            out += Buffer.pack("?", True) + Buffer.pack_varint(len(self.data)) + Buffer.pack("?", True) + self.data
        else:
            out += Buffer.pack("?", False) + Buffer.pack("?", False)

        return out
Esempio n. 6
0
    def encode(self) -> bytes:
        out = b""

        for tags, REG in (
            (
                self.block,
                BLOCK_REGISTRY,
            ),
            (
                self.item,
                ITEM_REGISTRY,
            ),
            (
                self.fluid,
                FLUID_REGISTRY,
            ),
            (
                self.entity,
                ENTITY_REGISTRY,
            ),
        ):
            out += Buffer.pack_varint(len(tags))  # pack length

            for identifier in tags:
                # pack identifier name and  length of upcoming array
                out += Buffer.pack_string(identifier) + Buffer.pack_varint(
                    len(tags[identifier]))

                for value in tags[identifier]:
                    # values should be encoded as varints, so we need their id
                    out += Buffer.pack_varint(REG.encode(value))

        return out
Esempio n. 7
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_varint(self.sound_id)
         + Buffer.pack_varint(self.category)
         + Buffer.pack_varint(self.eid)
         + Buffer.pack("f", self.volume)
         + Buffer.pack("f", self.pitch)
     )
Esempio n. 8
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_string(" " * 20)
         + Buffer.pack_varint(len(self.public_key))
         + self.public_key
         + Buffer.pack_varint(len(self.verify_token))
         + self.verify_token
     )
Esempio n. 9
0
 def encode(self) -> bytes:
     return (Buffer.pack_varint(self.window_id) +
             Buffer.pack("b", len(self.trades)) +
             b"".join(Buffer.pack_trade(**trade) for trade in self.trades) +
             Buffer.pack_varint(self.villager_lvl) +
             Buffer.pack_varint(self.xp) +
             Buffer.pack("?", self.is_regular) +
             Buffer.pack("?", self.can_restock))
Esempio n. 10
0
 def encode(self) -> bytes:
     return (Buffer.pack_varint(self.entity_id) +
             Buffer.pack_uuid(self.object_uuid) +
             Buffer.pack_varint(self.type_) + Buffer.pack("d" + self.x) +
             Buffer.pack("d" + self.y) + Buffer.pack("d" + self.z) +
             Buffer.pack("i" + self.pitch) + Buffer.pack("i" + self.yaw) +
             Buffer.pack("h" + self.vloc_x) +
             Buffer.pack("h" + self.vloc_x) +
             Buffer.pack("h" + self.vloc_z))
Esempio n. 11
0
 def encode(self) -> bytes:
     return (Buffer.pack("i", self.chunk_x) +
             Buffer.pack("i", self.chunk_z) +
             Buffer.pack("?", self.trust_edges) +
             Buffer.pack_varint(self.sky_light_mask) +
             Buffer.pack_varint(self.block_light_mask) +
             Buffer.pack_varint(self.empty_sky_light_max) +
             Buffer.pack_varint(self.empty_block_light_mask) +
             self.sky_light_array + self.block_light_array)
Esempio n. 12
0
    def encode(self) -> bytes:
        out = (Buffer.pack_varint(self.feet_or_eyes) +
               Buffer.pack("d", self.tx) + Buffer.pack("d", self.ty) +
               Buffer.pack("d", self.tz))

        if self.is_entity:
            out += Buffer.pack_varint(self.entity_id) + Buffer.pack_varint(
                self.entity_feet_or_eyes)

        return out
Esempio n. 13
0
def test_varint(var_int, error_msg):
    buf = Buffer()
    if error_msg:
        with pytest.raises(ValueError) as err:

            buf.write(Buffer.pack_varint(var_int))
            assert error_msg in str(err)
    else:
        buf.write(Buffer.pack_varint(var_int))
        assert buf.unpack_varint() == var_int
Esempio n. 14
0
def test_varint():
    buf = Buffer()

    buf.write(Buffer.pack_varint(0))
    buf.write(Buffer.pack_varint(1))
    buf.write(Buffer.pack_varint(3749146))

    assert buf.unpack_varint() == 0
    assert buf.unpack_varint() == 1
    assert buf.unpack_varint() == 3749146
Esempio n. 15
0
    def encode(self) -> bytes:
        out = Buffer.pack_varint(self.entity_id)

        for prop in self.properties:
            out += (
                Buffer.pack_string(prop["key"])
                + Buffer.pack("d", prop["value"])
                + Buffer.pack_varint(len(prop["modifiers"]))
                + b"".join([Buffer.pack_modifier(m) for m in prop["modifiers"]])
            )
Esempio n. 16
0
    def encode(self) -> bytes:
        out = (Buffer.pack_varint(((self.chunk_sect_x & 0x3FFFFF) << 42)
                                  | (self.chunk_sect_y & 0xFFFFF) | (
                                      (self.chunk_sect_z & 0x3FFFFF) << 20)) +
               Buffer.pack("?", self.trust_edges) +
               Buffer.pack_varint(len(self.blocks)))

        for block_id, local_x, local_y, local_z in self.blocks:
            out += Buffer.pack_varint(block_id << 12
                                      | (local_x << 8 | local_z << 4
                                         | local_y))

        return out
Esempio n. 17
0
    def encode(self) -> bytes:
        # if self.event == 0:  # start combat
        #     return Buffer.pack_varint(self.event)
        #
        # if self.event == 1:  # end combat
        #     return Buffer.pack_varint(self.event) + Buffer.pack_varint(self.data['duration']) + \
        #         Buffer.pack('i', self.data['opponent'])

        if self.event == 2:  # entity dead, only one actually used
            return (Buffer.pack_varint(self.event) +
                    Buffer.pack_varint(self.data["player_id"]) +
                    Buffer.pack("i", self.data["entity_id"]) +
                    Buffer.pack_chat(self.data["message"]))
Esempio n. 18
0
    def encode(self) -> bytes:
        out = Buffer.pack_varint(self.action)

        if self.action == 0:  # set size
            out += Buffer.pack("d", self.data["diameter"])
        elif self.action == 1:  # lerp size
            out += (Buffer.pack("d", self.data["old_diameter"]) +
                    Buffer.pack("d", self.data["new_diameter"]) +
                    Buffer.pack_varint(self.data["speed"]))
        elif self.action == 2:  # set center
            out += Buffer.pack("d", self.data["x"]) + Buffer.pack(
                "d", self.data["z"])
        elif self.action == 3:  # initialize
            out += (Buffer.pack("d", self.data["x"]) +
                    Buffer.pack("d", self.data["z"]) +
                    Buffer.pack("d", self.data["old_diameter"]) +
                    Buffer.pack("d", self.data["new_diameter"]) +
                    Buffer.pack_varint(self.data["speed"]) +
                    Buffer.pack_varint(self.data["portal_teleport_boundary"]) +
                    Buffer.pack_varint(self.data["warning_blocks"]) +
                    Buffer.pack_varint(self.data["warning_time"]))
        elif self.action == 4:  # set warning time
            out += Buffer.pack_varint(self.data["warning_time"])
        elif self.action == 5:  # set warning blocks
            out += Buffer.pack_varint(self.data["warning_blocks"])

        return out
Esempio n. 19
0
    def encode(self) -> bytes:
        out = Buffer.pack_varint(self.chunk.x) + Buffer.pack_varint(self.chunk.z) + Buffer.pack("?", True)

        sky_light_mask = 0
        block_light_mask = 0
        empty_sky_light_mask = 0
        empty_block_light_mask = 0

        sky_light_arrays = []
        block_light_arrays = []

        for y, section in self.chunk.sections.items():
            if y >= 0:
                if section.sky_light is not None:
                    if len(section.sky_light.nonzero()) == 0:
                        empty_sky_light_mask |= 1 << y
                    else:
                        sky_light_mask |= 1 << y

                        data = []

                        for y in range(16):
                            for z in range(16):
                                for x in range(0, 16, 2):
                                    data.append(
                                        Buffer.pack("b", section.sky_light[x][y][z] | (section.sky_light[x + 1][y][z] << 4))
                                    )

                        sky_light_arrays.append(b"".join(data))

                if section.block_light is not None:
                    if len(section.block_light.nonzero()) == 0:
                        empty_block_light_mask |= 1 << y
                    else:
                        block_light_mask |= 1 << y

                        data = []

                        for y in range(16):
                            for z in range(16):
                                for x in range(0, 16, 2):
                                    data.append(
                                        Buffer.pack(
                                            "b", section.block_light[x][y][z] | (section.block_light[x + 1][y][z] << 4)
                                        )
                                    )

                        block_light_arrays.append(b"".join(data))

        return (
            out
            + Buffer.pack_varint(sky_light_mask)
            + Buffer.pack_varint(block_light_mask)
            + Buffer.pack_varint(empty_sky_light_mask)
            + Buffer.pack_varint(empty_block_light_mask)
            + Buffer.pack_varint(len(sky_light_arrays))
            + b"".join(sky_light_arrays)
            + Buffer.pack_varint(len(block_light_arrays))
            + b"".join(block_light_arrays)
        )
Esempio n. 20
0
    def encode(self) -> bytes:
        out = (Buffer.pack_varint(self.id) + Buffer.pack_varint(self.start) +
               Buffer.pack_varint(self.length) +
               Buffer.pack_varint(len(self.matches)))

        for m in self.matches:
            out += Buffer.pack_string(m[0])

            if len(m) > 1:
                out += Buffer.pack("?", True) + Buffer.pack_chat(Chat(m[1]))
            else:
                out += Buffer.pack("?", False)

        return out
Esempio n. 21
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_varint(self.entity_id)
         + Buffer.pack("f", self.yaw)
         + Buffer.pack("f", self.pitch)
         + Buffer.pack("?", self.on_ground)
     )
Esempio n. 22
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_varint(self.eid)
         + Buffer.pack("h", self.vel_x)
         + Buffer.pack("h", self.vel_y)
         + Buffer.pack("h", self.vel_z)
     )
Esempio n. 23
0
 def encode(self) -> bytes:
     return (Buffer.pack_string(self.name) +
             Buffer.pack_varint(self.category) +
             Buffer.pack("i", self.category) +
             Buffer.pack("i", self.effect_pos_x) +
             Buffer.pack("i", self.effect_pos_y) +
             Buffer.pack("i", self.effect_pos_z) +
             Buffer.pack("f", self.volume) + Buffer.pack("f", self.pitch))
Esempio n. 24
0
 def encode(self) -> bytes:
     return (Buffer.pack("d", self.player.x) +
             Buffer.pack("d", self.player.y) +
             Buffer.pack("d", self.player.z) +
             Buffer.pack("f", self.player["Rotation"][0].data)  # yaw
             + Buffer.pack("f", self.player["Rotation"][1].data)  # pitch
             + Buffer.pack("b", self.flags) +
             Buffer.pack_varint(self.teleport_id))
Esempio n. 25
0
 def encode(self) -> bytes:
     return (Buffer.pack("i", self.entity_id) +
             Buffer.pack("?", self.is_hardcore) +
             Buffer.pack("B", self.gamemode) +
             Buffer.pack("b", self.prev_gamemode) +
             Buffer.pack_varint(len(self.world_names)) +
             b"".join([Buffer.pack_string(w) for w in self.world_names]) +
             Buffer.pack_nbt(self.dim_codec) +
             Buffer.pack_nbt(self.dimension) +
             Buffer.pack_string(self.world_name) +
             Buffer.pack("q", self.hashed_seed) +
             Buffer.pack_varint(self.max_players) +
             Buffer.pack_varint(self.view_distance) +
             Buffer.pack("?", self.reduced_debug_info) +
             Buffer.pack("?", self.enable_respawn_screen) +
             Buffer.pack("?", self.is_debug) +
             Buffer.pack("?", self.is_flat))
Esempio n. 26
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_varint(self.entity_id)
         + Buffer.pack("h", self.dx)
         + Buffer.pack("h", self.dy)
         + Buffer.pack("h", self.dz)
         + Buffer.pack("?", self.on_ground)
     )
Esempio n. 27
0
    def encode(self) -> bytes:
        out = Buffer.pack_varint(self.action)

        if 2 >= self.action >= 0:
            out += Buffer.pack_chat(Chat(self.data))
        elif self.action == 3:
            out += b"".join([Buffer.pack("i", i) for i in self.data])

        return out
Esempio n. 28
0
 def encode(self) -> bytes:
     return (
         Buffer.pack("d", self.x)
         + Buffer.pack("d", self.y)
         + Buffer.pack("d", self.z)
         + Buffer.pack("f", self.yaw)
         + Buffer.pack("f", self.pitch)
         + self.flags
         + Buffer.pack_varint(self.tp_id)
     )
Esempio n. 29
0
 def encode(self) -> bytes:
     return (
         Buffer.pack_varint(self.eid)
         + Buffer.pack("d", self.x)
         + Buffer.pack("d", self.y)
         + Buffer.pack("d", self.z)
         + Buffer.pack("i", self.yaw)
         + Buffer.pack("i", self.pitch)
         + Buffer.pack("?", self.on_ground)
     )
Esempio n. 30
0
    def encode(self) -> bytes:
        out = (Buffer.pack_varint(self.action) +
               Buffer.pack("?", self.crafting_book_open) +
               Buffer.pack("?", self.crafting_book_filter_active) +
               Buffer.pack("?", self.smelting_book_open) +
               Buffer.pack("?", self.smelting_book_filter_active) +
               Buffer.pack("?", self.blast_furnace_book_open) +
               Buffer.pack("?", self.blast_furnace_book_filter_active) +
               Buffer.pack("?", self.smoker_book_open) +
               Buffer.pack("?", self.smoker_book_filter_active) +
               Buffer.pack_varint(len(self.recipe_ids_1)) +
               b"".join(Buffer.pack_string(rid) for rid in self.recipe_ids_1))

        if self.recipe_ids_2:
            out += Buffer.pack("?", True) + b"".join(
                Buffer.pack_string(rid) for rid in self.recipe_ids_2)
        else:
            out += Buffer.pack("?", False)

        return out