def _disconnect(self, connection): address = connection.get_address() player = None if not address: logging.debug("Invalid connection, ignoring") return logging.debug("Disconnecting connection %s" % connection.get_address()) try: connection.close() except IOError: pass if address in self._players_by_address: player = self.get_player_by_address(address) with self._connections_lock: del self._connections[address] if player: logging.info("Player %s has quit" % player.name) del self._players_by_address[address] del self._players[player.player_id] self.broadcast( DespawnPlayerPacket.make({"player_id": player.player_id})) self.broadcast( MessagePacket.make({ "player_id": 0, "message": "&e%s&f has quit!" % player.name }))
def _disconnect(self, connection): address = connection.get_address() player = None if not address: logging.debug("Invalid connection, ignoring") return logging.debug("Disconnecting connection %s" % connection.get_address()) try: connection.close() except IOError: pass if address in self._players_by_address: player = self.get_player_by_address(address) with self._connections_lock: del self._connections[address] if player: logging.info("Player %s has quit" % player.name) del self._players_by_address[address] del self._players[player.player_id] self.broadcast(DespawnPlayerPacket.make({"player_id": player.player_id})) self.broadcast(MessagePacket.make({"player_id": 0, "message": "&e%s&f has quit!" % player.name}))
def kick_player(self, player_id, reason): player = self._players[player_id] logging.info("Kicking player %s for %s" % (player.name, reason)) player.connection.send(DisconnectPlayerPacket.make({"reason": reason})) logging.info("KICK PLAYER: acquiring player lock") with self._players_lock: logging.info("KICK PLAYER: player lock acquired") del self._players[player_id] logging.info("KICK PLAYER: player lock released") self.broadcast(MessagePacket.make({"player_id": 0, "message": "Player %s kicked, %s" % (player.name, reason)})) self._disconnect(player.connection)
def _save_thread(self): while self._running: try: self.broadcast(MessagePacket.make({ "player_id": 0, "message": "Autosaving the world..." })) self.save_world() time.sleep(120) except BaseException as ex: logging.error("Autosaving failed: %s" % repr(ex)) logging.debug(traceback.format_exc()) self.save_world()
def kick_player(self, player_id, reason): """ Kicks the player given an ID. :param player_id: The ID of the target player. :type player_id: int :param reason: The reason to be reported to the player. :type reason: str """ player = self._players[player_id] logging.info("Kicking player %s for %s" % (player.name, reason)) player.connection.send(DisconnectPlayerPacket.make({"reason": reason})) with self._players_lock: del self._players[player_id] self.broadcast(MessagePacket.make({"player_id": 0, "message": "Player %s kicked, %s" % (player.name, reason)})) self._disconnect(player.connection)
def _save_thread(self): while self._running: try: self.broadcast( MessagePacket.make({ "player_id": 0, "message": "Autosaving the world..." })) self.save_world() time.sleep(120) except: logging.error("Autosaving failed") logging.debug(traceback.format_exc()) self.save_world()
def _disconnect(self, connection): player = None if connection.get_address() in self._players_by_address: player = self.get_player_by_address(connection.get_address()) logging.info("DISCONNECT: Acquiring connection lock") with self._connections_lock: logging.info("DISCONNECT: connection lock acquired") del self._connections[connection.get_address()] logging.info("DISCONNECT: connection lock released") if player: logging.info("Player %s has quit" % player.name) del self._players_by_address[connection.get_address()] del self._players[player.player_id] self.broadcast(DespawnPlayerPacket.make({"player_id": player.player_id})) self.broadcast(MessagePacket.make({"player_id": 0, "message": "&e%s&f has quit!" % player.name}))
def kick_player(self, player_id, reason): """ Kicks the player given an ID. :param player_id: The ID of the target player. :type player_id: int :param reason: The reason to be reported to the player. :type reason: str """ player = self._players[player_id] logging.info("Kicking player %s for %s" % (player.name, reason)) player.connection.send(DisconnectPlayerPacket.make({"reason": reason})) with self._players_lock: del self._players[player_id] self.broadcast( MessagePacket.make({ "player_id": 0, "message": "Player %s kicked, %s" % (player.name, reason) })) self._disconnect(player.connection)
def handle_command(server, player, command, args): if command == "tp": if len(args) == 3: try: player.coordinates = [float(args[0]), float(args[1]), float(args[2])] player.connection.send( PositionAndOrientationPacket.make( { "player_id": -1, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch, } ) ) server.broadcast( PositionAndOrientationPacket.make( { "player_id": player.player_id, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch, } ) ) except ValueError: player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4Invalid coordinates,"})) player.connection.send( MessagePacket.make({"player_id": 0, "message": "&4please use /tp <x> <y> <z> or"}) ) player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4/tp <playerName>"})) elif len(args) == 1: for target_player in server.get_players().values(): if target_player.name == args[0]: player.coordinates = target_player.coordinates player.connection.send( PositionAndOrientationPacket.make( { "player_id": -1, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch, } ) ) server.broadcast( PositionAndOrientationPacket.make( { "player_id": player.player_id, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch, } ) ) break else: player.connection.send( MessagePacket.make({"player_id": 0, "message": "&4Target player not found."}) ) else: player.connection.send( MessagePacket.make( {"player_id": 0, "message": "&4Invalid arguments, please use /tp <x> <y> <z> or"} ) ) player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4/tp <playerName>"})) elif command == "kick": if server.is_op(player.name): if len(args) >= 1: player_name = args[0] reason = " ".join(args[1:]) if len(args) > 1 else "No reason specified" for target_player in server.get_players().values(): if target_player.name == player_name: server.kick_player(target_player.player_id, reason) break else: player.connection.send( MessagePacket.make({"player_id": 0, "message": "&4Couldn't find the player specified."}) ) else: player.connection.send( MessagePacket.make({"player_id": 0, "message": "&4Usage: /kick <playerName> [reason]"}) ) else: player.connection.send( MessagePacket.make({"player_id": 0, "message": "&4You need to be an op to do that!"}) ) elif command == "help": for line in HELP_TEXT.split("\n"): line = line.strip() player.connection.send(MessagePacket.make({"player_id": 0, "message": "%s" % line}))
def handle_command(server, player, command, args): """ Handles the in-game commands. :param server: The parent server object. :type server: ClassicServer :param player: The player who issued the command. :type player: Player :param command: The command issued, without the leading slash. :type command: str :param args: The arguments for the command :type args: list """ if command == "tp": if len(args) == 3: try: player.coordinates = [float(args[0]), float(args[1]), float(args[2])] player.connection.send(PositionAndOrientationPacket.make({"player_id": -1, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch })) server.broadcast(PositionAndOrientationPacket.make({"player_id": player.player_id, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch })) except ValueError: player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4Invalid coordinates,"})) player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4please use /tp <x> <y> <z> or"})) player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4/tp <playerName>"})) elif len(args) == 1: for target_player in server.get_players().values(): if target_player.name == args[0]: player.coordinates = target_player.coordinates player.connection.send(PositionAndOrientationPacket.make({"player_id": -1, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch })) server.broadcast(PositionAndOrientationPacket.make({"player_id": player.player_id, "frac_x": int(player.coordinates[0] * 32), "frac_y": int(player.coordinates[1] * 32), "frac_z": int(player.coordinates[2] * 32), "yaw": player.yaw, "pitch": player.pitch })) break else: player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4Target player not found."})) else: player.connection.send(MessagePacket.make({ "player_id": 0, "message": "&4Invalid arguments, please use /tp <x> <y> <z> or" })) player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4/tp <playerName>"})) elif command == "kick": if server.is_op(player.name): if len(args) >= 1: player_name = args[0] reason = " ".join(args[1:]) if len(args) > 1 else "No reason specified" for target_player in server.get_players().values(): if target_player.name == player_name: server.kick_player(target_player.player_id, reason) break else: player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4Couldn't find the player specified."})) else: player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4Usage: /kick <playerName> [reason]"})) else: player.connection.send(MessagePacket.make({"player_id": 0, "message": "&4You need to be an op to do that!"})) elif command == "help": for line in HELP_TEXT.split("\n"): line = line.strip() player.connection.send(MessagePacket.make({"player_id": 0, "message": "%s" % line}))