def encode(self) -> bytes: out = Buffer.pack_varint(self.action) + len(self.players) if self.action == 0: # add player for player in self.players: out += ( Buffer.pack_uuid(player["uuid"]) + Buffer.pack_string(player["name"]) + Buffer.pack_varint(len(player["properties"])) ) for prop in player["properties"]: out += ( Buffer.pack_string(prop["name"]) + Buffer.pack_string(prop["value"]) + Buffer.pack_optional(Buffer.pack_string, prop.get("signature")) ) out += ( Buffer.pack_varint(player["gamemode"]) + Buffer.pack_varint(player["ping"]) + Buffer.pack_optional(Buffer.pack_chat, player["display_name"]) ) elif self.action == 1: # update gamemode out += b"".join(Buffer.pack_uuid(p["uuid"]) + Buffer.pack_varint(p["gamemode"]) for p in self.players) elif self.action == 2: # update latency out += b"".join(Buffer.pack_uuid(p["uuid"]) + Buffer.pack_varint(p["ping"]) for p in self.players) elif self.action == 3: # update display name out += b"".join(Buffer.pack_uuid(p["uuid"]) + Buffer.pack_optional(p.get("display_name")) for p in self.players) elif self.action == 4: out += b"".join(Buffer.pack_uuid(p["uuid"]) for p in self.players) return out
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
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
async def join(stream: Stream, uuid_: uuid.UUID, username: str, props: list) -> None: server.cache.uuid[stream.remote] = int(uuid_) # update uuid cache player = await server.playerio.fetch_player( uuid_) # fetch player data from disk player.props = props player.stream = stream player.username = username world = server.worlds[ player["Dimension"].data] # the world player *should* be spawning into await send_join_game_packet(stream, world, player) # send server brand via plugin channels await server.send_packet( stream, packets.play.plugin_msg.PlayPluginMessageClientBound( "minecraft:brand", Buffer.pack_string(server.meta.pymine)), ) # sends info about the server difficulty await server.send_packet( stream, packets.play.difficulty.PlayServerDifficulty( world["Difficulty"].data, world["DifficultyLocked"].data), ) await send_player_abilities(stream, player) await join_2(stream, player)
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))
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))
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 )
def encode(self) -> bytes: return (Buffer.pack_nbt(self.dimension) + Buffer.pack_string(self.world_name) + Buffer.pack("l", self.hashed_seed) + Buffer.pack("B", self.gamemode) + Buffer.pack("B", self.prev_gamemode) + Buffer.pack("?", self.is_debug) + Buffer.pack("?", self.is_flat) + Buffer.pack("?", self.copy_metadata))
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"]]) )
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
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
def test_string(): buf = Buffer() buf.write(Buffer.pack_string("")) buf.write(Buffer.pack_string("")) buf.write(Buffer.pack_string("2")) buf.write( Buffer.pack_string( "adkfj;adkfa;ldkfj\x01af\t\n\n00;\xc3\x85\xc3\x84\xc3\x96")) buf.write(Buffer.pack_string("")) buf.write(Buffer.pack_string("BrUh")) buf.write(Buffer.pack_string("")) assert buf.unpack_string() == "" assert buf.unpack_string() == "" assert buf.unpack_string() == "2" assert buf.unpack_string( ) == "adkfj;adkfa;ldkfj\x01af\t\n\n00;\xc3\x85\xc3\x84\xc3\x96" assert buf.unpack_string() == "" assert buf.unpack_string() == "BrUh" assert buf.unpack_string() == ""
def encode(self) -> bytes: return Buffer.pack_string(self.url) + Buffer.pack_string(self.hash_)
def encode(self) -> bytes: return Buffer.pack_uuid(self.uuid) + Buffer.pack_string(self.username)
def encode(self) -> bytes: return (Buffer.pack_string(self.objective_name) + Buffer.pack("b", self.mode) + Buffer.pack_optional( Buffer.pack_chat, Chat(self.objective_value)) + Buffer.pack_optional(Buffer.pack_varint, self.type_))
def encode(self) -> bytes: return (Buffer.pack_string(self.entity_name) + self.action + Buffer.pack_string(self.objective_name) + Buffer.pack_optional_varint(self.value))
def encode(self) -> bytes: return Buffer.pack("b", self.pos) + Buffer.pack_string(self.score)
def encode(self) -> bytes: return Buffer.pack("b", self.window_id) + Buffer.pack_string( self.recipe_identifier)
def encode(self) -> bytes: return Buffer.pack_string(self.channel) + self.data