/*
  Called when this packet has been received.
  Creates the PlayerMP of the client, then assign him/her a tempID.
   */
  @Override
  public void received(Connection connection, Object object) {

    /*
    Instance of the ServerHandler.
     */
    ServerHandler serverHandler = ServerHandler.instance;

    /*
    The packet that was received.
     */
    Packet01LoginRequest packet = (Packet01LoginRequest) object;

    /*
    Create the PlayerMP!
     */
    PlayerMP playerMP = new PlayerMP();
    playerMP.tempID = new UID().generate();
    playerMP.name = packet.name;
    playerMP.x = 0;
    playerMP.y = 0;

    Logger.PLAYER_CONNECTED(playerMP.name, playerMP.tempID);

    /*
    Create the list of networked players that we will send to this player (the one that just connected!).
     */
    ArrayList<PlayerMP> list = new ArrayList<>();
    synchronized (serverHandler.playerMap) {
      if (!serverHandler.playerMap.isEmpty()) {
        for (PlayerMP mp : serverHandler.playerMap.values()) {
          list.add(mp);
        }
      }
    }

    /*
    Send the login response.
     */
    serverHandler.packetHandler.sendPacket01LoginRequest(
        connection.getID(), true, "Accepted", playerMP.tempID, list);

    /*
    Add this player to the playerMap.
     */
    synchronized (serverHandler.playerMap) {
      serverHandler.playerMap.put(connection.getID(), playerMP);
    }

    /*
    Send all other players the login packet, signifying that this player has just logged in.
     */
    serverHandler.packetHandler.sendPacket02Login(
        connection.getID(), playerMP.tempID, playerMP.name, playerMP.x, playerMP.y);
  }
  /*
  Called when this packet has been received.
  Removes the player from the playerMap safely.
   */
  @Override
  public void received(Connection connection, Object object) {

    /*
    Instance of ServerHandler.
     */
    ServerHandler.instance.server.sendToAllExceptUDP(connection.getID(), object);

    /*
    The packet.
     */
    Packet03Disconnect packet = (Packet03Disconnect) object;

    /*
    Check if the packet has been sent with the correct tempID.
    Kicks the player if it is an invalid temporary ID.
     */
    synchronized (ServerHandler.instance.playerMap) {
      if (packet.tempID == null) {
        ServerHandler.instance.packetHandler.sendPacket04Kick(
            connection.getID(), "Invalid temporary ID");
        connection.close();
        return;
      }
      if (!packet.tempID.equals(ServerHandler.instance.playerMap.get(connection.getID()).tempID)) {
        ServerHandler.instance.packetHandler.sendPacket04Kick(
            connection.getID(), "Invalid temporary ID");
        connection.close();
        return;
      }
    }

    /*
    Remove the player that disconnected.
     */
    Iterator<PlayerMP> iterator = ServerHandler.instance.playerMap.values().iterator();
    while (iterator.hasNext()) {
      PlayerMP playerMP = iterator.next();
      if (playerMP.tempID.equals(packet.tempID)) {
        iterator.remove();

        Logger.PLAYER_DISCONNECTED(playerMP.name, playerMP.tempID);
      }
    }
  }