def update_player_map_data(prev_x_offset, prev_z_offset, new_x_offset, new_z_offset, player, tp_packet): curr_time = time.time() player.loading = True init_splash_screen = helpers.gen_chat_packet("Loading map...", 100) end_splash_screen = helpers.gen_chat_packet("Map loaded!", 100) init_loading_text = helpers.gen_chat_packet("Loading...", 1) end_loading_text = helpers.gen_chat_packet("", 1) use_bbu = helpers.get_extension_state(player, ('BulkBlockUpdate', 1)) player.add_packet(init_splash_screen) if helpers.get_extension_state(player, ('MessageTypes', 1)): player.add_packet(init_loading_text) fast_packets_to_send = calc_bbu_map_change_packets(prev_x_offset, prev_z_offset, new_x_offset, new_z_offset, use_bbu) for result in fast_packets_to_send: for packet in result: player.add_packet(packet) if tp_packet is not None: player.add_packet(tp_packet) if helpers.get_extension_state(player, ('MessageTypes', 1)): player.add_packet(end_loading_text) player.add_packet(end_splash_screen) player.last_teleport_time = time.time() player.loading = False print("took", time.time() - curr_time)
def handle_spawn_other_players(proto_inst): tmp_player = proto_inst.player for other_player in proto_inst.factory.data.players: if other_player != tmp_player: tmp_is_long = helpers.get_extension_state(tmp_player, ('ExtEntityPositions', 1)) other_is_long = helpers.get_extension_state(other_player, ('ExtEntityPositions', 1)) spawn_tmp_player_packet = helpers.gen_player_spawn_packet(tmp_player, False, other_is_long) spawn_other_player_packet = helpers.gen_player_spawn_packet(other_player, False, tmp_is_long) other_player.add_packet(spawn_tmp_player_packet) tmp_player.add_packet(spawn_other_player_packet)
def handle_pos_update_packet(data, proto_inst): tmp_player = proto_inst.player if helpers.get_extension_state(tmp_player, ('ExtEntityPositions', 1)): pos_data = struct.unpack('!bbiiicc', data) else: pos_data = struct.unpack('!bbhhhcc', data) tmp_player.update_pos(pos_data)
def command_handle_tp(player, args, proto_inst): name = args[0] players_matched = get_players(name, proto_inst) if len(players_matched) > 1: players_list = '' for curr_player in players_matched: players_list += curr_player.username + " " info = helpers.handle_gen_chat_packets( r"More than 1 player matched: &6" + players_list, 0, 0) for packet in info: player.add_packet(packet) elif len(players_matched) == 1: dest_player = players_matched[0] is_long = helpers.get_extension_state(player, ('ExtEntityPositions', 1)) tp_packet = helpers.gen_pos_packet(-1, dest_player.xpos, dest_player.ypos, dest_player.zpos, dest_player.yaw, dest_player.pitch, is_long) player.add_packet(tp_packet) player.teleport_to(dest_player) if not (player.x_offset == dest_player.x_offset and player.z_offset == dest_player.z_offset): map_handler.get_player_tp_map_data(player, dest_player) player.teleport_to(dest_player) else: info = helpers.gen_chat_packet("No such player found.", 0) player.add_packet(info)
def handle_set_init_pos(proto_inst): is_long = helpers.get_extension_state(proto_inst.player, ('ExtEntityPositions', 1)) spawn_packet = helpers.gen_player_spawn_packet(proto_inst.player, True, is_long) proto_inst.transport.write(spawn_packet) init_pos_packet = helpers.gen_player_pos_packet(proto_inst.player, True) proto_inst.transport.write(init_pos_packet)
def handle_chat_packet(data, proto_inst): tmp_player = proto_inst.player chat_data = struct.unpack('!bb64s', data) if helpers.get_extension_state(tmp_player, ('LongerMessages', 1)): tmp_player.add_msg(chat_data[2]) if chat_data[1] == 0: handle_chat(tmp_player, proto_inst) else: tmp_player.add_msg(chat_data[2]) handle_chat(tmp_player, proto_inst)
def finish_server_handshake(proto_inst): handle_send_server_ident(proto_inst) helpers.send_map(map_handler.get_initial_map_compressed(), config.map_dims.x, config.map_dims.y, config.map_dims.z, proto_inst) handle_set_init_pos(proto_inst) if helpers.get_extension_state(proto_inst.player, ('MessageTypes', 1)): proto_inst.transport.write(helpers.gen_chat_packet('Curr pos:', 12)) handle_reconnect(proto_inst) handle_spawn_other_players(proto_inst) proto_inst.factory.data.players.append(proto_inst.player) handle_inform_player_spawn(proto_inst)
def handle_update_player_positions(players): for player in players: curr_x_pos = player.x_offset + (player.xpos // 32) - config.map_dims.x // 2 curr_z_pos = player.z_offset + (player.zpos // 32) - config.map_dims.z // 2 if helpers.get_extension_state(player, ('MessageTypes', 1)): player.add_packet( helpers.gen_chat_packet( "(" + str(curr_x_pos) + ", " + str(curr_z_pos) + ")", 11)) for curr_player in players: if player != curr_player: loc_packet = helpers.gen_relative_pos_packet( player, curr_player) curr_player.add_packet(loc_packet)
def handle_pos_tp_check(self): p_block_xpos = self.xpos // 32 p_block_zpos = self.zpos // 32 prev_x_offset = self.x_offset prev_z_offset = self.z_offset big_border_size = config.BORDER_SIZE * 32 is_long = helpers.get_extension_state(self, ('ExtEntityPositions', 1)) if (p_block_xpos + config.BORDER_SIZE > config.map_dims.x): print("moving player +x!") self.last_teleport_time = time.time() teleport_packet = helpers.gen_pos_packet( -1, (config.map_dims.center_x * 32) - big_border_size, self.ypos, self.zpos, self.yaw, self.pitch, is_long) self.xpos = (config.map_dims.center_x // 2) * 32 self.x_offset += config.map_dims.center_x self.add_packet(teleport_packet) bbu_sm_thread = threading.Thread( target=map_handler.update_player_map_data, args=(prev_x_offset, self.z_offset, self.x_offset, self.z_offset, self, teleport_packet)) bbu_sm_thread.start() elif (p_block_xpos - config.BORDER_SIZE < 0): print("moving player -x!") self.last_teleport_time = time.time() teleport_packet = helpers.gen_pos_packet( -1, (config.map_dims.center_x * 32) + big_border_size, self.ypos, self.zpos, self.yaw, self.pitch, is_long) self.xpos = (config.map_dims.center_x // 2) * 32 self.x_offset -= config.map_dims.center_x self.add_packet(teleport_packet) bbu_sm_thread = threading.Thread( target=map_handler.update_player_map_data, args=(prev_x_offset, self.z_offset, self.x_offset, self.z_offset, self, teleport_packet)) bbu_sm_thread.start() elif (p_block_zpos + config.BORDER_SIZE > config.map_dims.z): print("moving player +z!") self.last_teleport_time = time.time() teleport_packet = helpers.gen_pos_packet( -1, self.xpos, self.ypos, (config.map_dims.center_z * 32) - big_border_size, self.yaw, self.pitch, is_long) self.zpos = (config.map_dims.center_z // 2) * 32 self.z_offset += config.map_dims.center_z self.add_packet(teleport_packet) bbu_sm_thread = threading.Thread( target=map_handler.update_player_map_data, args=(self.x_offset, prev_z_offset, self.x_offset, self.z_offset, self, teleport_packet)) bbu_sm_thread.start() elif (p_block_zpos - config.BORDER_SIZE < 0): print("moving player -z!") self.last_teleport_time = time.time() teleport_packet = helpers.gen_pos_packet( -1, self.xpos, self.ypos, (config.map_dims.center_z * 32) + big_border_size, self.yaw, self.pitch, is_long) self.zpos = (config.map_dims.center_z // 2) * 32 self.z_offset -= config.map_dims.center_z self.add_packet(teleport_packet) bbu_sm_thread = threading.Thread( target=map_handler.update_player_map_data, args=(self.x_offset, prev_z_offset, self.x_offset, self.z_offset, self, teleport_packet)) bbu_sm_thread.start() pass